Chapter 8, Object Design Reuse and Patterns II
Chapter 8, Object Design Reuse and Patterns II
A Game: Get-15
Start with the nine numbers 1,2,3,4, 5, 6, 7, 8 and 9. You and your opponent take alternate turns, each taking a number Each number can be taken only once: If you opponent has selected a number, you cannot also take it. The first person to have any three numbers that total 15 wins the game. Example: You: Opponent:
1 6
5 9
3 7
8 2
Opponent Wins!
Characteristics of Get-15
Hard to play, The game is especially hard, if you are not allowed to write anything done. Why?
All the numbers need to be scanned to see if you have won/lost It is hard to see what the opponent will take if you take a certain number The choice of the number depends on all the previous numbers Not easy to devise an simple strategy
Source: http://boulter.com/ttt/index.cgi
A Draw Sitation
Winning Patterns
Tic-Tac-Toe is Easy
Why? Reduction of complexity through patterns and symmetries Patterns: Knowing the following two patterns, the player can anticipate the opponents move.
Symmetries: The player needs to remember only these three patterns to deal with 8 different game siuations
The
Any three numbers that solve the 15 problem also solve tic-tac-toe. Any tic-tac-toe solution is also a solution the 15 problem To see the relationship between the two games, we simply arrange the 9 digits into the following pattern
8 3 4
1 5 9
6 7 2
Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
You: Opponent:
1 6
8 3
1 5
6 7
3
4
5
9
7
2
11
During Object Modeling we do many transformations and changes to the object model. It is important to make sure the object design model stays simple! In the next two lectures we show how to use design patterns to keep system models simple.
12
Modeling Heuristics
Modeling must address our mental limitations:
Our short-term memory has only limited capacity (7+-2)
Reduce complexity
Use abstractions
Design Patterns
Usefulness of design patterns Design Pattern Categories
Finding Objects
15
Requirements Analysis
Start with Use Cases. Identify participating objects Textual analysis of flow of events (find nouns, verbs, ...) Extract application domain objects by interviewing client (application domain knowledge) Find objects by using general knowledge
System Design
Subsystem decomposition Try to identify layers and partitions
Object Design
Find additional objects by applying implementation domain knowledge
16
17
18
Models tree structures that represent part-whole hierarchies with arbitrary depth and width. The Composite Pattern lets client treat individual objects and compositions of these objects uniformly
Client Component
Leaf
Operation()
Composite
Operation() AddComponent RemoveComponent() GetChild()
Children
19
Software System:
Definition: A software system consists of subsystems which are either other subsystems or collection of classes Composite: Subsystem (A software system consists of subsystems which consists of subsystems , which consists of subsystems, which...) Leaf node: Class
Software Lifecycle:
Definition: The software lifecycle consists of a set of development activities which are either other actitivies or collection of tasks Composite: Activity (The software lifecycle consists of activities which consist of activities, which consist of activities, which....) Leaf node: Task
20
with a Composite
User
Software System
Class Subsystem
Children
21
Manager
Software Lifecycle
Task Activity
Children
22
*
Doors
*
Wheels
Battery Engine
School
Department
Pattern
Program
*
Block
Compound Statement
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java
Simple Statement
23
Line
Draw()
Circle
Draw()
Picture
Draw() Add(Graphic g) RemoveGraphic) GetChild(int)
Children
24
25
If the model is still too complex, we show the subclasses on a separate slide We make sure to use the name of the patterns
Object-Oriented Software Engineering: Using UML, Patterns, and Java 26
Composite Patterns
Schedule * Outcome * produces *
* Organizational responWork Unit * sible plays depends for Role Task Participant Staff
Work Product
Activity
Project Deliverable
Project Function
Department
Team
27
Exercise
Redraw the complete model for Project from your memory using the following knowledge
1. The key abstractions are task, schedule, and participant 2. Workproduct, Task and Participant are modeled with composite patterns, for example
Work Product
Adapter pattern
ClientInterface
Client
Request()
LegacyClass
ExistingRequest()
adaptee
Adapter
Delegation is used to bind an Adapter and an Adaptee Interface inheritance is use to specify the interface of the Adapter class. Target and Adaptee (usually called legacy system) pre-exist the Adapter. Target may be realized as an interface in Java.
Request()
29
Adapter Pattern
Convert the interface of a class into another interface clients expect. The adapter pattern lets classes work together that couldnt otherwise because of incompatible interfaces Used to provide a new interface to existing legacy components (Interface engineering, reengineering). Also known as a wrapper Two adapter patterns:
Class adapter:
Object adapter:
Object adapters are much more frequent. We will only cover object adapters (and call them therefore simply adapters)
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30
Bridge Pattern
Use a bridge to decouple an abstraction from its implementation so that the two can vary independently. (From [Gamma et al 1995]) Also know as a Handle/Body pattern. Allows different implementations of an interface to be decided upon dynamically.
31
Using a Bridge
The bridge pattern is used to provide multiple implementations under the same interface. Examples: Interface to a component that is incomplete, not yet known or unavailable during testing JAMES Project: if seat data is required to be read, but the seat is not yet implemented, known, or only available by a simulation, provide a bridge:
Seat (in Vehicle Subsystem) GetPosition() SetPosition()
imp
VIP
SeatImplementation
Stub Code
Bernd Bruegge & Allen H. Dutoit
AIMSeat
SARTSeat
32
Seat Implementation
public interface SeatImplementation { public int GetPosition(); public void SetPosition(int newPosition); } public class Stubcode implements SeatImplementation { public int GetPosition() { // stub code for GetPosition } ... } public class AimSeat implements SeatImplementation { public int GetPosition() { // actual call to the AIM simulation system } . } public class SARTSeat implements SeatImplementation { public int GetPosition() { // actual call to the SART seat simulator } ... }
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 33
Bridge Pattern
Client imp Abstra ct ion Ope ration() Imp->O perationImp(); Implementor OperationImpl()
34
Adapter vs Bridge
Similarities:
Both are used to hide the details of the underlying implementation.
Difference:
The adapter pattern is geared towards making unrelated components work together
A bridge, on the other hand, is used up-front in a design to let abstractions and implementations vary independently.
Green field engineering of an extensible system New beasts can be added to the object zoo, even if these are not known at analysis or system design time.
35
Facade Pattern
Provides a unified interface to a set of objects in a subsystem. A facade defines a higher-level interface that makes the subsystem easier to use (i.e. it abstracts out the gory details) Facades allow us to provide a closed architecture
36
Design Example
Subsystem 1 can look into the Subsystem 2 (vehicle subsystem) and call on any component or class operation at will. This is Ravioli Design Why is this good?
Efficiency
Subsystem 1
AIM
SA/RT
37
We can use design patterns to realize this subsystem structure Realization of the Interface Object: Facade
Provides the interface to the subsystem
The subsystem decides exactly how it is accessed. No need to worry about misuse by callers If a faade is used the subsystem can be used in an early integration test
We need to write only a driver
VIP Subsystem
Seat
Card
AIM
SA/RT
39
A facade pattern should be used by all subsystems in a software system. The faade defines all the services of the subsystem.
The facade will delegate requests to the appropriate components within the subsystem. Most of the time the faade does not need to be changed, when the component is changed,
What
41
42
43
44
45
46
Summary
These classes can be adapted and refined for the specific system under construction.
Customization of the system Reuse of existing solutions
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 48
Summary II
Composite Pattern:
Models trees with dynamic width and dynamic depth
Facade Pattern:
Interface to a subsystem closed vs open architecture
Adapter Pattern:
Interface to reality
Bridge Pattern:
Interface to reality and prepare for future
49
Additional Slides
50
Additional References
Design (This talk): E. Gamma et.al., Design Patterns, 1994. Analysis: M. Fowler, Analysis Patterns: Reusable Object Models, 1997 System design: F. Buschmann et. Al., Pattern-Oriented Software
Architecture: A System of Patterns, 1996 1997
Middleware: T. J. Mowbray & R. C. Malveau, CORBA Design Patterns, Process modeling S. W. Ambler, Process Patterns: Building Large-Scale
Systems Using Object Technology, 1998.
http://www.oose.globalse.org
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 51
What is this?
1.Nf3 d5 2.c4 c6 3.b3 Bf5 4.g3 Nf6 5.Bg2 Nbd7 6.Bb2 e6 7.OO Bd6 8.d3 O-O 9.Nbd2 e5 10.cxd5 cxd5 11.Rc1 Qe7 12.Rc2 a5 13.a4 h6 14.Qa1 Rfe8 15.Rfc1
This is a fianchetto! The fianchetto is one of the basic building-blocks of chess thinking.
52
Fianchetto (Reti-Lasker)
The diagram is from Reti-Lasker, New York 1924. We can see that Reti has allowed Lasker to occupy the centre but Rtei has fianchettoed both Bishops to hit back at this, and has even backed up his Bb2 with a Queen on a1!
Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 53
Never use implementation inheritance, always use interface inheritance A subclass should never hide operations implemented in a superclass If you are tempted to use implementation inheritance, use delegation instead
54
Component
getGraphics()
Text Component
Button
Label
Container
add(Component c) paint(Graphics g)
TextField
TextArea
55
Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software, Addison Wesley, 1995 Based on OMT Notation (a precursor to UML) Notational differences between the notation used by Gamma et al. and UML. In Gamma et al:
Attributes come after the Operations Associations are called acquaintances Multiplicities are shown as solid circles Dashed line : Instantiation Assocation (Class can instantiate objects of associated class) (In UML it denotes a dependency) UML Note is called Dogear box (connected by dashed line to class operation): Pseudo-code implementation of operation
56
Paradigms
57