tddb84 lecture 4
play

TDDB84: Lecture 4 Abstract Factory, Dependency Injection, Composite - PowerPoint PPT Presentation

TDDB84: Lecture 4 Abstract Factory, Dependency Injection, Composite Abstract factory Ingredients Pizza Store Clients Fresh Clam NY Mozzarella Cheese Thin Crust Dough I Want a Cheese Pizza Chicago Frozen Clam Parmesan Cheese Thick


  1. TDDB84: Lecture 4 Abstract Factory, Dependency Injection, Composite

  2. Abstract factory

  3. Ingredients Pizza Store Clients Fresh Clam NY Mozzarella Cheese Thin Crust Dough I Want a Cheese Pizza Chicago Frozen Clam Parmesan Cheese Thick Crust Dough

  4. Ingredients Pizza Store Clients Fresh Clam NY Mozzarella Cheese Thin Crust Dough I Want a Cheese Pizza Chicago Frozen Clam Parmesan Cheese Thick Crust Dough

  5. Ingredients Pizza Store Clients Fresh Clam NY Mozzarella Cheese I Want a Thin Crust Dough Cheese Pizza Chicago Frozen Clam Parmesan Cheese Thick Crust Dough

  6. Abstract Factory

  7. Concrete Factory Abstract Factory

  8. Concrete Factory Abstract Factory Abstract Products

  9. Clients

  10. Abstract factory: consequences

  11. Abstract factory: consequences + Isolates clients from concrete dependencies

  12. Abstract factory: consequences + Isolates clients from concrete dependencies + Makes interchanging families of products easier

  13. Strategy

  14. Strategy • When related classes only differ in behavior • You need different variants of an algorithm • An algorithm uses data the clients don’t need to know • A class uses conditionals for selecting behavior

  15. Abstract Strategy Factory • When related classes only • A system should be differ in behavior independent of how its products are created • You need different variants • A system should be of an algorithm configured with one of • An algorithm uses data the multiple families of products clients don’t need to know • You want to provide a class • A class uses conditionals library of products, and only expose their interfaces for selecting behavior

  16. Abstract Strategy Factory • When related classes only • A system should be differ in behavior independent of how its products are created • You need different variants • A system should be of an algorithm configured with one of • An algorithm uses data the multiple families of products clients don’t need to know • You want to provide a class • A class uses conditionals library of products, and only expose their interfaces for selecting behavior Behavioral

  17. Abstract Strategy Factory • When related classes only • A system should be differ in behavior independent of how its products are created • You need different variants • A system should be of an algorithm configured with one of • An algorithm uses data the multiple families of products clients don’t need to know • You want to provide a class • A class uses conditionals library of products, and only expose their interfaces for selecting behavior Behavioral Creational

  18. Design principles • Encapsulate what varies • Program to an interface, not to an implementation • Favor composition over inheritance • Classes should be open for extension but closed for modification • Don’t call us, we’ll call you

  19. Design principles • Encapsulate what varies • Program to an interface, not to an implementation • Favor composition over inheritance • Classes should be open for extension but closed for modification • Don’t call us, we’ll call you

  20. Design principles • Encapsulate what varies • Program to an interface, not to an implementation • Favor composition over inheritance • Classes should be open for extension but closed for modification • Don’t call us, we’ll call you

  21. Dependency Injection

  22. ICheesePizza <<ICheese> -cheese > StandardCheesePizza MozzarellaCheese ParmesanCheese IClamPizza <<IClam>> -cheese -clam FancyClamPizza FreshClam FrozenClam NYStyle ChicagoStyle Distinguished by namespaces in C#

  23. DI: How?

  24. DI: How? 1. Declare dependencies as constructor arguments of interface types

  25. DI: How? 1. Declare dependencies as constructor arguments of interface types 2. Register classes (components) in an Inversion-of-Control Container

  26. DI: How? 1. Declare dependencies as constructor arguments of interface types 2. Register classes (components) in an Inversion-of-Control Container 3. Resolve the top-level object from an interface through the Container

  27. 1. Dependencies namespace DITest { � public class FancyClamPizza: IClamPizza � { � � private IClam clam; � � private ICheese cheese; � � public FancyClamPizza (IClam clam, ICheese cheese) � � { � � � this.clam = clam; � � � this.cheese = cheese; � � } � � public String ClamType() { � � � return String.Format("fancy {0}",clam); � � } � � public String Describe() { � � � return String.Format("fancy clam pizza with {0} and {1}",ClamType(), cheese); � � } � } }

  28. 2. Registration namespace DITest { � public class IoCInstaller: IWindsorInstaller � { � � public void Install(IWindsorContainer container, IConfigurationStore store) � � { � � � container.Register(Classes � � � .FromThisAssembly() � � � .InNamespace("DITest.NYStyle") � � � .WithServiceAllInterfaces()); � � � container.Register (Classes � � � .FromThisAssembly() � � � .AllowMultipleMatches() � � � .InSameNamespaceAs<IoCInstaller>() � � � .WithServiceAllInterfaces()); � � } � } }

  29. 2. Registration namespace DITest { � public class IoCInstaller: IWindsorInstaller � { � � public void Install(IWindsorContainer container, IConfigurationStore store) � � { � � � container.Register(Classes � � � .FromThisAssembly() � � � .InNamespace("DITest.NYStyle") � � � .WithServiceAllInterfaces()); � � � container.Register (Classes � � � .FromThisAssembly() � � � .AllowMultipleMatches() � � � .InSameNamespaceAs<IoCInstaller>() � � � .WithServiceAllInterfaces()); � � } � } } Castle Windsor, http://www.castleproject.org

  30. 3. Resolution � � � var container = new WindsorContainer(); � � � // adds and configures all components using WindsorInstallers from executing assembly � � � container.Install(FromAssembly.This()); � � � // instantiate and configure root component and all its dependencies and their dependencies and... � � � var p = container.Resolve<ICheesePizza>(); � � � Console.WriteLine (p.Describe ());

  31. Demo

  32. Design principles • Encapsulate what varies • Program to an interface, not to an implementation • Favor composition over inheritance • Classes should be open for extension but closed for modification • Don’t call us, we’ll call you • Depend on abstractions, do not depend on concrete classes

  33. Composite

  34. Ok, now we’re done with menus, coffees and pizzas. Right?

  35. Ok, now we’re done with menus, coffees and pizzas. Right? Right?

  36. Ok, now we’re done with menus, coffees and pizzas. Right? Right? No.

  37. Ok, now we’re done with menus, coffees and pizzas. Right? Right? No. We need a desert menu!

  38. We need a coffee menu! Ok, now we’re done with menus, coffees and pizzas. Right? Right? No. We need a desert menu!

  39. Waiter

  40. Waiter printMenu()

  41. Waiter printMenu() Coffee menu Dark roast Tea Espresso Coffee

  42. Waiter printMenu() Breakfast menu Spam, Coffee Coffee Ham & Spam & Eggs & spam & menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee

  43. Waiter printMenu() Pizza Breakfast menu menu Spam, Coffee Clam Cheese Coffee Coffee Ham & Spam & Eggs & spam & menu Pizza Pizza menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee

  44. Waiter Diner printMenu() menu Pizza Breakfast menu menu Spam, Coffee Clam Cheese Coffee Coffee Ham & Spam & Eggs & spam & menu Pizza Pizza menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee

  45. Waiter print() Diner printMenu() menu Pizza Breakfast menu menu Spam, Coffee Clam Cheese Coffee Coffee Ham & Spam & Eggs & spam & menu Pizza Pizza menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee

  46. Waiter print() Diner printMenu() menu print() Pizza Breakfast menu menu Spam, Coffee Clam Cheese Coffee Coffee Ham & Spam & Eggs & spam & menu Pizza Pizza menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee

  47. Waiter print() Diner printMenu() menu print() Pizza Breakfast menu menu Spam, print() Coffee Clam Cheese Coffee Coffee Ham & Spam & Eggs & spam & menu Pizza Pizza menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee

  48. Waiter print() Diner printMenu() menu print() Pizza Breakfast menu menu Spam, print() Coffee Clam Cheese Coffee Coffee Ham & Spam & Eggs & spam & menu Pizza Pizza menu menu eggs eggs spam eggs Dark print() roast Tea Espresso Coffee

  49. � �

Recommend


More recommend