SlideShare a Scribd company logo
http://www.tutorialspoint.com/design_pattern/factory_pattern.htm Copyright © tutorialspoint.com
DESIGN PATTERN - FACTORY PATTERNDESIGN PATTERN - FACTORY PATTERN
Factory pattern is one of most used design pattern 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
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.");
}
}
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;
}
}
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 circle
shape3.draw();
}
}
Step 5
Verify the output.
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.

More Related Content

PPTX
6. static keyword
DOCX
Abstraction in c++ and Real Life Example of Abstraction in C++
PPTX
class and objects
PPTX
Methods in java
PPTX
Inheritance and Interfaces
PPTX
Inheritance in c++
PPT
Final keyword in java
PPTX
This pointer
6. static keyword
Abstraction in c++ and Real Life Example of Abstraction in C++
class and objects
Methods in java
Inheritance and Interfaces
Inheritance in c++
Final keyword in java
This pointer

What's hot (20)

PPT
File handling in C++
PPT
Chapter 13 - Recursion
PDF
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
PPTX
Inheritance In Java
PPTX
Polymorphism in C# Function overloading in C#
PPT
Java programming concept
PPTX
Control Statement programming
PPTX
inheritance
PPTX
Functions in PHP.pptx
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
PPTX
Interface in java ,multiple inheritance in java, interface implementation
PDF
Equivalence relations
PPTX
Constructor and Destructor in c++
PPTX
Android activity lifecycle
PPT
Oops in Java
PPTX
Interface in java
DOCX
Laporan praktikum modul 4 (pemrograman applet)
PDF
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
PDF
Java keywords
PPTX
for loop in java
File handling in C++
Chapter 13 - Recursion
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Inheritance In Java
Polymorphism in C# Function overloading in C#
Java programming concept
Control Statement programming
inheritance
Functions in PHP.pptx
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Interface in java ,multiple inheritance in java, interface implementation
Equivalence relations
Constructor and Destructor in c++
Android activity lifecycle
Oops in Java
Interface in java
Laporan praktikum modul 4 (pemrograman applet)
UNIT 5-JavaFX Event Handling, Controls and Components.pdf
Java keywords
for loop in java
Ad

Viewers also liked (20)

PPTX
Agg framework selectgroup feb2015 v2
PDF
フナコシニュース2017年11月15日号
PPTX
Sbmptn gambaran antarmuka1
PDF
Bootstrap latihan
PDF
Simda proposal
PDF
Bootstrap tutorial
PDF
Modul 1-html
PPTX
Project charter-1
PDF
Javascript1 pdf
PDF
Project charter-Contoh
PPT
PDF
Presentasi alur pendaftaran untuk peserta reguler (non bidikmisi)
PDF
Project charter-template
PDF
Sbmptn 1
PDF
Proposal ppdb
PPTX
Bootstrap
PDF
Format kak
PDF
Modul 2-form html-css
PDF
Perencanaan proyek si
DOCX
Studi kelayakan sistem siap
Agg framework selectgroup feb2015 v2
フナコシニュース2017年11月15日号
Sbmptn gambaran antarmuka1
Bootstrap latihan
Simda proposal
Bootstrap tutorial
Modul 1-html
Project charter-1
Javascript1 pdf
Project charter-Contoh
Presentasi alur pendaftaran untuk peserta reguler (non bidikmisi)
Project charter-template
Sbmptn 1
Proposal ppdb
Bootstrap
Format kak
Modul 2-form html-css
Perencanaan proyek si
Studi kelayakan sistem siap
Ad

Similar to Contoh Factory pattern (20)

