Factory Design Pattern, cont’d COMP 401 Fall 2014 Lecture 13 10/2/2014.

Slides:



Advertisements
Similar presentations
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Advertisements

Mutability SWE 332 Fall 2011 Paul Ammann. SWE 3322 Data Abstraction Operation Categories Creators Create objects of a data abstraction Producers Create.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 20 Object Oriented Theory II.
Chapter 6: Using Design Patterns
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Object-Oriented PHP (1)
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Object Oriented Databases - Overview
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Programming Languages and Paradigms Object-Oriented Programming.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
CSCI 6962: Server-side Design and Programming Support Classes and Shopping Carts.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Andrew S. Budarevsky Adaptive Application Data Management Overview.
Chapter 18 Java Collections Framework
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Encapsulation COMP 401, Fall 2014 Lecture 06 9/4/2014.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Goals for Today 1  Multiton  maps  static factory methods.
Mixing Static and Non-static
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Lecture 121 CS110 Lecture 12 Tuesday, March 9, 2004 Announcements –hw5 due Thursday –Spring break next week Agenda –questions –ArrayList –TreeMap.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
1 Kyung Hee University Modeling with Objects Spring 2001.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Mixing Static and Non-static Singleton 1. Singleton Pattern 2  “There can be only one.”  Connor MacLeod, Highlander.
Maps Nick Mouriski.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Class Fundamentals BCIS 3680 Enterprise Programming.
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
CSCE 240 – Intro to Software Engineering Lecture 3.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Data Abstraction: The Walls
Sixth Lecture ArrayList Abstract Class and Interface
Factory Patterns 1.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
CS 326 Programming Languages, Concepts and Implementation
Chapter 3: Using Methods, Classes, and Objects
Chapter 6: Using Design Patterns
Java Collections Overview
Arrays and Collections
Stacks.
Review: libraries and packages
Hashing in java.util
Presentation transcript:

Factory Design Pattern, cont’d COMP 401 Fall 2014 Lecture 13 10/2/2014

Factory Design Pattern When direct construction of an object is complicated or harmful – … or at least undesired – By “direct construction”, I mean by using the new keyword Several different contexts when factory design pattern is appropriate: – Singleton When there should only be one instance of a particular class in the whole system. – Multiton When there should only be one instance of of an object associated with some identifying property – Dynamic subclass binding When the specific subclass of an object can only be determined at the time that it is created – Complex construction When the construction of the object requires complex validation and/or side effects that must be handled. When null should be a valid result When an existing object might need to be provided instead of a new one.

The Basic Factory Pattern Make the constructor private – This prevents direct use of the new keyword for creating new objects outside of the class. Provide static class methods that construct new instances and return them. – These are the “factories” – Return type of the factory method is the same as the class. class FPClass { private FPClass() { // Private constructor } public static FPClass factoryMethod() { return new FPClass(); } FPClass o = FPClass.factoryMethod();

Singleton One Object To Rule Them All When there should only be one instance of a particular class in the whole system at any given time. – Generally the object represents some sort of system-wide resource that may be needed by many objects in different parts of the software. – Modifies basic factory pattern by maintaining a single instance. » Created on demand when first requested Lazy initiation » Stored in a private static variable and retrieved by a static getter Static getter is the factory method lec12.ex1 – Simple logging mechanism. lec12.ex2 – A variant that allows for singleton instance to be one of several subclasses.

Multiton Maintains a single instance of the object with regard to some uniquely identifying characteristic of object. Multiton factory method – Static (as per the general factory pattern) – Provided all info. needed to create a new object if necessary. – Determines if corresponding object already exists. If so, returns the existing object If not, creates the new object and inserts it into a static structure Usually implemented using a “map”

Map and HashMap Map is a collection of key/value pairs – Sometimes called a “dictionary” Java Collections Framework – Interface: Map – Implementation: HashMap – K and V are placeholders for type names K for the type of the key, and V for the type of the value. Must be reference types Basic operations: – Creating a new map Map m = new HashMap () – Inserting a value associated with a key m.put(key, value); – Retrieving a value associated with a key V value = m.get(key); – Checking to see if key already in the map m.containsKey(key); // Returns boolean – Removing a key/value pair from the map m.remove(key)

Multiton Example lec12.ex3 – Student is a class that models students Uniquely identified by PID property – getStudent() is factory method Notice that all info needed to create student is provided. – lookupStudent() is a factory method variant that only retrieves existing objects. Does not create object if it doesn’t exist (but must signal that fact) In example, this is done by returning null What would another option have been? lec12.ex4

Value Types In A Reference Type Context Integer reference type – Map can only work with reference types but the key we want to use is simple integer – Java provides reference type versions of basic value types for these kinds of situations. Integer, Double, Character Automatic “boxing” and “unboxing” – As of Java 1.7, can use value type where reference type is expected and Java will automatically “box” the value. – Similarly, will “unbox” a reference type to the value type when assigning to a value type variable.

Dynamic Subclass Binding Useful when choice needs to be made between several different subclasses – Leaves decision about which subclass to use to the factory method Factory method given any/all information that is relevant to decision and for creating new object if necessary. lec12.ex5

A rose by any other name Some design pattern textbooks / frameworks associate the name “Factory” specifically and only for dynamic subclass binding use case. – Other cases described as separate patterns In my presentation, I’ve grouped all of these use cases under the name “Factory” more generally. – Just something to be aware of if/when you encounter the term in tutorials/documentation/etc.