an object's behavior depends on its current state. operations have large, multipart conditional...

Download An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state

If you can't read please download the document

Upload: amie-henry

Post on 19-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Gumball machine

TRANSCRIPT

An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state. Gumball machine Telephone: State: onhook, offhook, inuse Action: pickup, dialing, hangup States Action: Call/ /store front Put everything together Put in the oven Take out of oven Box it Amazon order State: Order received Pending Ship Now Delivered Action: A buyer checkout Process order information and packaging Send to UPS for shipping UPS drop off the order Games: States: standing, jumping, ducking, and diving. Actions: bbutton presses and releases. Car: Gumball machine State Machines 101 Managing States Managing actions Creating the implementation Source code New Design State Implementation of Gumball machine Look at Eclipse code State Pattern defined The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. State Pattern Class Diagram state Adding winner state Look at source code Comparison of patterns Summary so far.. OO Basics Abstraction Encapsulation Inheritance Polymorphism OO Principles Encapsulate what varies Favor composition over inheritance Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact Classes should be open for extension but closed for modification. Depend on abstracts. Do not depend on concrete classes. Only talk to your friends Hollywood principles: dont call us, we will call you. Depend on abstracts. Do not depend on concrete classes. A class should have only one reason to change. Summary so far OO Patterns Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Decorator Pattern attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality Singleton Pattern ensure a class only has one instance, and provide a global point of access to it The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldnt otherwise because of incompatible interfaces. The Faade Pattern provides a unified interface to a set of interfaces in a subsystem. Faade defines a higher level interface that makes the subsystem easier to use. The Template Pattern defines steps of an algorithm. Subclasses cannot change the algorithm (final). It facilitates code reuse. The Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to the subclasses. The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.