1 / 20

Lecture 15: abstract Factory Pattern

CSC 313 – Advanced Programming Topics. Lecture 15: abstract Factory Pattern. What Is the Factory Method?. Creation details hidden by AbstractCreator Does effective job of limiting concrete knowledge. Factory Method Intent. Use incomplete types as the declared type of

yan
Download Presentation

Lecture 15: abstract Factory Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 313 – Advanced Programming Topics Lecture 15:abstract Factory Pattern

  2. What Is the Factory Method? • Creation details hidden by AbstractCreator • Does effective job of limiting concrete knowledge

  3. Factory Method Intent • Useincomplete types as the declared type of • Local variables, fields, parameters … • Everywhere, since change might occur anywhere • Since it must be concrete, avoid new command • Replace with factory pattern • Design is then much easier to add new classes • Limits changes required when modify existing classes

  4. Factory Method Use/Avoid • Use a factory method pattern when you • Create object, but can choose class instantiated • Hide subclasses of abstract type from client • Must do complex allocation of object stack • Multiple related allocations is also important • Factory patterns so far define 1 factory method • Create 1 type of object (though many subtypes) • Forced linking or chaining allocations impossible

  5. Decorator:Factory::Strategy:? • Factory Method great for decorator pattern • Inheritance-based design of classes in this pattern • Core concept will be wrapped by many decorators • Returns single instance of abstract supertype • But pattern not helpful for Strategy Pattern • Works for single strategy, since need only 1 type • Often allocate & use many strategies, however • Factory needed for each type of strategy, however

  6. Decorator:Factory::Strategy:?

  7. Often Have Related Strategy • Skinnable apps can change look-and-feel • Similar allocations needed for all widgets

  8. Often Have Related Strategy • Skinnable apps can change look-and-feel • Similar allocations needed for all widgets

  9. Often Have Related Strategy • Skinnable apps can change look-and-feel • Similar allocations needed for all widgets

  10. Sets of Choices • Instantiate sets of components to look good • Different set defined by each look-and-feel • Instantiate only from set defined by components • To change skins, must switch set allocating from • More places need this than skinnable apps • Each game level generates class of enemies • Quacking & flying behaviors in SimUDuck • Printing & transmitting behavior of messages in IM • Colors & fonts used in a presentation

  11. Abstract Factory Pattern • Abstract Factory defines set of methods • Each method is factory for different type of object • Use this pattern to allocate family of objects • Every componentwithin a look-and-feel • Level’s collection of snipers, tanks, & grues • Quacking & flying behavior for the ducks created • Abstract Factory is composition of factories • Groups related ideas so will be allocated together

  12. Client View of Pattern • Clients use AbstractFactory & ignores details • All of the factory methods declared by this class

  13. Abstract Factory Design • Common code & fields in AbstractFactory • Can be either an abstract class or interface

  14. Abstract Factory UML • Products are supertypesof useful objects • But pattern will only return subtypes of these

  15. Abstract Factory UML • Client uses subclasses of AbstractFactory • ConcreteFactorysperform actual allocations

  16. Composition over Inheritance • Inheritance makes mixing options easy

  17. Nearly Real Example • Abstract factory generates related types • Building car from parts would be real-life example

  18. Abstract Factory Vitals • Family of related classes from 1 instance • Pattern is also a composition of factory methods • Related or interdependent classes allocated together • Unlike factory method, instantiates multiple types • Skinnable & visual systems clearest example • Scrollbars, tabs, menus, …: many shared concepts • Concrete implementation changes with each look

  19. Abstract Factory Vitals • Family of related classes from 1 instance • Pattern is also a composition of factory methods • Related or interdependent classes allocated together • Unlike factory method, instantiates multiple types • Skinnable & visual systems clearest example • Scrollbars, tabs, menus, …: many shared concepts • Concrete implementation changes with each look • Clearly identified if classes used must be related

  20. For Next Lecture • Lab #4 available on Angel • Lab will be due before next lab (Fri. 2/26) • Read pages 169-178 in book • How can we make sure only 1 end boss? • What is plural of apocalypse? How to code this? • How are design patterns & Highlander alike?

More Related