SLIDE 1 TDDB84: Lecture 4
Abstract Factory, Dependency Injection, Composite
SLIDE 2
Abstract factory
SLIDE 3 NY Chicago
Fresh Clam Mozzarella Cheese Thin Crust Dough Frozen Clam Parmesan Cheese Thick Crust Dough
Ingredients Pizza Store Clients
I Want a Cheese Pizza
SLIDE 4 NY Chicago
Fresh Clam Mozzarella Cheese Thin Crust Dough Frozen Clam Parmesan Cheese Thick Crust Dough
Ingredients Pizza Store Clients
I Want a Cheese Pizza
SLIDE 5 NY Chicago
Fresh Clam Mozzarella Cheese Thin Crust Dough Frozen Clam Parmesan Cheese Thick Crust Dough
Ingredients Pizza Store Clients
I Want a Cheese Pizza
SLIDE 6
SLIDE 7
SLIDE 8
SLIDE 9
SLIDE 10
SLIDE 11
SLIDE 12
Abstract Factory
SLIDE 13
Abstract Factory Concrete Factory
SLIDE 14
Abstract Products Abstract Factory Concrete Factory
SLIDE 15
SLIDE 16
Clients
SLIDE 17
Abstract factory: consequences
SLIDE 18 Abstract factory: consequences
+Isolates clients from
concrete dependencies
SLIDE 19 Abstract factory: consequences
+Isolates clients from
concrete dependencies
+Makes interchanging
families of products easier
SLIDE 20
SLIDE 21
Strategy
SLIDE 22 Strategy
- When related classes only
differ in behavior
- You need different variants
- f an algorithm
- An algorithm uses data the
clients don’t need to know
- A class uses conditionals
for selecting behavior
SLIDE 23 Strategy
- When related classes only
differ in behavior
- You need different variants
- f an algorithm
- An algorithm uses data the
clients don’t need to know
- A class uses conditionals
for selecting behavior
Abstract Factory
independent of how its products are created
configured with one of multiple families of products
- You want to provide a class
library of products, and only expose their interfaces
SLIDE 24 Strategy
- When related classes only
differ in behavior
- You need different variants
- f an algorithm
- An algorithm uses data the
clients don’t need to know
- A class uses conditionals
for selecting behavior
Abstract Factory
independent of how its products are created
configured with one of multiple families of products
- You want to provide a class
library of products, and only expose their interfaces
Behavioral
SLIDE 25 Strategy
- When related classes only
differ in behavior
- You need different variants
- f an algorithm
- An algorithm uses data the
clients don’t need to know
- A class uses conditionals
for selecting behavior
Abstract Factory
independent of how its products are created
configured with one of multiple families of products
- You want to provide a class
library of products, and only expose their interfaces
Behavioral Creational
SLIDE 26 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
SLIDE 27 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
SLIDE 28 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
SLIDE 29
Dependency Injection
SLIDE 30 Distinguished by namespaces in C#
ICheesePizza <<ICheese> > MozzarellaCheese ParmesanCheese <<IClam>> FreshClam FrozenClam
IClamPizza FancyClamPizza StandardCheesePizza NYStyle ChicagoStyle
SLIDE 31
DI: How?
SLIDE 32
- 1. Declare dependencies as constructor
arguments of interface types
DI: How?
SLIDE 33
- 1. Declare dependencies as constructor
arguments of interface types
- 2. Register classes (components) in an
Inversion-of-Control Container
DI: How?
SLIDE 34
- 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
DI: How?
SLIDE 35
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);
- }
- }
}
SLIDE 36
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());
- }
- }
}
SLIDE 37
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
SLIDE 38
- 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 ());
SLIDE 39
Demo
SLIDE 40 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
SLIDE 41
Composite
SLIDE 42
SLIDE 43
Ok, now we’re done with menus, coffees and pizzas. Right?
SLIDE 44
Ok, now we’re done with menus, coffees and pizzas. Right? Right?
SLIDE 45
Ok, now we’re done with menus, coffees and pizzas. Right? Right? No.
SLIDE 46 We need a desert menu!
Ok, now we’re done with menus, coffees and pizzas. Right? Right? No.
SLIDE 47 We need a desert menu! We need a coffee menu!
Ok, now we’re done with menus, coffees and pizzas. Right? Right? No.
SLIDE 48
SLIDE 50 Waiter
printMenu()
SLIDE 51 Coffee menu Dark roast Coffee Tea Espresso
Waiter
printMenu()
SLIDE 52 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso
Waiter
printMenu()
SLIDE 53 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso Pizza menu Coffee menu Clam Pizza Cheese Pizza
Waiter
printMenu()
SLIDE 54 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso Pizza menu Coffee menu Clam Pizza Cheese Pizza Diner menu
Waiter
printMenu()
SLIDE 55 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso Pizza menu Coffee menu Clam Pizza Cheese Pizza Diner menu
Waiter
printMenu() print()
SLIDE 56 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso Pizza menu Coffee menu Clam Pizza Cheese Pizza Diner menu
Waiter
printMenu() print() print()
SLIDE 57 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso Pizza menu Coffee menu Clam Pizza Cheese Pizza Diner menu
Waiter
printMenu() print() print() print()
SLIDE 58 Breakfast menu Coffee menu Ham & eggs Spam & eggs Eggs & spam Spam, spam & eggs Coffee menu Dark roast Coffee Tea Espresso Pizza menu Coffee menu Clam Pizza Cheese Pizza Diner menu
Waiter
printMenu() print() print() print() print()
SLIDE 59
SLIDE 60
SLIDE 61
Client
SLIDE 62
Client Component
SLIDE 63
Client Component Composite
SLIDE 64
Client Component Composite Leaf
SLIDE 65
Composite: consequences
SLIDE 66 Composite: consequences
+Allow us to treat
composite objects and individual objects uniformly
SLIDE 67 Composite: consequences
+Allow us to treat
composite objects and individual objects uniformly
+Allows arbitrarily
complex trees
SLIDE 68 Composite: consequences
classes that violate the principle of a single responsibility
+Allow us to treat
composite objects and individual objects uniformly
+Allows arbitrarily
complex trees
SLIDE 69 Composite: consequences
classes that violate the principle of a single responsibility
rely on components to implement all methods
+Allow us to treat
composite objects and individual objects uniformly
+Allows arbitrarily
complex trees
SLIDE 70 Exam question
1. What are the relationships between the Strategy and Flyweight patterns? a) They both concern decoupling objects from their dependencies b) Using a Flyweight pattern may also require the Strategy pattern c) Using the Strategy pattern may also result in using the Flyweight pattern d) The Strategy pattern, if implemented with the use of type template parameters, does not necessitate the Flyweight pattern e) You often implement the Flyweight pattern by using the Strategy pattern for object creation f) The Flyweight pattern is a simplified form of the Strategy pattern
SLIDE 71 Lecture 4 Lecture 5
Proxy Singleton
SLIDE 72 Lecture 4 Lecture 5
Proxy Singleton
SLIDE 73 Lecture 4 Lecture 5
Proxy Dynamic proxies in C# & Java Singleton
SLIDE 74 Lecture 4 Lecture 5
Proxy Dynamic proxies in C# & Java Singleton Classes ARE objects, and each object can have a class (in Ruby)
SLIDE 75 Lecture 4 Lecture 5
Proxy Dynamic proxies in C# & Java Singleton Classes ARE objects, and each object can have a class (in Ruby)
Yes, it may be on the exam