Module 10 DesginPattersIntro L1 L4
Module 10 DesginPattersIntro L1 L4
Later, the Gang of Four - Design patterns, elements of reusable object-oriented software book
was written by a group of four persons named as Erich Gamma, Richard Helm, Ralph Johnson
and John Vlissides in 1995.
That's why all the above 23 Design Patterns are known as Gang of Four (GoF) Design Patterns.
As an Object Oriented developer, we may think that our code contains all the benefits provided by
the Object Oriented language.
The code we have written is flexible enough that we can make any changes to it easily.
Our code is re-usable so that we can re-use it anywhere without any trouble.
We can maintain our code easily and any changes to a part of the code will not affect any other
part of the code.
well-proved and testified solutions since they have been built upon the knowledge and
experience of expert software developers.
provide clarity to the system architecture and the possibility of building a better system.
Creational design patterns are concerned with the way of creating objects.
These design patterns are used when a decision must be made at the time of
instantiation of a class (i.e. creating an object of a class).
1. Singleton Pattern
The singleton pattern restricts the instantiation of a Class and ensures that
only one instance of the class exists in the JVM.
2. Factory Pattern
• The factory design pattern is used when, we have a superclass with multiple
subclasses and based on input, we need to return one of the subclasses.
• This pattern takes out the responsibility of the instantiation of a Class from the
client program to the factory class. We can apply a singleton pattern on the
factory class or make the factory method static.
BITS Pilani, Pilani Campus
Factory Pattern
• A Factory Pattern says that just define an interface or abstract class for
creating an object, but let the subclasses decide which class to
instantiate. In other words, subclasses are responsible to create the instance
of the class.
• Factory Pattern allows the sub-classes to choose the type of objects to create.
• It promotes the loose-coupling by eliminating the need to bind application-
specific classes into the code.
• The code interacts solely with the resultant interface or abstract class, so that
it will work with any classes that implement that interface or extends the
abstract class.
//3 Create an Abstract class to get factories for both Shape Objects.
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
BITS Pilani, Pilani Campus
Contd …
// 4 Create Factory classes extending AbstractFactory to generate object of concrete class
based on given information.
class Director
{Builder builder;
// Director knows how to use the builder and the sequence of steps.
public void construct(Builder builder)
{
this.builder = builder;
builder.startUpOperations();
builder.buildBody();
builder.insertWheels();
builder.addHeadlights();
builder.endOperations();
}
}
BITS Pilani, Pilani Campus
public class BuilderPatternExample {
public static void main(String[ ] args) {
System.out.println("***Builder Pattern Demo***");
Director director = new Director();
Builder fordCar = new Car("Ford");
Builder hondaMotorycle = new MotorCycle("Honda");
director.construct(fordCar);
Product p1 = fordCar.getVehicle();
p1.showProduct();
director.construct(hondaMotorycle ); //Making MotorCycle
Product p2 = hondaMotorycle.getVehicle();
p2.showProduct();
}}
BITS Pilani, Pilani Campus
Example
• The adapter translates that request on the adaptee using the adaptee interface.
• Client receive the results of the call and is unaware of adapter’s presence.
• Client requests to the adapter by calling a method on it using target interface.
//2 Create a Iphone implementation class that will implement Mobileshop interface.
public class Iphone implements MobileShop {
public void modelNo() {
System.out.println(" Iphone ");
} Similarly, create the classes for
public void price() { Samsung and Other Brands
System.out.println(" Rs 85000 ");
}
}
BITS Pilani, Pilani Campus
//3 Create a ShopKeeper concrete class that will use MobileShop interface.
public class ShopKeeper {
private MobileShop iphone;
private MobileShop samsung;
private MobileShop blackberry;
public ShopKeeper(){
iphone= new Iphone();
samsung=new Samsung();
blackberry=new Blackberry();
}
public void iphoneSale(){ iphone.modelNo(); iphone.price(); }
public void samsungSale(){ samsung.modelNo(); samsung.price(); }
public void blackberrySale(){ blackberry.modelNo(); blackberry.price(); }
}
Flyweight design pattern is used when we need to create a lot of Objects of a class.
Since every object consumes memory space that can be crucial for low memory
devices, such as mobile devices or embedded systems, flyweight design pattern
can be applied to reduce the load on memory by sharing objects.
Use sharing to support large numbers of fine-grained objects efficiently.
A flyweight object essentially has intrinsic and extrinsic attributes.
An intrinsic state attribute is stored/shared in the flyweight object, and it is
independent of flyweight’s context. We should make intrinsic states immutable.
An extrinsic state varies with flyweight’s context, which cannot be shared.
Client objects maintain the extrinsic state, and they need to pass this to a flyweight
object during object creation.
import java.util.Observable;
import java.util.Observer;
import java.util.Observable;
import java.util.Observer;
State pattern is to allow the object for changing its behavior without changing
its class.
System.out.println(context.getState().toString());
System.out.println(context.getState().toString());
}
}