cs342: software design - university of illinois at chicago · cs342: software design oct 24, 2017....

27
CS342: Software Design Oct 24, 2017

Upload: others

Post on 22-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

“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

● 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

● 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

● 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

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

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

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

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

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

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

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

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

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

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

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

“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

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

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

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

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

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

Abstract Factory Pattern: an interface for creating families of related or dependent objects without specifying their concrete classes.