Java Design Patterns
Design Patterns are very popular among software developers. A design pattern is a well described solution to a common software problem. I have written extensively on java design patterns. You can download PDF eBook (130+ pages) by subscribing to our newsletter.
Java Design Patterns
Some of the benefits of using design patterns are:
- Design Patterns are already defined and provides industry standard approach to solve a recurring problem, so it saves time if we sensibly use the design pattern. There are many java design patterns that we can use in our java based projects.
- Using design patterns promotes reusability that leads to more robust and highly maintainable code. It helps in reducing total cost of ownership (TCO) of the software product.
- Since design patterns are already defined, it makes our code easy to understand and debug. It leads to faster development and new members of team understand it easily.
Java Design Patterns are divided into three categories – creational, structural, and behavioral design patterns.
Creational Design PatternsCreational design patterns provide solution to instantiate a object in the best possible way for specific situations.
- Singleton Pattern
- Factory Pattern
- Abstract Factory Pattern
- Builder Pattern
- Prototype Pattern
Structural patterns provide different ways to create a class structure, for example using inheritance and composition to create a large object from small objects.
1. Adapter PatternAdapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter. As a real life example, we can think of a mobile charger as an adapter because mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). Composite Pattern
Composite pattern is one of the Structural design pattern and is used when we have to represent a part-whole hierarchy. When we need to create a structure in a way that the objects in the structure has to be treated the same way, we can apply composite design pattern.
2. Proxy PatternProxy pattern intent is to "Provide a surrogate or placeholder for another object to control access to it". The definition itself is very clear and proxy pattern is used when we want to provide controlled access of a functionality.
3. Flyweight PatternFlyweight design pattern is used when we need to create a lot of Objects of a class. Since every object consumes memory space that can be crucial for low memory devices, such as mobile devices or embedded systems, flyweight design pattern can be applied to reduce the load on memory by sharing objects.
4. Facade PatternFacade Pattern is used to help client applications to easily interact with the system. Suppose we have an application with set of interfaces to use MySql/Oracle database and to generate different types of reports, such as HTML report, PDF report etc. So we will have different set of interfaces to work with different types of database. Bridge Pattern
5.Bridge PatternWhen we have interface hierarchies in both interfaces as well as implementations, then bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client programs. Like Adapter pattern, its one of the Structural design pattern.