We have prepared a presentation on design pattern. Here are 7 types of design pattern we have presented.
1. Mediator Design
2. Iterator Pattern
3. Bridge Pattern
4. Decorator Pattern
5. Composite Pattern
6. Template Pattern
7. Singleton Pattern
Here is the link of Canva Slide to use as template----
https://www.canva.com/design/DAEmun8Sjks/NmH08MOYrAWSaTERNbe5LA/view?utm_content=DAEmun8Sjks&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton&mode=preview
You can contact me in LinkedIn or any other social platform for more infromation.
Thank you.
2. What is Design Pattern?
A D E S I G N P A T T E R N I S A G E N E R A L R E U S A B L E
S O L U T I O N T O A C O M M O N L Y O C C U R R I N G P R O B L E M
I N S O F T W A R E D E S I G N .
5. The Mediator defines the interface for
communication between objects. This is a
behavioural pattern as it defines a manner for
controlling communication between classes or
entities. The mediator pattern is used to reduce
coupling between classes that communicate with
each other.
Brief Description:
7. Implementation
1. Mediator
i)This component defines an interface for
communicating with Colleague objects.
2. ConcreteMediator
i) This component implements cooperative
behavior by coordinating Colleague objects.
ii)It knows and maintains its colleagues.
3. Colleague classes
i) Each Colleague class knows its Mediator object.
ii) Each colleague communicates with its mediator whenever it
would have otherwise communicated with another colleague.
8. Code Example
public abstract Colleague{
private Mediator mediator;
public Colleague(Mediator m)
{
mediator = m;
}
Colleage interface
public void send(String
message) {
mediator.send(message,
this);
}
send a message via the mediator
//Mediator interface
public interface Mediator
{
public void send(String
message, Colleague
colleague);
}
Mediator interface
public Mediator
getMediator() {return
mediator;}
public abstract void
receive(String message);
}
get access to the mediator
public class ApplicationMediator implements Mediator {
private ArrayList<Colleague> colleagues;
public ApplicationMediator() {
colleagues = new ArrayList<Colleague>();
}
public void addColleague(Colleague colleague) {
colleagues.add(colleague);
}
public void send(String message, Colleague originator) {
//let all other screens know that this screen has changed
for(Colleague colleague: colleagues) {
//don't tell ourselves
if(colleague != originator) {
colleage.receive(message);
}
}
}
}
9. Code Example
public class HeadDepartment
implements Department {
public class ConcreteColleague
extends Colleague {
public void receive(String
message) {
System.out.println("Colleague
Received: " + message);
}
}
public class MobileColleague extends
Colleague {
public void receive(String message) {
System.out.println("Mobile Received:
" + message);
}
} public class Client {
public static void main(String[] args) {
ApplicationMediator mediator = new
ApplicationMediator();
ConcreteColleague desktop = new
ConcreteColleague(mediator);
ConcreteColleague mobile = new
MobileColleague(mediator);
mediator.addColleague(desktop);
mediator.addColleague(mobile);
desktop.send("Hello World");
mobile.send("Hello");
}
}
10. PROS AND CONS
Its one of the main advantage is that we can
replace one object in the implementation
with a different other object without
affecting the classes and the interfaces.
Mediator Design Pattern is very simple to
understand and implement in real life
examples such as chatRoom, Facebook, Air
traffic control, Road traffic control etc.
Disadvantage of Mediator pattern is that it
centralises the communications between
various components and thus the mediator
pattern deals complexity of interaction for
complexity in the mediator. Because a
mediator hides protocols, it can become
more and more complex than any
individual colleague. This can make the
mediator itself a large block that is hard to
maintain.
13. Iterator is a behavioral design pattern that lets you
traverse elements of a collection without exposing
its underlying representation (list, stack, tree, etc.).
Brief Description:
14. Declare the iterator interface.
Implement concrete iterator
classes for the collections that you
want to be traversable with
iterators.
Implement the collection interface
in your collection classes.
Step 1
Declare the collection interface and
describe a method for fetching
iterators.
Step 2
Step 3
Step 4
Implementation
Step 5
Go over the client code to replace all
of the collection traversal code with
the use of iterators.
16. Code Example
public interface Iterator
{
boolean hasNext();
String Next();
}
public interface TV {
Iterator getIterator();
}
public class ConcreteIterator implements Iterator{
int index=0;
@Override
public boolean hasNext() {
if (index < channels.length)
return true;
return false;
}
@Override
public String Next() {
if(hasNext()) {
return channels[index++];
}
return null;
}
public class ConcreteTV implements TV{
private String[] channels = {"NTV", "GTV", "GB"};
private Iterator iterator;
public ConcreteTV() {
iterator= new ConcreteIterator();
}
@Override
public Iterator getIterator() {
return iterator;
}
Iterator.java TV.java
ConcreteIterator ConcreteTV.java
17. Code Example
public class Demo {
public static void main(String[] args) {
TV tv = new ConcreteTV();
Iterator iterator = tv.getIterator();
while (iterator.hasNext()) {
String channel = iterator.Next();
System.out.println("TV is Playing "+channel);
}
}
}
Demo.java
18. PROS AND CONS
We can iterate over the same collection in
parallel because each iterator object
contains its own iteration state.
Open/Closed Principle.
Single Responsibility Principle.
Using an iterator may be less efficient than
going through elements of some
specialized collections directly.
Applying the pattern can be an overkill if
your app only works with simple
collections.
19. Examples where
Iterate pattern is used
TV Remote
A good example of an Iterator is
a TV remote, which has the “next”
and “previous” buttons to surf TV
channels.
Media Players
In media players, we have a list of songs
listed and we can play the song by
traversing to the songs list and select
desired song. It’s also an iterator example.
21. Bridge is a structural design pattern that lets
you split a large class or a set of closely related
classes into two separate hierarchies—
abstraction and implementation—which can be
developed independently of each other.
Brief Description:
22. first what operations the client needs and define them in the base abstraction
class.
Declare the ones that the abstraction needs in the general implementation
interface.
For all platforms in the domain create concrete implementation classes, but make
sure they all follow the implementation interface.
Inside the abstraction class, add a reference field for the implementation type.
If there have several variants of high-level logic, create refined abstractions for
each variant by extending the base abstraction class.
The client code should pass an implementation object to the abstraction’s
constructor to associate one with the other.
Implementation
24. Code Example with any language with Diagram :
Color.java
public interface Color {
String fill();
}
Implementcolor.java
public class Blue implements Color {
@Override
public String fill() {
return "Color is Blue";
}
}
25. Shape.java
public abstract class Shape {
protected Color color;
abstract public String draw();
}
extendShape.java
public class Square extends Shape {
public Square(Color color) {
super(color);
}
@Override
public String draw() {
return "Square drawn. " + color.fill();
}
}
test.java
@Test
public void whenBridgePatternInvoked_thenConfigSuccess()
{
Shape square = new Square(new Red());
assertEquals(square.draw(), "Square drawn. Color is Red");
}
Code Steps :
26. Pros and Cons of the pattern :
It can create platform-
independent classes and apps.
The client code works with high-
level abstractions. It isn’t exposed
to the platform details.
It can introduce new abstractions
and implementations
independently from each other.
Pros :
it might make the code more
complicated by applying the
pattern to a highly cohesive
class.
Cons :
27. Examples where Iterate
pattern is used:
For Retail shop owner
For software developer
If you are a software
developer and if you are
using some software for
writing some data, you could
just call an API for doing the
job.
If you are a retail shop owner and you send
a message to the customers regarding the
new products whenever they arrive at the
shop. So, you need to intimate the
customers via SMS. You will use some
software for doing that job. You click on
send the message with the help of that
software.
29. Decorator is a design pattern that allows you to
attach additional behaviors to objects by enclosing
them in special wrapper objects that contain the
behaviors.
Brief Description:
30. IMPLEMANTION
WITH (DIAGRAM)
Ascertain that your company's domain may be represented
as a major component with several optional layers layered
on top of it.
AAI | Pro X
Figure out what methods are common to both the primary
component and the optional layers. Create a component
interface and declare those methods there.
Create a concrete component class in which to define the
base behavior.
Make a decorator base class. It should provide a field for
storing a wrapped object reference.
Ascertain that all classes adhere to the component
interface.
Create concrete decorators by extending them from the
base decorator.
The client code must be responsible for creating decorators
and composing them in the way the client needs.
31. W E ' L L M A K E A S H A P E I N T E R F A C E A S W E L L A S C O N C R E T E
C L A S S E S T H A T I M P L E M E N T T H E S H A P E I N T E R F A C E .
A F T E R T H A T , W E ' L L M A K E A N A B S T R A C T D E C O R A T O R C L A S S
C A L L E D S H A P E D E C O R A T O R T H A T I M P L E M E N T S T H E S H A P E
I N T E R F A C E A N D H A S A S H A P E O B J E C T A S A N I N S T A N C E
V A R I A B L E .
R E D S H A P E D E C O R A T O R I S A C O N C R E T E C L A S S I M P L E M E N T I N G
S H A P E D E C O R A T O R .
D E C O R A T O R P A T T E R N D E M O , O U R D E M O C L A S S W I L L U S E
R E D S H A P E D E C O R A T O R T O D E C O R A T E S H A P E O B J E C T S .
CODE EXAMPLE
WITH DIAGRAM
32. Shape.java
public interface Shape {
void draw();
}
Step 1
Step 2 Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Rectangle");
}
}
Step 2 Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Shape: Circle");
}
}
Step 3 ShapeDecorator.java
public abstract class ShapeDecorator implements Shape
{
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){
decoratedShape.draw();
}
}
33. Step 4 RedShapeDecorator.java
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
}
Step 5 DecoratorPatternDemo.java
public class DecoratorPatternDemo {
public static void main(String[] args) {
Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(new Circle());
Shape redRectangle = new RedShapeDecorator(new Rectangle());
System.out.println("Circle with normal border");
circle.draw();
System.out.println("nCircle of red border");
redCircle.draw();
System.out.println("nRectangle of red border");
redRectangle.draw();
}
}
34. PROS AND CONS
You can change the behavior of an
object without creating a new
subclass.
At runtime, you can add or remove
responsibilities from an object.
Wrapping an object in numerous
decorators allows you to mix multiple
behaviors.
Principle of a single point of
responsibility. A monolithic class that
implements several possible behavior
variants can be broken down into
several smaller classes.
Pros :
It's difficult to remove a
specific wrapper from the
stack of wrappers.
It's difficult to create a
decorator whose behavior
is independent of the
sequence in which the
decorators are stacked.
The initial configuration
code of layers might look
pretty ugly.
Cons :
35. A N A S S A U L T G U N I S A D E A D L Y W E A P O N O N I T S O W N . B U T
Y O U C A N A P P L Y C E R T A I N " D E C O R A T I O N S " T O M A K E I T
M O R E A C C U R A T E , S I L E N T A N D D E V A S T A T I N G .
T H E D E C O R A T O R A T T A C H E S A D D I T I O N A L
R E S P O N S I B I L I T I E S T O A N O B J E C T D Y N A M I C A L L Y . T H E
O R N A M E N T S T H A T A R E A D D E D T O P I N E O R F I R T R E E S A R E
E X A M P L E S O F D E C O R A T O R S . L I G H T S , G A R L A N D , C A N D Y
C A N E S , G L A S S O R N A M E N T S , E T C . , C A N B E A D D E D T O A
T R E E T O G I V E I T A F E S T I V E L O O K . T H E O R N A M E N T S D O
N O T C H A N G E T H E T R E E I T S E L F W H I C H I S R E C O G N I Z A B L E
A S A C H R I S T M A S T R E E R E G A R D L E S S O F P A R T I C U L A R
O R N A M E N T S U S E D . A S A N E X A M P L E O F A D D I T I O N A L
F U N C T I O N A L I T Y , T H E A D D I T I O N O F L I G H T S A L L O W S O N E
T O " L I G H T U P " A C H R I S T M A S T R E E .
Examples where
Decorator pattern is used
37. Composite is a structural design pattern that
lets you compose objects into tree structures
and then work with these structures as if they
were individual objects.
Brief Description:
38. Create a leaf class to represent
simple elements. A program may
have multiple different leaf classes.
Create a container class to represent
complex elements. In this class, provide
an array field for storing references to
sub-elements.
Step 1
Make sure that the core model of
your app can be represented as a
tree structure.
Step 2
Step 3
Step 4
Implementation
Step 5
Finally, define the methods for adding
and removal of child elements in the
container.
Implement concrete iterator classes
for the collections that you want to
be traversable with iterators.
40. Code Example
public class FinancialDepartment implements Department
{
private Integer id;
private String name;
public void printDepartmentName() {
System.out.println(getid().getSimpleName());
}
// standard constructor, getters, setters
}
public interface
Department {
void
printDepartmentName();
}
Department
FinancialDepartment
public class SalesDepartment implements Department {
private Integer id;
private String name;
public void printDepartmentName() {
System.out.println(getid().getSimpleName());
}
// standard constructor, getters, setters
}
SalesDepartment
41. Code Example
public class HeadDepartment implements Department {
private Integer id;
private String name;
private List<Department> childDepartments;
public HeadDepartment(Integer id, String name) {
this.id = id;
this.name = name;
this.childDepartments = new ArrayList<>();
}
public void printDepartmentName() {
childDepartments.forEach(Department::printDepartmentName);
}
public void addDepartment(Department department) {
childDepartments.add(department);
}
public void removeDepartment(Department department) {
childDepartments.remove(department);
}
}
public class CompositeDemo {
public static void main(String args[]) {
Department salesDepartment = new SalesDepartment(
1, "Sales department");
Department financialDepartment = new FinancialDepartment(
2, "Financial department");
HeadDepartment headDepartment = new HeadDepartment(
3, "Head department");
headDepartment.addDepartment(salesDepartment);
headDepartment.addDepartment(financialDepartment);
headDepartment.printDepartmentName();
}
}
}
HeadDepartment
CompositeDemo
42. PROS AND CONS
Open/Closed Principle. You can introduce
new element types into the app without
breaking the existing code, which now works
with the object tree.
You can work with complex tree structures
more conveniently: use polymorphism and
recursion to your advantage.
It might be difficult to provide a common
interface for classes whose functionality
differs too much. In certain scenarios,
you’d need to overgeneralize the
component interface, making it harder to
comprehend.
45. The Template Method pattern provides a method in a super-class,
usually an abstract super-class, and defines the skeleton of an
operation in terms of several high-level steps.
Generally, these steps are implemented by additional helper methods
in the same class as the template method.
Brief Description:
46. Implementation :
We are going to create a Game abstract class defining
operations with a template method set to be final so that it
cannot be overridden. Cricket and Football are concrete classes
that extend Game and override its methods.
TemplatePatternDemo, our demo class, will use Game to
demonstrate use of template pattern.
48. Code :
Step 1
Create an abstract class with a
template method being final.
Game.java
public abstract class Game {
abstract void initialize();
abstract void startPlay();
abstract void endPlay();
//template method
public final void play(){
//initialize the game
initialize();
//start game
startPlay();
//end game
endPlay();
}
}
49. Step 2
Create concrete classes extending the above class.
Cricket.java
public class Cricket extends Game {
@Override
void endPlay() {
System.out.println("Cricket Game Finished!");
}
@Override
void initialize() {
System.out.println("Cricket Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Cricket Game Started. Enjoy the game!");
}
}
Football.java
public class Football extends Game {
@Override
void endPlay() {
System.out.println("Football Game Finished!");
}
@Override
void initialize() {
System.out.println("Football Game Initialized! Start
playing.");
}
@Override
void startPlay() {
System.out.println("Football Game Started. Enjoy the
game!");
}
}
50. Step 3
Use the Game's template method play() to demonstrate a defined way of
playing game.
TemplatePatternDemo.java
public class TemplatePatternDemo {
public static void main(String[] args) {
Game game = new Cricket();
game.play();
System.out.println();
game = new Football();
game.play();
}
}
51. Step 4
Verify the output.
Cricket Game Initialized! Start playing.
Cricket Game Started. Enjoy the game!
Cricket Game Finished!
Football Game Initialized! Start playing.
Football Game Started. Enjoy the game!
Football Game Finished!
52. PROS AND CONS
Pros:
1. Easy to Implement
and readable
2. Flexible.
3. Clear Architecture
Cons:
1. Enforce a particular
Design
2. Maintenance Issue
55. Singleton pattern is one of the simpleest design
patterns in java. This type of design pattern comes
under creational pattern as the pattern provides
one of the best ways to creat an object
Brief Description:
60. Pros and coins
You can be sure that a class has only a
single instance.
You gain a global access point to
that instance.
The singleton object is initialized only
when it’s requested for the first time.
Violates the Single Responsibility
Principle. The pattern solves two
problems at the time.
The Singleton pattern can mask
bad design, for instance, when the
components of the program know
too much about each other.
The pattern requires special
treatment in a multithreaded
environment so that multiple threads
won’t create a singleton object
several times.
61. Singleton pattern used
It is used where only a single instance of a class is
required to control the action throughout the execution
Singleton Pattern Used in Java Stand Library
1.
Singleton Pattern Used in driver objects
2.
62. Md. Shahin Alam
ID:201-15-3186
Ahmed Ishtiak Nihal
ID:201-15-3277
Yousuf Rayhan Emom
ID:201-15-3220
MD Saimur Rahman Swarnab
ID:201-15-3303
Team member