Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By Srinivas
Intended Audience
This guide is particularly intended for the developers who want to gain knowledge and
insight into Design Patterns - Creational Patterns. The developers or audience must be
having knowledge of the Core Java.
Revision History
This document is the first version of document which is going to cover regarding the
topics of Design Patterns - Creational Patterns.
These solutions have been obtained by trial and error by various software developers for
quite a substantial period of time.In software engineering, we can say that a design
pattern can be said as a general repeatable solution for any normally occurring problem
in terms of software design.
A design pattern cannot be said as a finished design which can be transformed directly
into some code. This can be said as a description or template for how solving any
problem which can be used in various scenarios.
These authors have been called as Gang of Four (GOF) from then. According to these
authors, design patterns have been primarily based on the below principles of object
orientated design.
We have to Program to an interface and not for an implementation.
We have to favor object composition instead of inheritance.
Reusing design patterns may help in preventing subtle issues which can be causing
major problems and may improve code readability for architects and coders who are
familiar with patterns.
Normally, people can only understand how to apply some software design techniques
for some type of problems. These techniques are often very difficult when we are
applying them to some broad range of problems.
Design patterns can provide us some general solutions, which have been documented in
a format that doesnt require any specifics which are tied to particular problem.
Besides that, patterns can allow developers in communicating with well-known, and
names for software interactions. The design patterns which are common can be
improved over time, which makes them robust than the ad-hoc designs.
Best Practices
Design patterns have been evolving over a long time and they can provide us best
solutions for certain problems which have been faced during development of software. If
developers can learn these patterns, it can help them for learning software design in an
faster and easy way.
Creational Patterns
With these design patterns, we can get a way for creating objects while we are hiding
creation logic, and we are rather instantiating objects directly by the usage of new
operator. With this, we give program so much flexibility, in deciding which objects have
to be created for any given use case.
Structural Patterns
These design patterns are concerned with object and class composition. Concept of
inheritance have been used for composing interfaces and defining ways for composing
objects and obtain new functionalities.
Behavioral Patterns
With these design patterns, we are concerned with communication between the objects.
Singleton pattern can be said as one of the simplest design patterns in Java. This
singleton design pattern can be said as a design pattern, which comes under creational
pattern as this pattern is providing us with one of best ways for creating an object.
This pattern gives us with a single class, which is responsible for creating an object while
making sure, that only single object is getting created. This class gives us a way for
accessing its only object, and this can be accessed directly without any need for
instantiating the object of class.
Implementation Steps
Here, We're going to create some SingleObject class. SingleObject class can have a static
instance of itself and its constructor have been set as private.
With SingleObject class, we get a static method for getting its static instance to outside
world. Our demo class SingletonPatternDemo, will use the SingleObject class for getting
a SingleObject object.
Step 3
The output.
Hello World!
With Builder pattern, we can build a complex object with the usage of simple objects
and we can follow a step by step approach for achieving this. This builder pattern comes
under creational pattern, as this pattern is providing with one of best ways for creating
an object.
A Builder class can build the final object on step by step basis. This builder is
independent of any other objects.
Implementation Steps
For this, We have to consider a business case of fast-food restaurant, where any typical
meal could be a cold drink and burger. Burger could be a Chicken Burger or Veg Burger
and it will be packed in some wrapper. Pepsi or coke could be a Cold drink and it will be
packed in a bottle.
Now, We will create an Item interface, which is going to represent food items such as
cold drinks and burgers, and concrete classes which implement the Item interface and a
Packing interface which represents the food items packaging and concrete classes
implementing the Packing interface as burger and they would be packed in wrapper and
cold drink which would be packed as bottle.
We then create a Meal class which has a ArrayList of Item and a MealBuilder which
builds different types of Meal objects which happens by combining Item.
BuilderPatternDemo, our demo class uses MealBuilder for building a Meal.
Item.java
public interface Item {
public Packing packing();
public String name();
public float price();
}
Packing.java
public interface Packing {
public String pack();
}
Step 2
Now, Create concrete classes and they are implementing Packing interface.
Wrapper.java
public class Wrapper implements Packing {
@Override
public String pack() {
return "Wrapper";
}
}
Bottle.java
public class Bottle implements Packing {
@Override
public String pack() {
return "Bottle";
}
}
Step 3
After that, Create abstract classes that are implementing item interface, and they are
providing you with default functionalities.
@Override
public abstract float price();
@Override
public Packing packing() {
return new Wrapper();
}
}
ColdDrink.java
public abstract class ColdDrink implements Item {
@Override
public abstract float price();
@Override
public Packing packing() {
return new Bottle();
}
}
Step 4
After them, Create concrete classes extending the ColdDrink and Burger classes
VegBurger.java
ChickenBurger.java
@Override
public String name() {
return "Chicken Burger";
}
@Override
public float price() {
return 50.5f;
}
}
Coke.java
public class Coke extends ColdDrink {
@Override
public String name() {
return "Coke";
}
@Override
public float price() {
return 30.0f;
}
}
Pepsi.java
public class Pepsi extends ColdDrink {
@Override
public String name() {
return "Pepsi";
}
@Override
public float price() {
return 35.0f;
}
}
Step 5
Create a Meal class having Item objects defined above.
Meal.java
MealBuilder.java
}
}
Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5
Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0
With Prototype pattern, we are going to create a duplicate object and also keep the
performance in mind. This Prototype Pattern normally comes under creational pattern,
and this pattern gives us with one of relevant ways for creating any object.
For example, suppose an object should to be created, that to, after some costly database
operation. For that to happen, we can cache the object, and we can return its clone on
any next update and can also request the database as and whenever it is needed thus
reducing the database calls.
Implementation Steps
We're going to create an abstract class Shape and concrete classes extending the Shape
class. A class ShapeCache is defined as a next step which stores shape objects in a
Hashtable and returns their clone when requested.
PrototypPatternDemo, our demo class will use ShapeCache class to get a Shape object.
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
Step 2
Create concrete classes extending the above class.
Rectangle.java
public Rectangle(){
type = "Rectangle";
}
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public Square(){
type = "Square";
}
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Circle.java
public Circle(){
type = "Circle";
}
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Step 3
Create a class to get concrete classes from database and store them in a Hashtable.
ShapeCache.java
import java.util.Hashtable;
Step 4
PrototypePatternDemo uses ShapeCache class to get clones of shapes stored in a
Hashtable.
PrototypePatternDemo.java
Shape : Circle
Shape : Square
Shape : Rectangle
are related to Design Patterns - Creational Patterns. Here, we also have covered about