Code Like a Ninja Session 7 - Creational Design Patterns

20
CODE LIKE A NINJA CREATIONAL DESIGN PATTERNS

description

presentation on some creational design patterns namely the factory method and the abstract factory

Transcript of Code Like a Ninja Session 7 - Creational Design Patterns

Page 1: Code Like a Ninja Session 7 - Creational Design Patterns

CODE LIKE A NINJACREATIONAL DESIGN PATTERNS

Page 2: Code Like a Ninja Session 7 - Creational Design Patterns

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-m\share\training\Code Like a Ninja

• Source was compiled using Visual Studio 2012

• http://www.gofpatterns.com/

Page 3: Code Like a Ninja Session 7 - Creational Design Patterns

Design Patterns

Creational Design PatternsStructural Design PatternsBehavioral Design Patterns

Page 4: Code Like a Ninja Session 7 - Creational Design Patterns

CREATIONAL DESIGN PATTERNS

Definition

Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

Page 5: Code Like a Ninja Session 7 - Creational Design Patterns

• Abstract Factory

• Builder

• Factory Method

• Prototype

• Singleton

CREATIONAL DESIGN PATTERNS

Page 6: Code Like a Ninja Session 7 - Creational Design Patterns

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

Page 7: Code Like a Ninja Session 7 - Creational Design Patterns

SIMPLE FACTORY

Client Factory

Concrete Class 1

Concrete Class 2

Concrete Class 2

Base class/

interface

Page 8: Code Like a Ninja Session 7 - Creational Design Patterns

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)

Page 9: Code Like a Ninja Session 7 - Creational Design Patterns

SIMPLE FACTORY

See simple factory code example

Question(s):

What will happen when we need to add new types

Page 10: Code Like a Ninja Session 7 - Creational Design Patterns

FACTORY METHOD

Client

Concrete Factory 1

Concrete Factory 2

Base Factory

Concrete Class 1

Concrete Class 2

Base class/

interface

Page 11: Code Like a Ninja Session 7 - Creational Design Patterns

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

Page 12: Code Like a Ninja Session 7 - Creational Design Patterns

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

Page 13: Code Like a Ninja Session 7 - Creational Design Patterns

FACTORY METHOD – BENEFITS

• Eliminates the need to bind application classes to your codeThe 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

Page 14: Code Like a Ninja Session 7 - Creational Design Patterns

FACTORY METHOD

See factory method code example

Page 15: Code Like a Ninja Session 7 - Creational Design Patterns

ABSTRACT FACTORYClient

Abstract Factory

Abstract Product A

Abstract Product B

Concrete Factory 1

Concrete Factory 2 Product

A2

Product B2

Product B1

Product A1

Page 16: Code Like a Ninja Session 7 - Creational Design Patterns

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.

Page 17: Code Like a Ninja Session 7 - Creational Design Patterns

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.

Page 18: Code Like a Ninja Session 7 - Creational Design Patterns

ABSTRACT FACTORY– BENEFITS

• Isolates concrete classes

• Makes exchanging product families easy

• Promotes consistency among products

Page 19: Code Like a Ninja Session 7 - Creational Design Patterns

ABSTRACT FACTORY

See abstract factory code example

Page 20: Code Like a Ninja Session 7 - Creational Design Patterns

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-m\share\training\Code Like a Ninja

• Source was compiled using Visual Studio 2012

• http://www.gofpatterns.com/