- Coupling
- among classes
- among subsystems, modules
- related classes knowing the internal details of each other
- when one is changed, other has to be changed
- makes the system difficult to understand
- no possibility of reuse of the entwined code
- Cohesion
- inside the class, among the class attributes and behavior
- well defined roles for the classes within the system
- the data is closely related with the functions which the class is providing
- Law of Demeter (more specific case of "low coupling", LoD)
- motto is "only talk to your immediate friends"
- each unit should have only limited knowledge about other units "closely" related to the current unit (method/function "f")
- in context of OOD, closely related
- are the functions of the class in which unit (function "f") exists
- other argument classes of unit (function "f")
- functions of immediate part classes (computed and stored) of class of unit (function "f")
- computed classes are the classes that are the return type of functions of this class
- stored classes are the classes of the data members of this class
- Tell (what to do, command) don't ask (putting class responsibilities in right place)
- tell objects what you want them to do
- do not ask questions about their state and then make a decision
- why not ask ? the decision (based on some state) has to made by the owner of that state
- for a non owner to take decision based on some other class state violates encapsulation, and increases coupling
- design the classes based on their responsibilities, specify the commands that the classes may execute
- if the responsibility of the class is rightly designed, it leads to loose coupling among classes
- Say it once and only once (don't repeat yourself ,DRY)
- avoiding the duplication of information (code) in a program
- "the rule of three" devised by Martin Fowler states that of a piece of code is copied more than once (3 or more copies) then it need to be abstracted out.
- if different pieces of code are performing the same function then, abstract out the parts that varies, and combine the part that is same for all (thus not repeating).
References
http://www.ccs.neu.edu/research/demeter/demeter-method/LawOfDemeter/general-formulation.html
http://pragprog.com/articles/tell-dont-ask
http://en.wikipedia.org/wiki/Abstraction_principle_(programming)
No comments:
Post a Comment