Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Reusable Classes.  Motivation: Write less code!
SE2811 Week 7, Class 2 The Gang of Four and more … Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Aniruddha Chakrabarti
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
1 Computer Science 340 Software Design & Testing Inheritance.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
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.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Design Patterns: Elements of Reusable Object- Orientated Software Gamma, Helm, Johnson, Vlissides Presented By: David Williams.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Inheritance ndex.html ndex.htmland “Java.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Catalog of Refactoring
Introduction To Design Patterns
Design Patterns: MORE Examples
Factory Method Pattern
Lecture 12 Inheritance.
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Low Budget Productions, LLC
Factory Patterns 1.
Design Patterns C++ Java C#.
Software Design and Architecture
CS202 Lecture 16.
Refactoring Methods: Kevin Murphy.
Factory pattern Unit of Work
Factory Method Pattern
null, true, and false are also reserved.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance Cse 3rd year.
Chapter 9 Carrano Chapter 10 Small Java
Instructor: Alexander Stoytchev
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Composite Design Pattern By Aravind Reddy Patlola.
Topics OOP Review Inheritance Review Abstract Classes
Software Design Lecture : 28.
Chapter 5 Classes.
Presentation transcript:

Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern

Overview Introduction Factory Method Structure Implementation Example Conclusion References 2

Introduction Reusability is a goal for design patterns Design patterns help programmers identify reoccurring design issues Design Patterns also help insure encapsulation and information hiding Factory method pattern is an object oriented design pattern. It’s considered a creational pattern 3

Factory Method The problem: We don’t know when to instantiate object! Factory method defers instantiation to subclasses It’s goal is when to instantiate more than what to instantiate Uses an interface that decides what subclass to choose, then instantiate an object from the subclass that was chosen 4

Factory Method The user code doesn’t create objects The factory method instantiates objects Determination of what class to instantiate is handled at runtime 5

Factory Method When to use the Factory Method pattern: The superclass cannot determine what class to instantiate objects from The superclass wants its subclass to specify the objects Factory Method pattern helps hiding the core classes from clients 6

Structure 7

Implementation The Creator class is designed as an abstract class No implementation for the abstract class Subclasses must have an implementation that deals with the Factory Method 8

Implementation 9

public abstract class Profile { public Profile (String sName, String s ) { m_sName = sName; m_s = s ; } public String getName () { return m_sName; } public String get () { return m_s ; } public boolean IsEmployee () { return m_bIsEmployee; } public abstract Resource getResource (); protected String m_sName, m_s ; protected boolean m_bIsEmployee; } Reference  Goplan Suresh Raj 10

Implementation The Creator class is designed as a concrete class Create the object then let the subclass overrides it More flexible Involves the concept of overriding 11

Implementation Using a parameterized Factory Method In this implementation we have to use an identifier From that identifier we can know what object to create 12

Implementation public class ResourceCreator { public static final int CONFIDENTIAL = 0; public static final int PUBLIC = 1; public Resource createResource (int nID) { switch (nID) { case CONFIDENTIAL: return new ConfidentialResource (); case PUBLIC: return new PublicResource (); } return null; } Reference  Goplan Suresh Raj 13

Implementation public class Employee extends Profile { public Employee (String sName, String s ) { super (sName, s ); m_bIsEmployee = true; } public Resource getResource () { ResourceCreator creator = new ResourceCreator (); return creator.createResource (ResourceCreator.CONFIDENTIAL); } Reference  Goplan Suresh Raj 14

Implementation public class NonEmployee extends Profile { public NonEmployee (String sName, String s ) { super (sName, s ); m_bIsEmployee = false; } public Resource getResource () { ResourceCreator creator = new ResourceCreator (); return creator.createResource (ResourceCreator.PUBLIC); } Reference  Goplan Suresh Raj 15

Implementation Templates instead of subclasses C++ Naming conventions Good for readability 16

Example 17

Example public class Person { // name string public String name; // gender : M or F private String gender; public String getName() { return name; } public String getGender() { return gender; } }// End of class 18

Example public class Male extends Person { public Male(String fullName) { System.out.println("Hello Mr. "+fullName); } }// End of class public class Female extends Person { public Female(String fullNname) { System.out.println("Hello Ms. "+fullNname); } }// End of class 19

Example public class SalutationFactory { public static void main(String args[]) { SalutationFactory factory = new SalutationFactory(); factory.getPerson(args[0], args[1]); } public Person getPerson(String name, String gender) { if (gender.equals("M")) return new Male(name); else if(gender.equals("F")) return new Female(name); else return null; } }// End of class 20

Conclusion Introduction Factory Method Structure Implementation Example Conclusion References 21

References Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley. ISBN Gamma, ErichHelm, RichardDesign Patterns: Elements of Reusable Object-Oriented SoftwareISBN Raj, Goplan Suresh. AllAppLaps.com. Tarr, Bob. Factory Patterns. 22