Abstract Factory Pattern

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Plab – Tirgul 12 Design Patterns
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Prototype Pattern Intent:
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Design Patterns.
Case Studies on Design Patterns Design Refinements Examples.
Creational Patterns (1) CS350, SE310, Fall, 2010.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Components Creational Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
ANU COMP2110 Software Design in 2004 Lecture 17Slide 1 COMP2110 in 2004 Software Design Lecture 17: Software design patterns (4) 1The Abstract Factory.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Advanced Object-oriented Design Patterns Creational Design Patterns.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CSC 480 Software Engineering Lab 5 – Abstract Factory Pattern Oct 30, 2002.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
 Creational design patterns abstract the instantiation process.  make a system independent of how its objects are created, composed, and represented.
The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.
S.Ducasse Stéphane Ducasse 1 Abstract Factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CSCE 240 – Intro to Software Engineering Lecture 3.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by FACADE PATTERN
Design Patterns: MORE Examples
Jim Fawcett CSE775 – Distributed Objects Spring 2017
Abstract Factory Pattern
Factory Method Pattern
Strategy Design Pattern
Factory Patterns 1.
Behavioral Design Patterns
Design Patterns with C# (and Food!)
Factory Method Pattern
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
object oriented Principles of software design
Decorator Design Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Jim Fawcett CSE776 – Design Patterns Summer 2003
UNIT-III Creational Patterns UNIT-III.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
An Introduction to Software Architecture
Strategy Design Pattern
Lesson 5: More on Creational Patterns
Creational Patterns.
Informatics 122 Software Design II
Chapter 8 - Design Strategies
Message Passing Systems Version 2
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Message Passing Systems
Presentation transcript:

Abstract Factory Pattern Jim Fawcett CSE776 – Design Patterns Fall 2017

Why is this so important? Intent “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.” provide a simple creational interface for a complex family of classes Client does not have to know any of those details. avoid naming concrete classes Clients use abstract creational interfaces and abstract product interfaces. Concrete classes can be changed without affecting clients. Clients can stay blissfully unaware of implementation details This is critically important! Why is this so important?

Motivating Examples Target multiple platforms by creating component instances for selected platform. Design neural network layers to contain the technology for their own construction, allowing networks to focus on learning strategies.

Forces The deep copy object model, used by the C++ language, binds a class to all the concrete classes it uses. The class’s header file holds information about supporting classes to allow them to be created and used. If one of the serving classes changes, the using class must also change, and every class that uses this class must also change. To prevent this “top-to-bottom” binding, a class must bind to interface abstractions instead. It must also use factories as proxies to create the instances it uses. In a large system we will be likely to use interfaces and factories between subsystems. A factory will then need to manage creation of many of the objects within a subsystem.

Dependencies – Mozilla, version 1.4.1 Strong component of dependency graph, e.g., set of mutually dependent files

Forces Languages like C# and Java have less need for factories due to their shallow reference object model. The physical layout of code for a given class does not depend on the sizes of the objects it uses, unlike C++. The new operator extracts information about how to build an instance from its class’s metadata, not from the classes that use it, unlike C++. However, even for these languages the Abstract Factory Pattern is still useful! Interfaces support the exchange of implementing types, even at run-time, which is often very useful. Factories support binding of specific objects to these interfaces, without requiring clients to have knowledge of the specific types.

Logical System Layering

Dependency Inversion Principle

Abstract Interface

Layering with Abstract Interfaces

Applicability Use the Abstract Factory Pattern if: clients need to be ignorant of how servers are created, composed, and represented. clients need to operate with one of several families of products a family of products must be used together, not mixed with products of other families. you provide a library and want to show just the interface, not implementation of the library components. Giving customers your product header files may disclose some of your proprietary value.

Abstract Factory Structure

Why is there a parallel hierarchy of factories and products? Participants Why is there a parallel hierarchy of factories and products? AbstractFactory provide an interface for building product objects ConcreteFactory implements the creation functionality for a specific product family AbstractProduct provides an interface for using product objects ConcreteProduct created by a ConcreteFactory, implements the AbstractProduct interface for a specific product family Client uses only abstract interfaces so is independent of the implemen-tation.

Collaborators Usually only one ConcreteFactory instance is used for an activation, matched to a specific application context. It builds a specific product family for client use -- the client doesn’t care which family is used -- it simply needs the services appropriate for the current context. The client may use the AbstractFactory interface to initiate creation, or some other agent may use the AbstractFactory on the client’s behalf. The factory returns, to its clients, specific product instances bound to the product interface. This is what clients use for all access to the instances.

Presentation Remark Here, we often use a sequence diagram (event- trace) to show the dynamic interactions between participants. For the Abstract Factory Pattern, the dynamic interaction is simple, and a sequence diagram would not add much new information.

Consequences The Abstract Factory Pattern has the following benefits: It isolates concrete classes from the client. You use the Abstract Factory to control the classes of objects the client creates. Product names are isolated in the implementation of the ConcreteFactory, clients use the instances through their abstract interfaces. Exchanging product families is easy. None of the client code breaks because the abstract interfaces don’t change. Because the abstract factory creates a complete family of products, the whole product family changes when the concrete factory is changed. It promotes consistency among products. It is the concrete factory’s job to make sure that the right products are used together.

Consequences More benefits of the Abstract Factory Pattern It supports the imposition of constraints on product families, e.g., always use A1 and B1 together, otherwise use A2 and B2 together.

Consequences The Abstract Factory pattern has the following liability: Adding new kinds of products to existing factory is difficult. Adding a new product requires extending the abstract interface which implies that all of its derived concrete classes also must change. Essentially everything must change to support and use the new product family abstract factory interface is extended derived concrete factories must implement the extensions a new abstract product class is added a new product implementation is added client has to be extended to use the new product

Abstract Factory Structure To add a new product, all the factory interfaces must change.

Implementation Concrete factories are often implemented as singletons. Creating the products Concrete factory usually use the factory method. simple new concrete factory is required for each product family alternately concrete factory can be implemented using prototype. only one is needed for all families of products product classes now have special requirements - they participate in the creation

Implementation Defining extensible factories by using create function with an argument only one virtual create function is needed for the AbstractFactory interface all products created by a factory must have the same base class or be able to be safely coerced to a given type it is difficult to implement subclass specific operations

Know Uses Interviews ET++ used to generate “look and feel” for specific user interface objects uses the Kit suffix to denote AbstractFactory classes, e.g., WidgetKit and DialogKit. also includes a layoutKit that generates different composite objects depending on the needs of the current context ET++ another windowing library that uses the AbstractFactory to achieve portability across different window systems (X Windows and SunView). COM – Microsoft’s Component Object Model technology Each COM component provides a concrete factory bound to the IClassFactory interface and provides clients specific instances bound to the server’s product interface.

Related Patterns Factory Method -- a “virtual” constructor Prototype -- asks products to clone themselves Singleton -- allows creation of only a single instance

Code Examples Skeleton Example Neural Net Example Abstract Factory Structure Skeleton Code Neural Net Example Neural Net Physical Structure Neural Net Logical Structure Simulated Neural Net Example

End of Presentation