Design Pattern in Java

Design Pattern in Java is the best practices for resolving problems in object-oriented software development. There are 23 design patterns which can be classified in 3 categories:

  • Creational pattern
  • Structural pattern
  • Behavioral pattern


< Java Technology


I. Creational pattern

1. Singleton Pattern

Singleton Pattern is a design pattern which you often meet in almost applications. Singleton Pattern provides a way to instantiate only one instance in an application context.

java design pattern - singleton pattern

>>> More detail at: Java Design Pattern – Singleton Pattern

2. Factory Pattern

Factory Pattern provides an interface for integration and hidden the complex implementation of related components.

java design pattern - Factory Pattern

>>> More details at: Java Design Pattern – Factory Pattern

3. Abstract Factory Pattern

Abstract factory pattern is a Super-Factory of a group familiar factories. We can use it to expose API for integration code and hidden the logic implementation. Abstract Factory gives us a way to manage and decouple design in case Program has more than one Factory having the same Interface.

Overview – Abstract Factory Pattern
– Program has a MobileFactory interface that define API. SamsungFactory and AppleFactory are 2 classes that implement Mobile Factory.
– Product includes: SmartPhone & Table. AppleFactory produces: Iphone6, IPhone6Plus, IPadPro, IPadMini4. SamsungFactory produces: GalaxyJ7, GalaxyOn7, GalaxyTabA, GalaxyTabS2.

java design pattern - Abstract Factory Pattern

>>> More details at: Java Design Pattern – Abstract Factory Pattern

4. Builder Pattern

Builder Pattern is under creational pattern for building a complex object in software development.

>>> More details at: Java Design Pattern – Builder Pattern

5. Prototype Pattern

In many cases, creating a new instance is a costly operation. Fortunately, we can copy or clone an instance of an existing one instead of creating from scratch. This approach can be done by using Prototype Pattern.

java-prototype-pattern-example-sample-structure

>>> More details at: Java Prototype Pattern tutorial with example

II. Structural pattern

1. Adapter Pattern

Adapter Pattern is a Structural Pattern in which, an interface of a class is converted into another interface that Client expects to work. With Adapter Pattern, we can reuse existing code without changing it.

Adapter Pattern defines an Adapter that can adapt a target class/interface (that we called Adaptee) to Client‘s requirement.

java design pattern - adapter-pattern-overview

The Adapter contains an instance of Adaptee (also hides it from Client). It helps that, Client calls Adapter methods without knowing anything about Adaptee, then Adapter uses Adaptee instance inside to call its appropriate methods.

java design pattern - adapter-pattern-diagram

>>> More details at: Adapter Pattern in Java

2. Facade Pattern

Facade Pattern is a Structural Pattern in which, a set of interfaces is gathered into only one interface that Client can work easily and simply. Facade Pattern helps us to wrap complexities of components inside and lose coupling between Client and subSystems.

Facade Pattern defines a higher interface/object – Facade – that wraps some lower interfaces/objects. Whenever Client wants to make a chain of complex actions which require many methods and interaction, it just call one simple Facade method, all operation can be done inside that method.

java design pattern - facade-pattern-overview

>>> More details at: Facade Pattern in Java

3. Decorator Pattern

Decorator Pattern is a Structural Pattern in which, functionality can be added to an existing object without changing its structure.

Decorator Pattern defines one or many Classes that wrap original Class. Everytime we decorate original Class with a Decorator Class, a functionality is added.

java design pattern - decorator-pattern-overview

There is an abstract Decorator Class which implements Component interface to get its original operation() function, then each concrete Decorator override operation() method to add its own functionality decorate() using instance of original Class Component.

java design pattern - decorator-pattern-structure

So, when Client wants to add more functionality, it just calls appropriate constructor method corresponding to specific Decorator.

>>> More details at: Decorator Pattern in Java

III. Behavioral pattern

1. Observer Pattern in Java

Observer Pattern is a Behavioral Pattern in which, an Object (we call it Subject) contains a list of dependency Objects (we call them Observers). Observers will be notified and updated automatically whenever Subject changes its state. Today we’re gonna look at how Observer Pattern in Java works.

Observer Pattern defines one-to-many relationship between Subject and Observers:
– The Subject maintains a list of Observers and has methods for adding, removing and notifying its Observers.
– When Subject changes its state, all Observers are notified and updated automatically.
– The Subject only know list of Observers and doesn’t care how they implement.
The image below shows how they interact with each other.

java design pattern - observer-pattern-overview

>>> More details at: Observer Pattern in Java

2. Chain of Responsibility Pattern

Chain of Responsibility Pattern is a Behavioral Pattern in which, a Request is sent through a chain of handling Objects without knowing which Object will handle it. With Chain of Responsibility Pattern, we can avoid coupling the Sender of a request to received Objects and create a processing sequence automatically based on type of request.

Chain of Responsibility Pattern defines a set of Handlers that receive request from Sender. Each Handler processes it if possible or pass to another Handler, or does both: process and pass it on.

java design pattern - chain-of-responsibility-overview

– We have an Abstract Handler class that contains inner Successor and handle method for processing Request and pass it to another Successor.
– All Handlers extend Abstract Handler class. Each Handler sets its own Successor (the next Handler).
– Client just gives Request for the first Handler to process, next Handler will handle Request automatically anytime it receives.

>>> More details at: Chain of Responsibility Pattern in Java


All posts: Design Pattern