Tuesday, March 16, 2010

Object Oriented design principles (OOD) - JAVA

All the major Object Oriented Design Principles are mentioned here with their corresponding design pattern examples.


1. The Accessibility minimization of Classes and Members by 
    following concepts 
  • Encapsulation
  • Abstraction
  • Use Accessors and Mutators, Not Public Members
2. Favor Composition over Inheritance
    Example - Strategy Pattern


3. Encapsulate what varies in the Design implementation and 
    keep it separate
    Example - Strategy Pattern
      
4. Program to an Interface, not an Implementation
    In case of Java, prefer Interface inheritance(Sub typing) to 
    Class inheritance  


5. Go for loosely coupled designs between objects that interact
    Example - Observer Pattern


6. The Open-Close Principle - Software entities should be open for 
    extension, but closed for modification.
    Example - Decorator Pattern
  • Its corollary is "The Single Choice Principle" which says - Whenever a Software system must support a set of alternatives, ideally only one class in the system knows the entire set of alternatives
7. Depend on Abstraction, do not depend on concrete classes   
    Example - Factory Method & Abstract Factory


8. Super Classes should call their Sub Classes not vice versa
    Example - Template Method Pattern


9. A class should have only one reason to change
    Example - Iterator & Composite Patterns


10.The Liskov Substitution Principle -  Functions that uses references 
    to the Super Class must be able to use Derived Classes without  
    knowing it.




Please note that any design pattern is usually based on more than one Object Oriented design principles.
examples are given for all the principles, where the pattern mentioned has unique implementation 

Monday, March 15, 2010

Difference: Design Pattern, Framework, Architecture & Methodology

Design Pattern : Design Pattern is a tried and tested solution to a specific & common design problem. The design pattern suggests how you will organise your classes and methods to do a specific activity. This is a generic model and can be used anywhere in the application. A design pattern has a name, motivation (goal for using the pattern), problem scenario, consequence (side affects and trade-offs of using the pattern) etc. Eg - Java APIs use design patterns like Decorator. 
Struts framework also uses design patterns. Design patterns are best practices for designing applications & also to avoid silly object oriented design mistakes.


Framework: Is a generic solution built for specific kind of solutions. such as web applications, web services. A framework is not just a collection of classes and intefaces with API exposed but one of the critical aspects is a framework is extendible to build custom application. There is a slight difference between products and frameworks. Product does a specific work and not extensible where as framework is. Framework is implementation of modelling  so it is a ready to use solution. Eg - Struts, ORM Hibernate framework

Architecture: Architecture at very highest level is how/which components are placed where. This is contextual. For example: if you are defining system architecture then you will define which technologies to be placed how and where. If you are defining application architecture then you will specify which application components are placed how and where? Architecture definition involves identifying tiers and further different layers to separate each unit responsibility.  Eg- MVC Architecture(Used in Struts, JSF, Spring, Swing etc) 

Frameworks are based on architectures.

Methodology: is used to structure, plan and control the process of development of the application. 

Design methodology is more about the manners to do a specific activity. you may follow specific SDLC methodology to run the complete project, you may follow an architecture methodology (Architectural pattern such as Zachmann, Togaf, DOD) for defining architecture. you may follow specific test methodology for testing process and so on.




 So, most abstract is Architecture, Frameworks are based on architecture. Framework uses design patterns to keep design problems at bay. Methodology takes care of the process flow.


                         
           
                         uses                                                       has
Architecture --------> Framework  -------- Design Patterns         
             most abstract                                        least abstract