Patterns – Day 10 Composite Responsibility Reminder: Faculty candidate talk Today 4:20 PM O-267. Salman Azhar: Optimal Prediction via Data Compression Read chapters 6-11 of Metsker.
Presentations happening soon We’re doing some chapters out of order, so that I don’t do too many in a row. Not ideal, but the best I can do. Be ready on your “earliest day”. It is possible that it will happen later. It is possible that it will be split.
Questions, comments, rebuttals, etc.?
Composite Pattern Metsker definition: –A composite is a group of objects in which some objects may contain others; thus, one object may represent groups, and another may represent an individual item, or leafcompositeleaf Examples: –(fill then in)
Advantages of Composite Groups can contain groups or individuals. There can be common behaviors for groups and individuals. Remember slist-recur from PLC? So a group and an individual can present the same interface.
CHALLENGE 5.1CHALLENGE 5.1(Paraphrased) In the main Swing composite example (Component and Container), why are these abstract classes instead of interfaces?
Challenge 5.2 The factory is composed of bays; each bay has one or more manufacturing lines; a line is a collection of machines that collaboratively produce material to meet a schedule. The developers at Oozinoz have modeled this composition from the problem domain with the class structure shown in Figure 5.2.Figure 5.2 As the figure shows, the getMachineCount() behavior applies to both individual machines and collections of machines and returns the number of machines in any given component. CHALLENGE 5.2 CHALLENGE 5.2 Write the code for the getMachineCount() methods implemented by Machine and by MachineComposite.
Additional methods for Machines and MachineComponents MethodBehavior isCompletelyUp() Indicates whether all the machines in a component are in an "up" state stopAll() Directs all the machines in a component to stop processing getOwners() Returns a set of process engineers responsible for the machines in a component getMaterial() Return all the in-process material in a machine component CHALLENGE 5.3 CHALLENGE 5.3 For each method declared by MachineComponent, give recursive definitions for MachineComposite and nonrecursive definitions for Machine.
Non-tree-structured layouts No cycles, but not a tree. What’s the common name for this kind of structure? CHALLENGE 5.4CHALLENGE 5.4 What does the following program print?
Detecting “non-treeness” CHALLENGE 5.5CHALLENGE 5.5 Write the code for MachineComposite’s isTree(Set s) method. Does that code work if there are cycles in the graph?
Responsibility In one sense, responsibility is what OO design and programming are all about. Who is responsible for what? So most patterns will say something about responsibility. In Metsker’s classification, seven of the patterns particularly deal with this.
CHALLENGE 7.1 CHALLENGE 7.1 The class structure shown here has at least ten questionable assignments of responsibility. Circle as many problems as you can find; for four of these points, write a statement of what is wrong.
Responsibility Patterns If you intend toApply the pattern Centralize responsibility in a single instance of a class SingletonSingleton (Chapter 8)Chapter 8 Decouple an object from awareness of which other objects depend on it ObserverObserver (Chapter 9)Chapter 9 Centralize responsibility in a class that oversees how a set of other objects interact MediatorMediator (Chapter 10)Chapter 10 Let an object act on behalf of another object ProxyProxy (Chapter 11)Chapter 11 Allow a request to escalate up a chain of objects until one handles it Chain of Responsibility Chain of Responsibility (Chapter 12)Chapter 12 Centralize responsibility in shared, fine- grained objects FlyweightFlyweight (Chapter 13)Chapter 13