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 Crust Dough
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
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
Abstract Factory
Concrete Factory Abstract Factory
Concrete Factory Abstract Factory Abstract Products
Clients
Abstract factory: consequences
Abstract factory: consequences + Isolates clients from concrete dependencies
Abstract factory: consequences + Isolates clients from concrete dependencies + Makes interchanging families of products easier
Strategy
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
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
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
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
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
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
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
Dependency Injection
ICheesePizza <<ICheese> -cheese > StandardCheesePizza MozzarellaCheese ParmesanCheese IClamPizza <<IClam>> -cheese -clam FancyClamPizza FreshClam FrozenClam NYStyle ChicagoStyle Distinguished by namespaces in C#
DI: How?
DI: How? 1. Declare dependencies as constructor arguments of interface types
DI: How? 1. Declare dependencies as constructor arguments of interface types 2. Register classes (components) in an Inversion-of-Control Container
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
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); � � } � } }
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()); � � } � } }
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
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 ());
Demo
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
Composite
Ok, now we’re done with menus, coffees and pizzas. Right?
Ok, now we’re done with menus, coffees and pizzas. Right? Right?
Ok, now we’re done with menus, coffees and pizzas. Right? Right? No.
Ok, now we’re done with menus, coffees and pizzas. Right? Right? No. We need a desert menu!
We need a coffee menu! Ok, now we’re done with menus, coffees and pizzas. Right? Right? No. We need a desert menu!
Waiter
Waiter printMenu()
Waiter printMenu() Coffee menu Dark roast Tea Espresso Coffee
Waiter printMenu() Breakfast menu Spam, Coffee Coffee Ham & Spam & Eggs & spam & menu menu eggs eggs spam eggs Dark roast Tea Espresso Coffee
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
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
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
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
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
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
� �
Recommend
More recommend