Factory Methods Factory Method is a method that creates objects.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 10 API Review; Where Next.
Advertisements

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.
Introduction to Programming with Java, for Beginners
1 Inheritance Overview l Inheritance ensures Reusability l Example of Inheritance l What is actually Inherited? l Overloading Vs. Overriding l Object:
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
Abstract Data Types II. 2 Sufficient operations Operations on an ADT are sufficient if they meet all the requirements They must be able to create all.
How do you simplify? Simple Complicated.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Goals for Today  implement a Deck of Cards  composition  Iterator interface  Iterable interface 1.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Effective Java: Creating and Destroying Objects Last Updated: Fall 2012.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns Based on The Design Patterns Java Companion by James Cooper
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Programming in Java CSCI-2220 Object Oriented Programming.
Mixing Static and Non-static
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Project Scenario for OpX. High-level Overview In the Test GUI Controller, opXController, for the opX command there is conceptually a call to an appropriate.
1 CSE 331 Design Patterns 1: Iterator, Adapter, Singleton, Flyweight slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin,
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Singleton Pattern (Creational)
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
Banking Service class BankingService { LinkedList accounts; LinkedList customers; double getBalance(int forAcctNum) { for (Account acct:accounts) { if.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Catalog of Refactoring (6) Making Method Calls Simpler.
Effective Java Source: Effective Java, Jason Arnold
Section 9: Design Patterns
MPCS – Advanced java Programming
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Data Types II.
Abstract Factory Pattern
Lecture 6: Composition and Inheritance
CS1101: Programming Methodology Recitation 7 – Exceptions
Refactoring Methods: Kevin Murphy.
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Un</br>able’s MySecretSecrets
suggested reading: Java Ch. 6
Section 9: Design Patterns
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Exercise on Java Basics
Polymorphism.
08/15/09 Design Patterns James Brucker.
Java Classes and Objects 3rd Lecture
Date-time.
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Introduction to Objects
Section 9: Design Patterns
Section 9: Design Patterns
Interator and Iterable
Week 5 Review & Announcement
Widely used guidance for writing better code.
Final Jim Brucker.
Abstract Method & Abstract Classes
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Designing For Testability
CSC 143 Java Errors and Exceptions.
2009 Test Key.
Making a copy of an object: shallow and deep copy
Section 9: Design Patterns
Presentation transcript:

Factory Methods Factory Method is a method that creates objects. 08/15/09 Factory Methods Factory Method is a method that creates objects. They are often used instead of constructors, when (a) creating object is complex (b) you want to control object creation (c) you want to create subclasses of the same type.

08/15/09 Factory Methods A simple factory method is useful for simplifying object creation. For example: Calendar cal = Calendar.newInstance( ); What it actually did: cal.getClass().getName() "java.util.GregorianCalendar"

08/15/09 Simple Factory Method Calendar.getInstance() controls object creation so it can create a calendar for your location. You can't write "new Calendar( )". Calendar cal = Calendar.getInstance( ); Calendar cal = Calendar.getInstance( locale ); cal.toString() //Thai Locale: 24 Jan 2561 //U.S. Locale: Jan 24, 2018 //Japan Locale: 2018 Jan 24

Factory Method can create subtype 08/15/09 Factory Method can create subtype A factory method has freedom to create an object of a subclass. Calendar cal = Calendar.getInstance( ); // What did it create? // Use getClass() to get the class name System.out.println( cal.getClass().getName() ); java.util.GregorianCalendar

Java API has Many Factory Methods 08/15/09 Java API has Many Factory Methods // An immutable date object LocalDate date = LocalDate.now( ); // A Double object from a String Double pi = Double.valueOf("3.14159"); // Connection to a database for JDBC // creating a connection is complex and // might fail, so use factory method String url = "jdbc:mysql://somehost/oopdb"; Connection connection = DriverManager.getConnection(url);

iterable.iterator( ) is Factory Method 08/15/09 iterable.iterator( ) is Factory Method How to create iterators? We don't want the application to know the logic for creating different kinds of iterators.

Creating Money in Purse App 08/15/09 Creating Money in Purse App In the PurseConsole, we have some code like this: private Money makeMoney(double value) { if (value==1.0 || value==2.0 || value=5.0 || value=10.0) return new Coin(value,"Baht"); if (value==20 || value==50 || value=100 || value==500) return new BankNote(value, "Baht"); if (value==1000) return null; // or throw IllegalArgumentException }

08/15/09 Any Problem?

08/15/09 Requirements Change

08/15/09 Localize Change Design 1 class to be responsible for creating all money. The Purse app always uses this class to create Money. If Money changes, we only need to modify one class. MoneyFactory factory = MoneyFactory.getInstance( ); Money m = factory.makeMoney( 10.0 ) ;

Need only 1 factory object 08/15/09 Need only 1 factory object We only need 1 MoneyFactory object. So, we would like getInstance() to always return the same object. MoneyFactory factory1 = MoneyFactory.getInstance( ); MoneyFactory factory2 = System.out.println(factory1 == factory2); true