Posts Tagged ‘object oriented design principle’

Following are design principle, which should be taken care when designing/implementing Object oriented system. This also fulfills requirement for design patterns.

1. SRP(Single Responsibility Principle)

According to this principle, a class should be handling a single functionality. In addition, there should be more than one reason for a developer to change a class from what it currently does. For example, if you have multiple functionalities in a class in Java then you might face problems when you are coupling this fake Java.

2. Open Close Principle

Open closed design principle means that your classes, functions or methods should be Open for inheritance/overriding so that new functionality can be added easily. The Open Closed Design Principle focuses on this aspect and makes it possible to avoid unauthorised changes to already tested and working code.

3. Liskov Substitution Principle (LSP)

This principle states that you must be able to subtypes for super types. This means that any methods or functions in your program that are making the use of super class types, then they should also be able to work with the sub class. It is in close relation to SRP and the Interface Segregation Principle.

4. Interface Segregation principle (ISP)

This principle tells you to ensure that clients do not implement any interface that it doesn’t use. In Java, an interface has the disadvantage from the design point of view, to implement all methods before the classes can use it;

5. Delegate

Delegate the tasks of your program to specific to classes. An example of this would be the equals () and hashCode() method.

All above principles are also called as SOLID oo principle

6. DRY

DRY stands for Don’t Repeat Yourself, which is pretty self explanatory. This means that you shouldn’t be duplicating your code in a program. If a code fragment is appearing in two places, then you would do well to turn it into a method instead of writing the entire fragment again. Lack of duplication helps in easy maintenance of the code.

7. Encapsulation

Whether your project gets bought over or whether you work on it yourself, your code will always change. So, it is good to encapsulate the part of the code that you think will be changed. This makes you code easy to maintain and test.

8. Dependency

The framework(spring, guice etc) that you use already provides you dependence, which means that looking for it a additionally will be a waste of time. You can also use byte code instrumentation as well. Some Aspect Oriented Programming (AOP) framework already does this. Lastly, you can also make the use of proxies.

9. Composition over Inheritance

According to many programmers, composition is more important than inheritance. They say that you should be focusing on the composition of your program than on employing a flexible inheritance structure. This is because composition allows you to change the behaviour of a class at runtime. Interface on the other hand allows you to use polymorphism, providing the flexibility to replace something with better implementation.

10. Programming for Interface

A programmer should always be programming for the interface and not for the implementation that will give them flexibility in code. Make use of interface types on variables and return types on methods. In addition, argument types should also be use.