Dependency and System Design

  • Created by: wreje035
  • Created on: 14-10-18 23:30
What is Dependency?
When a piece of code depends on other code. (Fields or methods that a class or module uses. Libraries or frameworks). When a dependency is modified, it will affect the class.
1 of 21
What is coupling?
Coupling is uncontrolled dependency
2 of 21
What is cohesion?
How focused a piece of code is (does one thing well, not many things poorly).
3 of 21
Goal with cohesion and coupling?
We want high cohesion and low coupling.
4 of 21
Encapsulation?
Hides complex internal details of component from other dependent components. We can change private internal details without impacting dependent components. Should offer simplified API that reduces complexity of internal details.
5 of 21
What is indirection?
Decouples a component from its dependent components. Interfaces and injection.
6 of 21
Why do we need to control dependency?
Changes have a ripple effect. Eg, if we didnt have DAOs, we would have data access code scattered throughout the code base. Changing the storage mechanism would require a signif rewrite. Team dev - dont want changes in your code to affect your team.
7 of 21
Why do we need to control dependency?
Maintenance - highly cohesive systems are easier to work on (understand and locate bugs). Testing - high coupling makes it hard to test. Debugging - Higher coupling = more places a bug can come from.
8 of 21
How do we control dependency?
3 teir system. And dependencies should be one way.
9 of 21
Inversion of Control?
Moving control of a dependency out of the class that uses it. Dependency supplied by constructor or setter. Programming to the depenencies interface is very important. All about INDIRECTION.
10 of 21
Eg of inversion of control in project?
DAO injection in dialogs.
11 of 21
Why do we need inversion of control?
Makes the system more flexible. Can change a dependency with a single line of code. (Eg collection to JDBC). Mocks can replace dependencies for testing. Can use alternative dependencies for diff situs (desktop vs mobile)
12 of 21
Dependency Injection Framework
Provide dependencies to the objects that need them. Spring and Guice are popular java frameworks. Avoids nasty constructor chaining thats often required to get dependencies where they're needed.
13 of 21
Controlling Dependency
Requires good understanding of encapsulation. Too much encaps is jsut as bad as not enough.
14 of 21
Ravioli Code
Opposite of Spagetti code. A mess of high coupling and low cohesion. Modifiying any line of code has a high chance of causing bugs. High cohesion taken too far (feauture that couldbe one method becomes5 classes). hard to find file to modify
15 of 21
Side effects of controlling dependency
Adv: Abstract code is easier to reuse. Testing is easier. Maintenace is easier. Cohesive code easier to write. Dis: Design and dev can take longer. Can go overboard.
16 of 21
Rich domain models
anemic = extract data from object so we can store it. rich = instruct object to save itself. Rich can save/display/update/validate themselves.
17 of 21
Advantages of rich domain models
Better encapsulation - more scope for reuse. Higher cohesion - simpler code. Rich = display method is purely display related. Very flexible when combined with injection frameworks.
18 of 21
Disadvantages of rich domain models
Harder to do - lots of scope for null pointer exceptions. Chicken and egg situations. Bordering on taking things too far. Anemic models are the de facto standard.
19 of 21
Anemic model
extract data from object so we can store it. Display code mixed in with unrelated stuff in the constructor.
20 of 21
Middle ground between rich and anemic
Use frameworks to take care of the plumbing code - moving data between domain objects and GUI components and domain objects and data stores. Code is more coheive as frameworks do most of the work behind the scenes.
21 of 21

Other cards in this set

Card 2

Front

What is coupling?

Back

Coupling is uncontrolled dependency

Card 3

Front

What is cohesion?

Back

Preview of the front of card 3

Card 4

Front

Goal with cohesion and coupling?

Back

Preview of the front of card 4

Card 5

Front

Encapsulation?

Back

Preview of the front of card 5
View more cards

Comments

No comments have yet been made

Similar ICT resources:

See all ICT resources »See all Dependency and System Design resources »