KEMBAR78
Factory Method Pattern: Bryan Hansen | PDF | Art | Computers
0% found this document useful (0 votes)
62 views8 pages

Factory Method Pattern: Bryan Hansen

The Factory Method Pattern defines a common interface for creating objects but allows subclasses to determine which objects are instantiated. This allows objects to be created without specifying the exact class and makes a system more extensible and adaptable to change. Some key aspects are that the factory is responsible for the lifecycle of objects, there is a common interface for the objects, and concrete subclasses implement creation logic. Examples of the pattern in use include the Calendar, ResourceBundle, and NumberFormat classes.

Uploaded by

Kuby Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views8 pages

Factory Method Pattern: Bryan Hansen

The Factory Method Pattern defines a common interface for creating objects but allows subclasses to determine which objects are instantiated. This allows objects to be created without specifying the exact class and makes a system more extensible and adaptable to change. Some key aspects are that the factory is responsible for the lifecycle of objects, there is a common interface for the objects, and concrete subclasses implement creation logic. Examples of the pattern in use include the Calendar, ResourceBundle, and NumberFormat classes.

Uploaded by

Kuby Santos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Factory Method Pattern

Bryan Hansen
twitter: bh5k | http://www.linkedin.com/in/hansenbryan
Concepts
▪ Doesn’t expose instantiation logic
▪ Defer to subclasses
▪ Common interface
▪ Specified by architecture, implemented by user
▪ Examples:
▪ Calendar
▪ ResourceBundle
▪ NumberFormat
Design

Factory is responsible for lifecycle


Common Interface
Concrete Classes
Parameterized create method
Everyday Example - Calendar

Calendar  cal  =  Calendar.getInstance();  


     
System.out.println(cal);  

System.out.println(cal.get(Calendar.DAY_OF_MONTH));  
     
Exercise Factory

Create Pages
Create Website
Create Concrete Classes
Create Factory
Enum
Pitfalls
▪ Complexity
▪ Creation in subclass
▪ Refactoring
Contrast

Singleton Factory
▪ Returns same instance ▪ Returns various instances
▪ One constructor method - no ▪ Multiple constructors
args ▪ Interface driven
▪ No Interface ▪ Subclasses
▪ No Subclasses ▪ Adaptable to environment more
easily
Factory Summary

• Parameter Driven
• Solves complex creation
• A little complex
• Opposite of a Singleton

You might also like