250 likes | 377 Views
Team 6 “The Unparseables” . Design Patterns . Chain of Responsibility Observer Flyweight. Chain of Responsibility. Overview A client sends a request to process data to a chain of program units that if possible, will handle the request.
E N D
Team 6 “The Unparseables” Design Patterns Chain of Responsibility Observer Flyweight
Chain of Responsibility • Overview • A client sends a request to process data to a chain of program units that if possible, will handle the request. • If the program unit or “handler” cannot handle the request, it will pass the request to the next “handler” in the chain.
Motivation • Chain the “handlers” and pass the request along the chain until an one of them is able to process it. • It is data-driven • Loosely coupled • Each object on the chain shares a common interface for handling requests and for accessing its successor on the chain.
Example (gravel filter) The finest gravel is collected in the sill
Problem There is a potential variable number of “handler” objects, and a stream of requests that must be handled. Efficiency could be a problem.
Sources http://userpages.umbc.edu/~tarr/dp/lectures/Chain-2pp.pdf http://www.codeproject.com/KB/architecture/Chain_of_Responsibility.aspx http://sourcemaking.com/design_patterns/chain_of_responsibility
Observer • Overview • Defines a one-to-many dependency between a subject object and any number of observer objects. • Has two parts and they are subject and observer. • The subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
Motivation • Encapsulate the core (or common or engine) components in a Subject abstraction, and the variable (or optional or user interface) components in an Observer hierarchy. • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Applicability: • When the abstraction has two aspects with one dependent on the other. • When the subject object doesn't know exactly how many observer objects it has. • When the subject object should be able to notify it's observer objects without knowing who these objects are.
Example The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically. Some auctions demonstrate this pattern.
import java.beans.PropertyChangeListener; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class MyModel { private List<Person> persons = new ArrayList<Person>(); private List<PropertyChangeListener> listener = new ArrayList<PropertyChangeListener>(); public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; notifyListeners(); } } public List<Person> getPersons() { return persons; } public MyModel() { // Just for testing we hard-code the persons here: persons.add(new Person("Lars", "Vogel")); persons.add(new Person("Jim", "Knopf")); } private void notifyListeners() { for (Iterator iterator = listener.iterator(); iterator.hasNext();) { PropertyChangeListener name = (PropertyChangeListener) iterator .next(); name.propertyChange(null); } } public void addChangeListener(PropertyChangeListenernewListener) { listener.add(newListener); } }
Problem In a complex system you want “maintain consistency between related objects”. This is not a trivial task, since coupling the objects together reduces reuse of each component. How can you maintain consistency, yet also maintain the loose coupling? • Solution If you make the related classes observers, who look at a common repository of data for their state, you can maintain consistency and no observer knows about the others. The subject has methods to attach, detach, and update the observers in the event that its state changes.
Sources • http://www.cs.clemson.edu/~malloy/courses/patterns/observer.html • http://www.skill-guru.com/blog/2011/01/09/observer-design-pattern/ • http://sourcemaking.com/design_patterns/observer
Flyweight • Overview • The Flyweight Design Pattern is useful when there is the need for many, many objects to exist that share some information. • Several thousand or even several hundred thousand objects might be needed, and this is usually very memory-consuming to keep track of. 18
Motivation • A large number of objects take up a large amount of space • Objects may contained shared intrinsic state that can be moved into shared objects • If all of the objects share some intrinsic, invariant information that is constant among all of them, it can be removed from each object, and referenced. This object that contains all of the intrinsic information is called a flyweight object. 19
Structure 20
Example The public switched telephone network is an example of a Flyweight. There are several resources such as dial tone generators, ringing generators, and digit receivers that must be shared between all subscribers. A subscriber is unaware of how many resources are in the pool when he or she lifts the handset to make a call. All that matters to subscribers is that a dial tone is provided, digits are received, and the call is completed. 21
Problem Designing objects down to the lowest levels of system “granularity” provides optimal flexibility, but can be unacceptably expensive in terms of performance and memory usage. • Discussion • The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost. • Each “flyweight” object is divided into two pieces: the state-dependent part, and the state-independent part. Intrinsic state is stored in the Flyweight object. • Extrinsic state is stored or computed by client objects, and passed to the Flyweight when its operations are invoked. 23
Sources http://www.exciton.cs.rice.edu/javaresources/designpatterns/flyweightpattern.htm http://sourcemaking.com/design_patterns/flyweight Design Patterns: Elements of Reusable Object-Oriented Software 24