Factory Pattern.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

More about classes and objects Classes in Visual Basic.NET.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Informatics 122 Software Design II Lecture 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
Advanced Programming Rabie A. Ramadan 7.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
DESIGN PATTERNS -CREATIONAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
(c) University of Washington02-1 CSC 143 Java Object and Class Relationships: Interfaces Reading: Ch. 9 (on Java interfaces)
The Factory Pattern Sanjay Yadav (ISE ).
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Repeating patterns Can you work out the next shape in the pattern?
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Object-Oriented Programming: Polymorphism Chapter 10.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
1 Section 11.4 Java Interfaces – The Implementation Perspective Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
1 More About Derived Classes and Inheritance Chapter 9.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Factory Method Pattern. Admin SCPI Patner Day Feb. 21 Lunch count Presentation (4-8 min.) Practice on Feb. 16. Morning availablity on Feb21 Brief overview.
Abstract Classes, Abstract Methods, Override Methods
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Unit II-Chapter No. : 5- design Patterns
Factory Method Pattern
Polymorphism.
Design Patterns C++ Java C#.
Low Budget Productions, LLC
Factory Patterns 1.
Design Patterns C++ Java C#.
Design Patterns Based on slides provided by Abbie Jarrett
Software Design and Architecture
Polymorphism.
Factory Method Pattern
Name the shape below. rectangle rhombus square triangle A B C D
CS250 Introduction to Computer Science II
Classes in C++ C++ originally called "C with classes":
Class vs Abstract vs Interface
ABSTRACT FACTORY.
Classes in C++ C++ originally called "C with classes":
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Week 7, Class 1: The Command Pattern (cont.)
Advanced Programing practices
Polymorphism Pattern.
Software Design Lecture : 12.
Polymorphism CMSC 202, Version 4/02.
Shapes.
Lesson 5: More on Creational Patterns
Classes and Objects B.Ramamurthy 5/9/2019 B.Ramamurthy.
Decorator Pattern.
Area Of Composite Shapes
What are different ways to show fractions?
Can you work out the next shape in the pattern?
Software Design Lecture : 28.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Can you work out the next shape in the pattern?
Presentation transcript:

Factory Pattern

Factory Method Defines an interface for creating objects but let sub-classes decide which of those instantiate. Enables the creator to defer Product creation to a sub-class. Factory pattern is one of the 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.

Factory Pattern 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. CLASS DIAGRAM :

Steps for Implementation 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."); }

Step 3 - Create concrete class implementing the same interface. Square.java public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); } Step 4 - Create concrete class implementing the same interface. Circle.java public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }

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

Step 6 - Use the Factory to get object of concrete class by passing an information such as type. FactoryPatternDemo.java OUTPUT Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method.

Factory Pros & Cons. Pros: Shields clients from concrete classes. If a framework follows a Factory pattern, it enables the third party developers to plug-in new products. Cons: Coding a new product means, writing two classes one for concrete factory and concrete product. Inheritance based.

Example problem let's create an AnimalFactory class that will return an animal object based on some data input.