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

Lecture 7 - Factory Method Pattern

Uploaded by

tajrianaltahrim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lecture 7 - Factory Method Pattern

Uploaded by

tajrianaltahrim
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 7

Kazi Rifat Ahmed


Lecturer
Department of Software Engineering
Daffodil International University
Factory Pattern
● What is Factory Pattern
● Principles of Factory pattern
● Where it is necessary
Outline ● Where it can be implemented
● Real life problem and its
necessary
Factory Method
What is Factory Patterns?
➔ A Factory Pattern or Factory Method Pattern is a part of Creational
Pattern.
➔ It says that just define an interface 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.
➔ The Factory Method Pattern is also known as Virtual Constructor.
Advantage of Factory Design Pattern
● Factory Method 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. That means the code
interacts solely with the resultant interface or abstract class, so that
it will work with any classes that implement that interface or that
extends that abstract class.
Usage of Factory Design Pattern
1. When a class doesn't know what sub-classes will be required to
create.
2. When a class wants that its sub-classes specify the objects to be
created.
3. When the parent classes choose the creation of objects to its
subclasses.
UML for Factory Pattern
Factory Pattern Example
Step 1
Create an interface.

Shape.java

public interface Shape {

void draw();

}
Factory Pattern Example
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.");

}
Factory Pattern Example
Step 2
Create concrete classes implementing the same interface.

Square.java

public class Square implements Shape {

@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Factory Pattern Example
Step 2
Create concrete classes implementing the same interface.

Circle.java

public class Circle implements Shape {

@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
Factory Pattern Example
Step 3
Create a Factory 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;
}
}
Factory Pattern Example
Step 4
Use the Factory to get object of concrete class by passing an information such as type.
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.
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(); }
}
References
1. https://www.tutorialspoint.com/design_pattern/factory_pattern.htm
2. https://www.tutorialspoint.com/design_pattern/factory_pattern.htm
3. https://refactoring.guru/design-patterns/factory-method
Thank You

You might also like