OOSE Lab 9
OOSE Lab 9
Experiment 09
Design Patterns - Elements of Reusable Object-
Oriented Software
Purpose:
Objectives:
1. Eclipse Helion
2. Net Beans
What is (GOF)?
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.
2. 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 helps unexperienced developers to learn software design in an easy
and faster way.
Factory pattern is one of the most used design patterns in Java. This type of design pattern
comes under creational pattern as this pattern provides one of the best ways to create an
object. In Factory pattern, we create object without exposing the creation logic to the client
and refer to newly created object using a common interface.
Implementation
We're going to create a Shape interface and concrete classes implementing the Shape
interface. A factory class ShapeFactory is defined as a next step.
FactoryPatternDemo, our demo class will use ShapeFactory to get a Shape object. It will
pass information (CIRCLE / RECTANGLE / SQUARE) to ShapeFactory to get the type of
object it needs.
Step 1
Create an interface.
Shape.java
Step 2
Rectangle.java
//@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Square.java
//@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Circle.java
// @Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Step 3
Create a Factory Class named” ShapeFactory” to generate object of concrete class based on given
information.
ShapeFactory.java
} 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 by demo
class.
FactoryPatternDemo.java
Step 5
Implementation
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.
Step 1
Shape.java
Rectangle.java
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Square.java
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Circle.java
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Step 3
Color.java
Red.java
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
Object Oriented Software Engineering 5 th Semester-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
Green.java
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
Blue.java
@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.
AbstractFactory.java
Create Factory classes extending AbstractFactory to generate object of concrete class based on
given information.
ShapeFactory.java
@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;
}
}
ColorFactory.java
@Override
public Shape getShape(String shapeType){
return null;
}
@Override
Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
}else if(color.equalsIgnoreCase("GREEN")){
return new Green();
}else if(color.equalsIgnoreCase("BLUE")){
return new Blue();
}
return null;
}
}
Step 7
FactoryProducer.java
if(choice.equalsIgnoreCase("SHAPE")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("COLOR")){
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.
AbstractFactoryPatternDemo.java
Implementation
We are going to create a Strategy interface defining an action and concrete strategy
classes implementing the Strategy interface. Context is a class which uses a Strategy.
StrategyPatternDemo, our demo class, will use Context and strategy objects to
demonstrate change in Context behaviour based on strategy it deploys or uses.
Step 1
Create an interface.
Strategy.java
OperationAdd.java
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
OperationSubstract.java
OperationMultiply.java
Context.java
Use the Context to see change in behaviour when it changes its Strategy.
StrategyPatternDemo.java
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
Task:
Define and Implement Command Pattern with example.
Assignment 2
Implement Abstract factory Pattern and Strategy Pattern in your Java Project in
any possible scenario.
Note: Assignment will be viva based and checked in both hard n soft form in coming
Lab.