This document discusses creational design patterns including factory method, abstract factory, and simple factory patterns. It provides definitions and examples of when each pattern should be used. The factory method pattern is used when classes delegate responsibility for object instantiation to subclasses, while abstract factory is used when families of related objects are designed to be used together and their instantiation must be consistent. Code examples are provided to illustrate simple factory, factory method, and abstract factory patterns. Session resources including presentation notes and code sources are also listed.
1 of 20
Downloaded 11 times
More Related Content
Code Like a Ninja Session 7 - Creational Design Patterns
2. SESSION RESOURCES
• Presentation session notes including link to this session, will be available on
http://learningaboutfudge.blogspot.com
• All the source for this session is publically available at:
https://github.com/SheepWorx/Training
• RSS Feed:
http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rss
• Local Network: dmeyer-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
• http://www.gofpatterns.com/
6. FACTORY (ABSTRACT AND METHOD)
• Lets a class defer instantiation into subclasses
• Allows you to introduce new classes without modifying the code
• Factory Method: when you need to delegate the creation of single
objects
• Abstract Factory: When you need to delegate the creation of families
of related or dependant objects without specifying the concrete
classes
8. SIMPLE FACTORY
• Classes that need to be instantiated need to inherit from a common
class (abstract class or interface)
• The factory will receive some form of identifier and create the correct
concrete class
• Will return it via its base class (abstract class or interface)
9. SIMPLE FACTORY
See simple factory code example
Question(s):
What will happen when we need to add new types
11. FACTORY METHOD
• Classes you want to create must inherit from a base object (abstract class or
interface)
• Each class will have its own factory
• The factories themselves will inherit off a base factory (abstract class)
• Base factory will control behaviour while individual factories will be
responsible for returning concrete instances of the desired class
• If unique logic exists for a particular class, I recommend it be encapsulated
within its factory
12. FACTORY METHOD – WHEN TO USE IT
It should be used when…
• A class cannot anticipate the class of objects it must create
• A class wants it subclasses to specify the objects it creates
• Classes delegate responsibility to one of several helper classes and
you want to localize the knowledge of which helper subclass is the
delegate
13. FACTORY METHOD – BENEFITS
• Eliminates the need to bind application classes to your code
The code only deals with the interface
• Enables subclasses to provide an extended version of an object
because creating an object inside a class is more flexible than
creating the object directly in the client
16. ABSTRACT FACTORY
• Provides an interface for creating families of related or dependant
objects without specifying their concrete classes
• The client interacts only with the product interfaces and the abstract
factory class. It thus never knows about the concrete construction
provided by this pattern
• Abstract factory is similar to the factory method, except it creates
families of related objects.
17. ABSTRACT FACTORY – WHEN TO USE IT
It should be used when…
• The system should be independent of how its products are created,
composed and represented
• The system should be configured with one of multiple families of
products
• The family of related product objects is designed to be used together
and you must enforce this constraint.
18. ABSTRACT FACTORY– BENEFITS
• Isolates concrete classes
• Makes exchanging product families easy
• Promotes consistency among products
20. SESSION RESOURCES
• Presentation session notes including link to this session, will be available on
http://learningaboutfudge.blogspot.com
• All the source for this session is publically available at:
https://github.com/SheepWorx/Training
• RSS Feed:
http://learningaboutfudge.blogspot.com/feeds/posts/default?alt=rss
• Local Network: dmeyer-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
• http://www.gofpatterns.com/
Editor's Notes
Basic srpExposeutils class antipatternExpose singleton as a bad pattern to use (use at own risk)