0% found this document useful (0 votes)
3 views

GOF Design Pattern

The Gang of Four (GOF) design patterns, introduced in their 1994 book, include several key patterns such as Factory, Singleton, Adapter, and Observer. The Factory pattern allows for object creation without exposing the creation logic, the Singleton pattern ensures a class has only one instance, the Adapter pattern bridges incompatible interfaces, and the Observer pattern enables automatic updates of dependent objects when an event occurs. Each pattern addresses specific design problems and provides structured solutions for reusable object-oriented software development.

Uploaded by

vedaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

GOF Design Pattern

The Gang of Four (GOF) design patterns, introduced in their 1994 book, include several key patterns such as Factory, Singleton, Adapter, and Observer. The Factory pattern allows for object creation without exposing the creation logic, the Singleton pattern ensures a class has only one instance, the Adapter pattern bridges incompatible interfaces, and the Observer pattern enables automatic updates of dependent objects when an event occurs. Each pattern addresses specific design problems and provides structured solutions for reusable object-oriented software development.

Uploaded by

vedaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Explain GOF Design Pattern in Detail

In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book
titled Design Patterns - Elements of Reusable Object-Oriented Software. These authors are collectively
known as Gang of Four (GOF).

1. Factory Pattern
 Factory pattern is one of most used design pattern in Java.
 In Factory pattern, we create object without exposing the creation logic to the client and refer to
newly created object using a common interface.
Problem
Who is responsible for creating objects that are required for complex creation logic?
Solution
Create a pure fabrication object called Factory that handles the creation.
Implementation
Create a Shape interface and concrete classes implementing the Shapeinterface. FactoryPatternDemo,
class will use ShapeFactory to get a Shape object. It will pass information (CIRCLE / RECTANGLE /
SQUARE) to ShapeFactory to get the type of object it needs.
Step 1 Create an interface.
Step 2 Create concrete classes implementing the same interface.
Step 3 Create a Factory to generate object of concrete class based on given information.
ShapeFactory.java
Step 4 Use the Factory to get object of concrete class by passing information such as type.
Step 5 Verify the output.

2.Singleton pattern
 Singleton pattern is one of the simplest design patterns in Java
 Singleton Pattern says that just "define a class that has only one instance and provides a global
point of access to it"
There are two forms of singleton design pattern
 Early Instantiation: creation of instance at load time.
 Lazy Instantiation: creation of instance when required.
Problem:
Singleton pattern is created in a situation in which only one instance of a class must be created and this
instance can be used as a global point of access.
Solution:
Define a static method of the class that returns singleton.
Implementation:
 Create a SingleObject class.
 SingleObject class has its constructor as private.
 SingleObject class provides a static method to get its static instance to outside world.
 SingletonPatternDemo, class will use SingleObject class to get a SingleObject object.

Step 1: Create a Singleton Class.

public class SingleObject {


private static SingleObject instance = new SingleObject();
private SingleObject(){ }
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}

Step 2 :Get the only object from the singleton class.


public class SingletonPatternDemo {
public static void main(String[] args) {
SingleObject object = SingleObject.getInstance();
object.showMessage();
}
}

Step 3: Verify the output.

Hello World!

3.Adapter 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.
Problem:
How to resolve incompatible interfaces. How to provide a stable interface to the intermediate having
different interfaces?
Solution:
Convert the original interface of component into another interface through intermediate adapter object.
Implementation:

We have a MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer
interface. AudioPlayer can play mp3 format audio files by default. We are having another interface
AdvancedMediaPlayer and concrete classes implementing the AdvancedMediaPlayer interface. These
classes can play vlc and mp4 format files.
We want to make AudioPlayer to play other formats . To attain this, we have created an adapter class
MediaAdapter which implements the MediaPlayer interface and usesAdvancedMediaPlayer objects to
play the required format. AdapterPatternDemo, our demo class will use AudioPlayer class to play various
formats.
4.Observer Pattern

The Observer pattern is a design pattern that defines a link between objects so that when one objects state
changes, all dependent objects are updated automatically.
Problem:
What could be done when some object generates an event and in response to that event different kinds of
subscriber objects changes the state.
Solution:
Define the subscriber interface. The publisher dynamically registers the corresponding subscribers and
notifies them when some event occurs.
Implementation:
Consider that a school sends the notice to the students via some SMS or by email. The notification system
works as soon as some event occurs. For example – if the notice like “ Due to heavy rainfall the school
will remain closed today” can be sent to the students either as a SMS or as an email when heavy rainfall
occurs. Here the school Notification system object will act as a publisher and student object will be
subscriber.

s.

You might also like