Design Pattern Tutorial
Design Pattern Tutorial
This tutorial will take you through step by step approach and examples using Java
while learning Design Pattern concepts.
Audience
This reference has been prepared for the experienced developers to provide best
solutions to certain problems faced during software development and for un-
experienced developers to learn software design in an easy and faster way.
Prerequisites
Before you start proceeding with this tutorial, we make an assumption that you
are already aware of the basic concepts of Java programming.
All the content and graphics published in this e-book are the property of Tutorials
Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy,
distribute or republish any contents or a part of contents of this e-book in any
manner without written consent of the publisher.
We strive to update the contents of our website and tutorials as timely and as
precisely as possible, however, the contents may contain inaccuracies or errors.
Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy,
timeliness or completeness of our website or its contents including this tutorial. If
you discover any errors on our website or in this tutorial, please notify us at
contact@tutorialspoint.com
i
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Implementation ...................................................................................................................................... 3
Implementation ...................................................................................................................................... 7
Implementation .................................................................................................................................... 15
Implementation .................................................................................................................................... 18
Implementation .................................................................................................................................... 26
Implementation .................................................................................................................................... 32
8. BRIDGE PATTERN............................................................................................................... 38
Implementation .................................................................................................................................... 38
ii
9. FILTER PATTERN ................................................................................................................ 42
Implementation .................................................................................................................................... 42
Implementation .................................................................................................................................... 50
Implementation .................................................................................................................................... 54
Implementation .................................................................................................................................... 59
Implementation .................................................................................................................................... 63
Implementation .................................................................................................................................... 69
Implementation .................................................................................................................................... 72
Implementation .................................................................................................................................... 77
Implementation .................................................................................................................................... 81
Implementation .................................................................................................................................... 85
Implementation .................................................................................................................................... 88
Implementation .................................................................................................................................... 91
iii
21. OBSERVER PATTERN .......................................................................................................... 95
Implementation .................................................................................................................................... 95
iv
33. SERVICE LOCATOR PATTERN............................................................................................ 151
v
DESIGN PATTERN OVERVIEW
These authors are collectively known as Gang of Four (GOF). According to these
authors design patterns are primarily based on the following principles of object
orientated design.
Best Practices
Design patterns have been evolved over a long period of time and they provide best
solutions to certain problems faced during software development. Learning these
patterns help unexperienced developers to learn software design in an easy and fast
way.
6
Types of Design Patterns
As per the design pattern reference book Design Patterns - Elements of Reusable
Object-Oriented Software, there are 23 design patterns which can be classified in
three categories: Creational, Structural and Behavioral patterns. We will also discuss
another category of design pattern: J2EE design patterns.
1 Creational Patterns
These design patterns provide a way to create objects while hiding the
creation logic, rather than instantiating objects directly using new
operator. This gives more flexibility to the program in deciding which
objects need to be created for a given use case.
2 Structural Patterns
These design patterns concern class and object composition. Concept of
inheritance is used to compose interfaces and define ways to compose
objects to obtain new functionalities.
3 Behavioral Patterns
These design patterns are specifically concerned with communication
between objects.
4 J2EE Patterns
These design patterns are specifically concerned with the presentation
tier. These patterns are identified by Sun Java Center.
7
FACTORY PATTERN
Factory pattern is one of most used design patterns in Java. This type of design
pattern comes under creational pattern as this pattern provides one of the best ways
to create an object.
In Factory pattern, we create objects without exposing the creation logic to the client
and refer to newly created object using a common interface.
Implementation
We are going to create a Shape interface and concrete classes implementing
the Shape interface. A factory class ShapeFactory is defined as a next step.
FactoryPatternDemo, our demo 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.
Shape.java
8
public interface Shape {
void draw();
}
Step 2
Create concrete classes implementing the same interface.
Rectangle.java
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Square.java
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Circle.java
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
9
}
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 an information such as
type.
FactoryPatternDemo.java
10
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
Step 5
Verify the output.
11
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com
12