Monday, September 13, 2010

Symptoms of Problems in Design, and Design Principles to follow

Symptoms
  • difficult to change
  • every change cascades changes in other dependent modules
  • software break in many places, with every change
  •  inability to reuse from other projects or from the same project
  • design preserving changes are harder than hacks
Causes

  • changing requirements
  • improper dependencies between the modules


    Principle of object oriented class design

    • OCP (open close principle), module should be open to extension but close for modification,
    • means the ability to change what modules do without changing the source code of the module
    • abstraction is the key to OCP
    • dynamic polymorphism(virtual functions) and static polymorphism (templates) are way to achieve OCP

    • LSP (Liskov Substitution Principle), subclasses should be substitutable by their base class
    • a user of base class should continue to function properly if a derivative of the base class is passed to it
    • in order to be substitutable the contract of the base class must be honored by the derived class
    • contract of a method is stated by its preconditions(what will be true before entering in the function) and postconditions(what will be true after the function execution is complete).
    • so a derived class is substitutable for its base class if
    1. derived class method preconditions are no stronger than the base class method
    2. derived class method postconditions are no weaker than the base class method

    • DIP (Dependency Inversion Principle), depend upon Abstractions, not on concretions
    • primary mechanism of OO architecture,idea behind component design COM, CORBA, EJB etc.
    • every dependency in the design should target interface or an abstract class, as the concrete implementation changes a lot (most of the time) 

    • ISP (Interface Segregation Principle), many client specific interfaces are better than one general purpose interface
    • needs of one client might differ from other client, when using the same interface, thus create client specific interface, and multiply inherit them into the concrete class
    • the clients should be categorized by their type
    • there is no harm in adding the same method to different interface of different clients, if both client need the same method
    • change in existing interface, is discouraged, rather adding a new interface is recommended
    http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf

    No comments: