SlideShare une entreprise Scribd logo
1  sur  20
CODE LIKE A NINJA
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-msharetrainingCode Like a Ninja
• Source was compiled using Visual Studio 2012
• http://www.gofpatterns.com/
Design Patterns
Creational Design Patterns
Structural Design Patterns
Behavioral 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.
• Abstract Factory
• Builder
• Factory Method
• Prototype
• Singleton
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
SIMPLE FACTORY
Client Factory
Concrete
Class 1
Concrete
Class 2
Concrete
Class 2
Base
class/
interface
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)
SIMPLE FACTORY
See simple factory code example
Question(s):
What will happen when we need to add new types
FACTORY METHOD
Client
Concrete
Factory 1
Concrete
Factory 2
Base
Factory
Concrete
Class 1
Concrete
Class 2
Base
class/
interface
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
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
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
FACTORY METHOD
See factory method code example
ABSTRACT FACTORY
Client
Abstract
Factory
Abstract
Product A
Abstract
Product B
Concrete
Factory 1
Concrete
Factory 2 Product A2
Product
B2
Product
B1
Product A1
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.
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.
ABSTRACT FACTORY– BENEFITS
• Isolates concrete classes
• Makes exchanging product families easy
• Promotes consistency among products
ABSTRACT FACTORY
See abstract factory code example
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/

Contenu connexe

Similaire à Code Like a Ninja Session 7 - Creational Design Patterns

Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
stanbridge
 
Factory Pattern
Factory PatternFactory Pattern
Factory Pattern
Deepti C
 
Design patterns
Design patternsDesign patterns
Design patterns
Alok Guha
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09
PCC
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptx
Adikhan27
 

Similaire à Code Like a Ninja Session 7 - Creational Design Patterns (20)

Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Sda 8
Sda   8Sda   8
Sda 8
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Factory Pattern
Factory PatternFactory Pattern
Factory Pattern
 
Weekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternWeekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design Pattern
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patterns
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Factory Pattern
Factory PatternFactory Pattern
Factory Pattern
 
Factory method pattern
Factory method patternFactory method pattern
Factory method pattern
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Csc253 chapter 09
Csc253 chapter 09Csc253 chapter 09
Csc253 chapter 09
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptx
 
Desing Patterns Summary - by Jim Fawcett
Desing Patterns Summary - by Jim FawcettDesing Patterns Summary - by Jim Fawcett
Desing Patterns Summary - by Jim Fawcett
 
Unit 2-Design Patterns.ppt
Unit 2-Design Patterns.pptUnit 2-Design Patterns.ppt
Unit 2-Design Patterns.ppt
 
Effective java
Effective javaEffective java
Effective java
 
Software Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural PatternsSoftware Design Patterns. Part I :: Structural Patterns
Software Design Patterns. Part I :: Structural Patterns
 

Dernier

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Dernier (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Code Like a Ninja Session 7 - Creational Design Patterns

  • 1. CODE LIKE A NINJA 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/
  • 3. Design Patterns Creational Design Patterns Structural Design Patterns Behavioral Design Patterns
  • 4. 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.
  • 5. • Abstract Factory • Builder • Factory Method • Prototype • Singleton CREATIONAL DESIGN PATTERNS
  • 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
  • 7. SIMPLE FACTORY Client Factory Concrete Class 1 Concrete Class 2 Concrete Class 2 Base class/ interface
  • 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
  • 10. FACTORY METHOD Client Concrete Factory 1 Concrete Factory 2 Base Factory Concrete Class 1 Concrete Class 2 Base class/ interface
  • 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
  • 14. FACTORY METHOD See factory method code example
  • 15. ABSTRACT FACTORY Client Abstract Factory Abstract Product A Abstract Product B Concrete Factory 1 Concrete Factory 2 Product A2 Product B2 Product B1 Product A1
  • 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
  • 19. ABSTRACT FACTORY See abstract factory code example
  • 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/

Notes de l'éditeur

  1. Basic srpExposeutils class antipatternExpose singleton as a bad pattern to use (use at own risk)