Patterns – Day 9 Façade Composite Reminders: Faculty candidate talk Friday 4:20 PM O-267. Brian...

Preview:

Citation preview

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.

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.

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.

Questions, comments, rebuttals, etc.?

Façade Pattern

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

services from a complex subsystem.

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.

GoF Compiler Example

Metsker definition

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

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

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

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

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 …

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?

A Swing Façade: JOptionPane

JOptionPane Example

It is in the class examples directory.

• Other façade examples/comments?

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)

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.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.

• 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.

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.

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?

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?

Recommended