




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































i
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.
This tutorial will take you through step by step approach and examples using Java while learning Design Pattern concepts.
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.
Before you start proceeding with this tutorial, we make an assumption that you are already aware of the basic concepts of Java programming.
ļ£ Copyright 201 5 by Tutorials Point (I) Pvt. Ltd.
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 [email protected]
Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.
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 which initiated the concept of Design Pattern in Software development.
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.
ļ· Program to an interface not an implementation ļ· Favor object composition over inheritance
Design Patterns have two main usages in software development.
Common platform for developers Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies the use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.
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.
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.
S.N. Pattern & Description
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.
Step 2
Create concrete classes implementing the same interface.
public class Rectangle implements Shape {
@Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } }
public class Square implements Shape {
@Override public void draw() { System.out.println("Inside Square::draw() method."); } }
public class Circle implements Shape {
@Override public void draw() { System.out.println("Inside Circle::draw() method."); } }
Step 3
Create a Factory to generate object of concrete class based on given information.
public class ShapeFactory {
//use getShape method to get object of type shape public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }
Step 4
Use the Factory to get object of concrete class by passing an information such as type.
public class FactoryPatternDemo {
public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory();
//get an object of Circle and call its draw method. Shape shape1 = shapeFactory.getShape("CIRCLE");
Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
In Abstract Factory pattern, an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.
We are going to create a Shape and Color interfaces and concrete classes implementing these interfaces. We create an abstract factory class AbstractFactory as next step. Factory classes ShapeFactory and ColorFactory are defined where each factory extends AbstractFactory. A factory creator/generator class FactoryProducer is created.
AbstractFactoryPatternDemo, our demo class, uses FactoryProducer to get an AbstractFactory object. It will pass information (CIRCLE / RECTANGLE / SQUARE for Shape) to AbstractFactory to get the type of object it needs. It also passes information (RED / GREEN / BLUE for Color) to AbstractFactory to get the type of object it needs.
Step 1
Create an interface for Shapes.
public interface Shape { void draw(); }
Step 2
Create concrete classes implementing the same interface.
public class Rectangle implements Shape {
@Override public void draw() { System.out.println("Inside Rectangle::draw() method.");
public class Red implements Color {
@Override public void fill() { System.out.println("Inside Red::fill() method."); } }
public class Green implements Color {
@Override public void fill() { System.out.println("Inside Green::fill() method."); } }
public class Blue implements Color {
@Override public void fill() { System.out.println("Inside Blue::fill() method."); } }
Step 5
Create an Abstract class to get factories for Color and Shape Objects.
public abstract class AbstractFactory { abstract Color getColor(String color);
abstract Shape getShape(String shape) ; }
Step 6
Create Factory classes extending AbstractFactory to generate object of concrete class based on given information.
public class ShapeFactory extends AbstractFactory {
@Override public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; }
@Override Color getColor(String color) { return null; } }
return new ColorFactory(); } return null; } }
Step 8
Use the FactoryProducer to get AbstractFactory in order to get factories of concrete classes by passing an information such as type.
public class AbstractFactoryPatternDemo { public static void main(String[] args) {
//get shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//get an object of Shape Circle Shape shape1 = shapeFactory.getShape("CIRCLE");
//call draw method of Shape Circle shape1.draw();
//get an object of Shape Rectangle Shape shape2 = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle shape2.draw();
//get an object of Shape Square Shape shape3 = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square shape3.draw();
//get color factory
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//get an object of Color Red Color color1 = colorFactory.getColor("RED");
//call fill method of Red color1.fill();
//get an object of Color Green Color color2 = colorFactory.getColor("Green");
//call fill method of Green color2.fill();
//get an object of Color Blue Color color3 = colorFactory.getColor("BLUE");
//call fill method of Color Blue color3.fill(); } }
Step 9
Verify the output.
Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method. Inside Red::fill() method. Inside Green::fill() method. Inside Blue::fill() method.