0% found this document useful (0 votes)
131 views

OOSE Lab 9

This document describes an experiment on design patterns for a software engineering class. The objectives are to learn what design patterns are, how to utilize design patterns to solve problems, and how to create objects using design patterns. Design patterns represent best practices used by experienced developers. The document provides examples of the factory pattern and abstract factory pattern to illustrate how to implement design patterns.

Uploaded by

Fahim Rocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

OOSE Lab 9

This document describes an experiment on design patterns for a software engineering class. The objectives are to learn what design patterns are, how to utilize design patterns to solve problems, and how to create objects using design patterns. Design patterns represent best practices used by experienced developers. The document provides examples of the factory pattern and abstract factory pattern to illustrate how to implement design patterns.

Uploaded by

Fahim Rocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

OBJECT ORIENTED SOFTWARE


ENGINEERING

Experiment 09
Design Patterns - Elements of Reusable Object-
Oriented Software

CLO 2: Applying Static modeling, and Dynamic modeling techniques to


define system boundary, to solve real world problems.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Purpose:

This experiment provides to Introduction to “Design Patterns” which Design patterns


represent the best practices used by experienced object-oriented software developers.
Students will learn the building blocks of a Design Patterns using Java IDE.

Objectives:

At the end of this experiment students will

1. Lean what the Design Patterns are.

2. Utilization of Design Patterns for solving different

3. Creation of objects using Design Patterns.

Equipment and Components:

1. Eclipse Helion
2. Net Beans

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

What is a Design Pattern?


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.

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.

 Program to an interface not an implementation


 Favor object composition over inheritance

Usage of Design Pattern

Design Patterns have two main usages in software development.

1. Common platform for developers


Design patterns provide a standard terminology and are specific to particular
scenario. For example, a singleton design pattern signifies 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.

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.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Types of Design Patterns


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'll also discuss another category of
design pattern: J2EE design patterns.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Design Pattern - Factory Pattern

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.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Step 1

Create an interface.

Shape.java

public interface Shape {


void draw();
}

Step 2

Create concrete classes implementing the same interface.

Rectangle.java

public class Rectangle implements Shape {

//@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}

Square.java

public class Square implements Shape {

//@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}

Circle.java

public class Circle implements Shape {

// @Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Step 3

Create a Factory Class named” ShapeFactory” to generate object of concrete class based on given
information.

ShapeFactory.java

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 by demo
class.

FactoryPatternDemo.java

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");

//call draw method of Circle


shape1.draw();

//get an object of Rectangle and call its draw method.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Shape shape2 = shapeFactory.getShape("RECTANGLE");

//call draw method of Rectangle


shape2.draw();

//get an object of Square and call its draw method.


Shape shape3 = shapeFactory.getShape("SQUARE");

//call draw method of square


shape3.draw();
}
}

Step 5

Verify the output.

Inside Circle::draw() method.


Inside Rectangle::draw() method.
Inside Square::draw() method.

Design Pattern - Abstract Factory Pattern


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.

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.

AbstractFactoryPatternDemo, our demo class uses FactoryProducer to get a


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.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Step 1

Create an interface for Shapes.

Shape.java

public interface Shape {


void draw();
}
Step 2

Create concrete classes implementing the same interface.

Rectangle.java

public class Rectangle implements Shape {

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}

Square.java

public class Square implements Shape {

@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}

Circle.java

public class Circle implements Shape {

@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Step 3

Create an interface for Colors.

Color.java

public interface Color {


void fill();
}
Step4

Create concrete classes implementing the same interface.

Red.java

public class Red implements Color {

@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

SOFTWARE ENGINEERING DEPARTMENT

Green.java

public class Green implements Color {

@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}

Blue.java

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.

AbstractFactory.java

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.

ShapeFactory.java

public class ShapeFactory extends AbstractFactory {

@Override
public Shape getShape(String shapeType){

if(shapeType == null){
return null;
}

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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

public class ColorFactory extends AbstractFactory {

@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

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Create a Factory generator/producer class to get factories by passing an information such as


Shape or Color

FactoryProducer.java

public class FactoryProducer {


public static AbstractFactory getFactory(String choice){

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

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();

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

//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

Design Patterns - Strategy Pattern


In Strategy pattern, a class behavior or its algorithm can be changed at run time. This
type of design pattern comes under behavior pattern. In Strategy pattern, we create
objects which represent various strategies and a context object whose behavior varies as
per its strategy object. The strategy object changes the executing algorithm of the
context object.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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

public interface Strategy {


public int doOperation(int num1, int num2);
}
Step 2

Create concrete classes implementing the same interface.

OperationAdd.java

public class OperationAdd implements Strategy{


Object Oriented Software Engineering 5 th Semester-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}

OperationSubstract.java

public class OperationSubstract implements Strategy{


@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}

OperationMultiply.java

public class OperationMultiply implements Strategy{


@Override
public int doOperation(int num1, int num2) {
return num1 * num2;
}
}
Step 3

Create Context Class.

Context.java

public class Context {


private Strategy strategy;

public Context(Strategy strategy){


this.strategy = strategy;
}

public int executeStrategy(int num1, int num2){


return strategy.doOperation(num1, num2);
}
}
Step 4

Use the Context to see change in behaviour when it changes its Strategy.

StrategyPatternDemo.java

public class StrategyPatternDemo {


Object Oriented Software Engineering 5 th Semester-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

public static void main(String[] args) {


Context context = new Context(new OperationAdd());
System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

context = new Context(new OperationSubstract());


System.out.println("10 - 5 = " + context.executeStrategy(10, 5));

context = new Context(new OperationMultiply());


System.out.println("10 * 5 = " + context.executeStrategy(10, 5));
}
}
Step 5

Verify the output.

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.

Object Oriented Software Engineering 5 th Semester-SE UET Taxila

You might also like