PDF
Design Patterns
DOCX
Abstract factory
PPTX
Design Pattern - Factory Method Pattern
PPTX
design patter related ppt and presentation
PPT
Unit 2-Design Patterns.ppt
PDF
Applying Design Patterns in Practice
PDF
E1803023637
PPTX
OOPSDesign PPT ( introduction to opps and design (
PDF
Design patterns through refactoring
PDF
The maze of Design Patterns & SOLID Principles
PPTX
gffhfghfgchfygnghS09-Design-Patterns.pptx
PDF
Design pattern tutorial
PPT
Oops design pattern intro
PPTX
PATTERNS02 - Creational Design Patterns
PPT
Introduction to design_patterns
PDF
Java design pattern tutorial
PPTX
Creational pattern 2
DOCX
Java Design Pattern Interview Questions
PPT
P Training Presentation
PDF
Design pattern tutorial
Design Patterns
Abstract factory
Design Pattern - Factory Method Pattern
design patter related ppt and presentation
Unit 2-Design Patterns.ppt
Applying Design Patterns in Practice
E1803023637
OOPSDesign PPT ( introduction to opps and design (
Design patterns through refactoring
The maze of Design Patterns & SOLID Principles
gffhfghfgchfygnghS09-Design-Patterns.pptx
Design pattern tutorial
Oops design pattern intro
PATTERNS02 - Creational Design Patterns
Introduction to design_patterns
Java design pattern tutorial
Creational pattern 2
Java Design Pattern Interview Questions
P Training Presentation
Design pattern tutorial

More from Fajar Baskoro (20)

PPTX
Pengembangan Basis Data untuk Web Application.pptx
PPTX
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
PPTX
Sosialisasi Program Digital Skills Unicef 2025.pptx
PDF
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
PDF
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
PDF
Pemrograman Mobile menggunakan kotlin2.pdf
PPTX
Membangun Kewirausahan Sosial Program Double Track.pptx
PPTX
Membangun Kemandirian DTMandiri-2025.pptx
PDF
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
PDF
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
PPTX
Seleksi Penerimaan Murid Baru 2025.pptx
PPTX
Pengembangan Program Dual Track 2025-2.pptx
PPTX
Pengembangan Program Dual Track 2025-1.pptx
PDF
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
PPTX
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PPTX
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
PPTX
Program Dual Track Kalimantan Timur 2025.pptx
PDF
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
PPTX
Pengembangan Program Digital Skills - 2025.pptx
PPTX
PPT-Proyek Magang Kewirausahaan Double Track.pptx
Pengembangan Basis Data untuk Web Application.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Sosialisasi Program Digital Skills Unicef 2025.pptx
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Pemrograman Mobile menggunakan kotlin2.pdf
Membangun Kewirausahan Sosial Program Double Track.pptx
Membangun Kemandirian DTMandiri-2025.pptx
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
Seleksi Penerimaan Murid Baru 2025.pptx
Pengembangan Program Dual Track 2025-2.pptx
Pengembangan Program Dual Track 2025-1.pptx
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
Program Dual Track Kalimantan Timur 2025.pptx
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Pengembangan Program Digital Skills - 2025.pptx
PPT-Proyek Magang Kewirausahaan Double Track.pptx

Recently uploaded (20)

PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Cost to Outsource Software Development in 2025
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
assetexplorer- product-overview - presentation
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
System and Network Administraation Chapter 3
PPTX
L1 - Introduction to python Backend.pptx
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Cost to Outsource Software Development in 2025
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
assetexplorer- product-overview - presentation
Odoo Companies in India – Driving Business Transformation.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Computer Software and OS of computer science of grade 11.pptx
wealthsignaloriginal-com-DS-text-... (1).pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
System and Network Administraation Chapter 3
L1 - Introduction to python Backend.pptx
Which alternative to Crystal Reports is best for small or large businesses.pdf
Digital Strategies for Manufacturing Companies
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PTS Company Brochure 2025 (1).pdf.......
Understanding Forklifts - TECH EHS Solution
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Contoh Factory pattern

  • 1. http://www.tutorialspoint.com/design_pattern/factory_pattern.htm Copyright © tutorialspoint.com DESIGN PATTERN - FACTORY PATTERNDESIGN PATTERN - FACTORY PATTERN Factory pattern is one of most used design pattern 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 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."); } }
  • 2. 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 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; } } 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");
  • 3. //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 circle shape3.draw(); } } Step 5 Verify the output. Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method.