Chap 11 - Design Pattern
Chap 11 - Design Pattern
www.ump.edu.my
What are Patterns?
“Each pattern describes a problem which occurs over and over
again in our environment, and then describes the core of the
solution to that problem in such a way that you can use this
solution a million times over, without ever doing it the same way
twice”
www.ump.edu.my
History: Design Pattern
• In 1994, Gang of Four (GoF): Erich Gamma, Richard Helm,
Ralph Johnson and John Vlissides, published a book on
software pattern – founders of the movement
• Gamma et al, Design Patterns: Elements of Reusable Object-
Oriented Software, Addison Wesley, 1994.
• These authors are collectively known as GoF . According to
them design patterns are primarily based on the following
principal of object orientated design.
www.ump.edu.my
OOP: Design Pattern
• In software engineering, a design pattern is a general
repeatable solution to a commonly occurring problem in
software design.
• It is a description or template for how to solve a problem that
can be used in many difference situation.
• A design pattern is not a finished design that can be
transformed directly into code
• Patterns record successful solutions in software development
for sharing between projects.
www.ump.edu.my
Why Design Pattern?
• Reuse – proven solutions
• Provides a head start
• Avoids error – that has occurred previously
• No need to reinvent the wheel
www.ump.edu.my
Other Advantages
• Most design patterns make software more modifiable, robust
• using well tested solutions.
• Using design patterns makes software systems easier to
change
• more maintainable.
• Helps increase the understanding of basic object-oriented design
principles
• encapsulation, inheritance, interfaces, polymorphism
www.ump.edu.my
Three Types of Pattern
• Creational patterns:
• Deal with initializing and configuring classes and objects
• Structural patterns:
• Deal with decoupling interface and implementation of classes and
objects
• Composition of classes or objects
• Behavioral patterns:
• Deal with dynamic interactions among societies of classes and
objects
• How they distribute responsibility
www.ump.edu.my
Creational Patterns
• Factory Method:
• method in a derived class creates associations
• Abstract Factory:
• Factory for building related objects
• Builder:
• Factory for building complex objects incrementally
• Prototype:
• Factory for cloning new instances from a prototype
• Singleton:
• Factory for a singular (sole) instance
www.ump.edu.my
Structural Patterns
• Adapter: • Facade:
• Translator adapts a server interface for a • simplifies the interface for a subsystem
client
• Flyweight:
• Bridge: • many fine-grained objects shared
• Abstraction for binding one of many efficiently.
implementations
• Proxy:
• Composite: • one object approximates another
• Structure for building recursive
aggregations
• Decorator:
• Decorator extends an object
transparently
www.ump.edu.my
Behavioral Patterns
• Chain of Responsibility • Mediator:
• request delegated to the responsible • coordinates interactions between its
service provider associates
• Command: • Memento:
• request is first-class object • snapshot captures and restores object
states privately
• Iterator:
• Aggregate elements are accessed
• Observer:
sequentially • dependents update automatically when
subject changes
• Interpreter:
• language interpreter for a small
• State:
grammar • object whose behavior depends on its state
www.ump.edu.my
Factory Method
• Factory design pattern is one of the most used design patterns
in Java. It fall under creational pattern as this pattern provides
one of the best ways to create an object.
• Factory design pattern is used when we have a super class
with multiple sub-classes and based on input, we need to
return one of the sub-class. This pattern take out the
responsibility of instantiation of a class from client program to
the factory class.
www.ump.edu.my
Factory Method (Cont.)
• A superclass or (interface or abstract class) specifies all
standard and generic behavior and then delegates the
creation details to subclasses.
• Factory Method makes a design more customizable and only a
little more complicated.
• It’s called a Factory because it creates various types of objects
without necessarily knowing what kind of object it creates or
how to create it.
www.ump.edu.my
Class diagram - inheritance
<<interface>>
Vehicle
+ calculateRoadTax(cc: int)
www.ump.edu.my
Polymorphism Implementation
public static void main(String[] args) {
Vehicle bic = new Bicycle();//polymorphism
Vehicle car = new Car();
Vehicle suv = new SUV();
}
Output: .Bicycle road tax is RM 0
Bicycle road tax is RM 1440
Bicycle road tax is RM 3000
www.ump.edu.my
Class diagram – Factory pattern VehicleFactory
<<interface>> + getRoadTax()
Vehicle
+ calculateRoadTax() VehicleTest
+ main(arg[]:String): void
www.ump.edu.my
Super Class
• Super class in factory design pattern can be an interface or
abstract class. For our factory design pattern example, we
have Interface super class with overridden
calculateRoadTax() method for testing purpose.
www.ump.edu.my
Concrete Class
• Concrete class or sub class in factory design pattern works
similar to normal Java class, it will override the super class
method and provide an implementation.
• Bicycle, Car and SUV class will complete the implementation
for calculateRoadTax()method.
public class Bicycle implements Vehicle{
public double calculateRoadTax(int cc){
return cc * 0;
}
}
www.ump.edu.my
Concrete Class
public class Car implements Vehicle{
public double calculateRoadTax(int cc){
return cc * .9;
}
}
public class SUV implements Vehicle{
public double calculateRoadTax(int cc){
return cc * 1.5;
}
}
www.ump.edu.my
Factory Class & Factory Method()
• In the Factory Class, define a Factory Method() that
return the interface or super class type, to create concrete
implementations of the interface class.
• The Factory Method() defines an interface for creating
objects (polymorphism), based on input parameter,
difference subclass or concrete class is instantiate and
return.
www.ump.edu.my
public class VehicleFactory { //factory class
public Vehicle getRoadtax(String type){ //factory method
if (type.toLowerCase().equals("car")){//input parameter
obj = new Car(); //polymorphism
}
else if (type.toLowerCase().equals("suv")){
obj = new SUV();
}
else if (type.toLowerCase().equals("bicycle")){
obj = new Bicycle();
}
else {
System.out.println(“Invalid Input!!");
}
return obj; //return selected concrete object
}
www.ump.edu.my
Creator: Main Class of Factory Design
Pattern
• The main() method is located and be the creator of factory
pattern.
• Declare the Factory Object (from the Factory Class) and use
the Factory Method() to get the object of concrete class
– that return from the Factory Method(), by passing the
input parameter.
• Override method will display the expected output
www.ump.edu.my
public class TestFactory {
public static void main(String[] args){
Scanner userInput = new Scanner(System.in);
if (obj != null)
System.out.println(vehicleType + " road tax is RM " +
obj.calculateRoadTax(cc)); //print output form the override method
}
}
www.ump.edu.my
When to Use?
• When we need to easily generate different instances of objects
depending on the environment we are in.
• When we’re working with many small objects or components
that share the same properties.
www.ump.edu.my