Thursday, March 17, 2016

[Design Patterns] 6 principals

  1. single duty
  2. we can always replace parent class object by his sub class (try to not overload and override parents' methos) ---Barbara Liskov
    • EX :
    •  then we need tow new functions:
    • 1 do sum  2 add 100 to the sum
    •  these two functions will be done with class B
    • if we write like this
    • here when we use b to replace a, there will be errors
    • so we should not override the func1, we should remain it to A and B will do the same thing as A for func1

  3.  Higher level abstraction should not depend on lower level abstraction EX:
    • mother read book :
    • when we want mother to read a Newspaper: she cannot! she accept only the class Book!
    • so we should do :
    • interface
    •  here, mother as an higher level abstraction, her behavior read is not depended on the lower abstraction ---- what she will read.
  4.  Interface isolation a class should implement the smallest interface needed to reduce the dependencies between them.
  5.  One object should have least knowledges of another class   
  6. Classes, modules, methods should be open to extend but closed to modification
 from : http://www.uml.org.cn/sjms/201211023.asp

EXs:
 1 and 2 is good
3 and 4 is acceptable
5 does not obey the design principals  need refactoring
6 obeys too much the design principles need refactoring
































No comments:

Post a Comment