23
Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday! Read chapters 6-9 of Metsker.

Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Patterns – Day 9Façade Composite

Reminders: Faculty candidate talk Friday 4:20 PM O-267.

Brian Postow: Games and Complexity Theory

Another talk on Monday!

Read chapters 6-9 of Metsker.

Page 2: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Student-led discussions on specific patterns

• Give the basic idea of what the pattern is about. (From different sources if possible)

• Examples.• Metsker’s examples and challenges

– As we go through the patterns, we also want to understand the oozinoz framework, because examples in later patterns might depend on them.

– The challenge problems often bring out interesting (and sometimes difficult) issues.

Page 3: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Presentations happening soonWe’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.

Page 4: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Questions, comments, rebuttals, etc.?

Page 5: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Façade Pattern

• What is it? What is its purpose?• Purpose: To provide a simple interface to

services from a complex subsystem.

Page 6: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

From wikipedia

• There may be a large set of objects with broad uses that are used in conjunction to perform a few functions for client objects.

• The façade pattern provides a simplified interface to achieve only those functions needed without having to reference many different, complicated interfaces.

• The result is a simple feel to a complicated process.

Page 7: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

GoF Compiler Example

Page 8: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Metsker definition

• a facade is a class with methods that make it easy to use the classes in a subsystem.

Page 9: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Metsker example – dud rockets

FlightPanel_1 does too many things:

•Window for displaying picture

•Trajectory calculations

•Application class’

C:\Documents and Settings\anderson\My Documents\Courses\Patterns\oozinoz\code\com\oozinoz\applications\FLightPanel_1.java

Page 10: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Complete the diagram in Figure 4.4 to show the code for FlightPanel_1 refactored into three classes: a FlightPath class, a reduced FlightPanel class that draws a flight path, and a SwingFacade class. Provide the facade with the utility methods that appeared in the initial FlightPanel_1 class. Also provide the facade with a method to launch a frame.

Challenge 4.1

Page 11: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Complete the diagram in Figure 4.4 to show the code for FlightPanel_1 refactored into three classes: a FlightPath class, a reduced FlightPanel class that draws a flight path, and a SwingFacade class. Provide the facade with the utility methods that appeared in the initial FlightPanel_1 class. Also provide the facade with a method to launch a frame.

Challenge 4.1-solution

Page 12: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

A problem

• A beginning Java student wants to run a program that requires a line or two of interactive user input.

• We can give the magic incantations:– BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );

– String oneLine;while( ( oneLine = in.readLine( ) ) != null)

• but this requires a lot of explanation that the student doesn’t need yet and is probably not ready for.

• or we can …

Page 13: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Consider this example

• Run this example:– c:\Documents and Settings\anderson\My

Documents\courses\Patterns\Lectures\Facade\facade

– Which classes are involved?– Does Swing provide a façade for this?

Page 14: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

A Swing Façade: JOptionPane

Page 15: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

JOptionPane Example

It is in the class examples directory.

Page 16: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

• Other façade examples/comments?

Page 17: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

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 leaf

• Examples:– (fill then in)

Page 18: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

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.

Page 19: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

CHALLENGE 5.1(Paraphrased) In the main Swing composite example (Component and Container), why are these abstract classes instead of interfaces?

Page 20: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

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.

• 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.2Write the code for the getMachineCount() methods implemented by Machine and by MachineComposite.

Page 21: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Additional methods for Machines and MachineComponents

Method Behavior

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.3For each method declared by MachineComponent, give recursive definitions for MachineComposite and nonrecursive definitions for Machine.

Page 22: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Non-tree-structured layouts

No cycles, but not a tree. What’s the common name for this kind of structure?

CHALLENGE 5.4 What does the following program print out?

Page 23: Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian Postow: Games and Complexity Theory Another talk on Monday!

Detecting “non-treeness”

CHALLENGE 5.5 Write the code for MachineComposite.isTree(Set s) .

Does that code work if there are cycles in the graph?