Java Design Patterns
Design Patterns are very popular among software developers. A design pattern is a well-described solution to a common software problem.
Now, a question will be arising in your mind what kind of well-described solution? Let me explain by taking an example.
Problem Given:
Suppose you want to create a class for which only a single instance (or object) should be created and that single object can be used by all other classes.
Solution:
Singleton design pattern is the best solution to the above specific problem. So, every design pattern has some specification or set of rules for solving the problems. What are those specifications? you will see later in the types of design patterns.
But remember one thing, design patterns are programming language independent strategies for solving the common object-oriented design problems. That means a design pattern represents an idea, not a particular implementation.
By using the design patterns you can make your code more flexible, reusable and maintainable. It is the most important part because java internally follows design patterns.
To become a professional software developer, you must know at least some popular solutions (i.e. design patterns) to the coding problems.
Benefits of using design patterns
- Reusable in multiple projects.
- Capture software engineering experiences.
- Provide transparency to the design of an application.
- Provide an industry standard approach to solve a recurring problem.
- Make our code easy to understand and debug.
Mainly there are three categories of Design Patterns
- Creational Design Pattern
- Factory Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Prototype Pattern
- Builder Pattern
2. Structural Design Pattern
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
3. Behavioral Design Pattern
- Chain Of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
Here I am going to explain some design patterns.
Facade Design Pattern
A facade is an object. It provides a simplified interface to a larger body of code. The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client.
When?
- A system is very complex or difficult to understand.
- A simple interface is required to access a complex system.
- The abstractions and implementations of a subsystem are tightly coupled.
Structure
We are going to create an Attend interface and concrete classes implementing the Attend interface. A facade class Facade is defined in the next step.
Facade class uses the concrete classes to delegate user calls to these classes. Example1, our main class, will use Facade class to show the results.
You can get the example code here.
Run the code then you will get a result like this.
Singleton Design Pattern
This is the simplest design pattern in Java. This pattern interacts with a single class which is responsible to create an object( making sure that only a single object gets created). Without initiating the object of a class we can access this only Object.
Structure
We’re going to create a DatabaseConnection class. DatabaseConnection class have its constructor as private and have a static instance of itself.
DatabaseConnection class provides a static method to get its static instance to the outside world. Example2 our main class will use DatabaseConnection class to get a DatabaseConnection object.
You can get the example code here.
Run the code then you will get a result like this.
Factory Design Pattern
Normally we use the constructor to create an Object. But if we need to do more behind the scene we can create a factory to create Objects. Here we define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate.
When?
- A class doesn’t know what sub-classes will be required to create
- A class wants its subclasses to specify the objects to be created.
- The parent classes choose the creation of objects to its sub-classes.
Structure
We’re going to create a Mammal interface and concrete classes implementing the Mammal interface. A factory class Factory is defined as a next step.
Example3 Class, our main class will use Factory to get a Mammal object. It will pass information (Human/Zebra) to Factory to get the type of object it needs.
You can get the example code here.
Run the code then you will get a result like this.
Adapter Design Pattern
Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
When?
- The client makes a request to the adapter by calling a method on it using the target interface.
- The adapter translates that request on the adaptee using the adaptee interface.
- Client receives the results of the call and is unaware of the adapter’s presence.
Structure
We are demonstrating the use of the Adapter pattern from an Example like one Englishman trying to talk and write German language by getting help from an intermediate person. Here Intermediate person acts as an Adaptor.
We have an English interface and a concrete class LanguageAdapter implementing the English interface. Englishman can talk and write English default.
We are having another interface German and concrete classes implementing the German interface. These classes can talk and write German itself.
We want to make Englishman class to write and talk other languages as well. To attain this, we have created an adapter class LanguageAdapter which implements the English interface and uses German objects to talk and write the German language.
Englishman uses the adapter class LanguageAdapter passing it the desired language type without knowing the actual class which can write or talk the specific language. Example4, our demo main class will use Englishman class to write and talk different languages.
You can get the example code here.
Run the code then you will get a result like this.
Decorator Design Pattern
You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class.
The decorator pattern allows a user to add new functionality to an existing object without altering its structure.
Structure
We’re going to create a Shape interface and concrete classes implementing the Shape interface. We will then create an abstract decorator class ShapeDecorator implementing the Shape interface and having Shape object as its instance variable.
RedBorderDecorator is a concrete class implementing ShapeDecorator.
Example5, our main demo class will use RedBorderDecorator to decorate Shape objects.
You can get the example code here.
Run the code then you will get a result like this.
See you in the next tutorial.
Thank you!