Saturday, 10 January 2015

10 Very Useful OOP Principles For The Java Designer!

ava may not be the newest programming language around, but it is still one of the most popular. This is an Object Oriented Programming (OOP) language, which has become a mainstay in most enterprise and home projects.

Here are a few good design principles you should follow:

Java, SRP, ISP, Java designers, Java coding, coding in Java, Java programming, Java programmers, tips for Java coding




1. 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.

2. 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.

3. Design Principle

Here you should try and follow an open closed design principle. This means that you classes, functions or methods should be Open 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.

4. Single Responsibility Principle (SRP)

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.

5. Dependency

The framework 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.

6. 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.

7. 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.

8. 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;

9. 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.

10. Delegate

Delegate the tasks of your program to specific to classes. An example of this would be the equals () and hashCode() method.
Related Posts Plugin for WordPress, Blogger...