![Page 1: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/1.jpg)
CS342: Software Design Oct 24, 2017
![Page 2: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/2.jpg)
Review of patterns
Factory pattern
Decorator pattern
Outline
![Page 3: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/3.jpg)
Talk only to your immediate friends..
Reduce interactions between objects
Avoid tight coupling between client and subsystem
Better structured and easier to read code
Applies to both OOP and SOA
Facade and Principle of least knowledge
![Page 4: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/4.jpg)
One to many dependency between objects
● When source/subject /publisher state changes, all dependents/observer /subscribers are notified and updated automatically
● There are many different ways to implement
Observer pattern: publish & subscribe
![Page 5: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/5.jpg)
Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.
Strategy pattern
![Page 6: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/6.jpg)
“Buzz words” that we have learnedKeep in mind those when you design your class
● Least knowledge
● Loosely coupled classes
● Program to an interface, not implementation
● Identify and encapsulate behaviors that vary (fly, quack)
What if the instantiation behaviors vary?
● Introducing the Factory pattern!
![Page 7: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/7.jpg)
● Programming to concrete classes, hard to change (add, remove pizza types)
● Based on the string parameter, create different types of pizza object
● All subclasses have the same “prepare”, “bake”, “cut”, “box” method
● Constructor behavior varies
Pizza ordering application
![Page 8: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/8.jpg)
● The object creation: factory
● Factories handle the details of object creation
Encapsulate object creation
![Page 9: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/9.jpg)
● Are we just moving coconuts around? What’s the benefit of this?
Pizza factory
![Page 10: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/10.jpg)
Simple FactorySimple factory isn’t a real pattern.
And it’s not “Factory pattern”
![Page 11: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/11.jpg)
Your pizza business is expanding
At the same time, you want to make sure the stores follow the same procedure in the process: bake, cut, box…
You need a framework to scaffold the process while allow flexibility
![Page 12: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/12.jpg)
Pizza store framework
What do we do with an abstract class? We extend it! Now pay attention to “orderPizza” method. It’s defined in superclass, but doesn’t know which sub-class will run it
![Page 13: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/13.jpg)
Once a NYPizzaStore or ChicagoPizzaStore object is instantiated, the “orderPizza” will be executed the same without knowing about the sub-class
NY pizza store
![Page 14: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/14.jpg)
Actually this is the beauty of using abstraction… Remember “Least Knowledge” Principle?
We have talked about a lot about store, what about pizza?
![Page 15: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/15.jpg)
PizzaStore class isn’t aware of NYStyleCheesePizza or ChicagoStyleCheesePizza, but that doesn’t prevent it from doing its job
Chicago and NY Pizza
![Page 16: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/16.jpg)
Test the code
![Page 17: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/17.jpg)
Factory (creator) classes
![Page 18: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/18.jpg)
Product class
![Page 19: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/19.jpg)
Parallel class hierarchy
![Page 20: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/20.jpg)
Factory pattern definition
The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses
![Page 21: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/21.jpg)
“Straightforward” implementation
![Page 22: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/22.jpg)
Depend upon abstractions. Do not depend upon concrete classes.
● Further step beyond “Program to an interface, not to an implementation”
● High-level components should not depend on low-level components
● Both high level and low level components should depend on abstractions
● Both PizzaStore and concrete pizza classes depend on Pizza abstraction
The Dependency Inversion Principle
![Page 23: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/23.jpg)
Homework: Add ingredients to pizza store
![Page 24: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/24.jpg)
Ingredient factory
![Page 25: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/25.jpg)
Now the prepare step will be different, so make it abstract...
![Page 26: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/26.jpg)
NYPizzaStore
![Page 27: CS342: Software Design - University of Illinois at Chicago · CS342: Software Design Oct 24, 2017. Review of patterns Factory pattern Decorator pattern Outline. Talk only to your](https://reader035.vdocument.in/reader035/viewer/2022071017/5fd0914443ac717d0f13571c/html5/thumbnails/27.jpg)
Abstract Factory Pattern: an interface for creating families of related or dependent objects without specifying their concrete classes.