Documente Academic
Documente Profesional
Documente Cultură
E.Ycel
So far...
Analysis:
application objects
System design:
off-the-shelf components
Object design:
We close the gap between application objects and off-theshelf components the process of adding details to the requirements analysis and making implementation decisions
Object Design
Use cases, functional and dynamic model deliver operations for object model
The object designer must choose among different ways to implement the analysis model
with the goal to minimize execution time, memory and other measures of cost. Iterates on the models, in particular the object model and refine the models
Object Design:
Problem
Application objects
Analysis
Design
- Object Design
Existing Machine
Use of inheritance Off-the-shelf components and additional solution objects Design patterns Chapter 8 Describes precisely each class interface Chapter 9 Transforms the object design model to improve its understandability and extensibility Chapter 10
2. Interface specification
Transforms the object design model to address performance criteria such as response time or memory utilization. Chapter 10
Specification
Reuse
Identifying components
Specifying visibility Adjusting components Specifying types & signatures Identifying patterns Specifying constraints
Specifying exceptions
Adjusting patterns
Collapsing classes
Realizing associations
Incident Report
Text box
Object Design (Language of Solution Domain)
Menu
Scrollbar
Application objects and Solution objects Specification Inheritance and Implementation Inheritance Delegation Delegation and Inheritance in Design Patterns
have no counterpart in the application domain e.g. persistent data stores, user interface objects, etc.
Modeling of the real world leads to a system that reflects todays realities but not necessarily tomorrows. There is a need for reusable and flexible designs Design knowledge such as the adapter pattern complements application domain knowledge and solution domain knowledge.
Reuse of Code
The list offers the operations Insert(), Find(), Delete() The stack needs the operations Push(), Pop() and Top() Can I reuse the existing list?
I am an employee in a company that builds cars with expensive car stereo systems
Can I add it as a subsystem to my e-mail program which I purchased from a vendor (replacing the vendor-supplied addressbook)? Can I reuse this class in the billing software of my dealer management system?
Develop new functionality Reuse knowledge from previous experience Reuse functionality already available New functionality is obtained by aggregation The new object with more functionality is an aggregation of existing objects New functionality is obtained by inheritance
Main goal:
Specialization:
Generalization:
Specification Inheritance:
Implementation Inheritance:
Discovering Inheritance
To discover inheritance associations, we can proceed in two ways, which we call specialization and generalization Generalization: the discovery of an inheritance relationship between two classes, where the subclass is discovered first. Specialization: the discovery of an inheritance relationship between two classes, where the super class is discovered first.
CoffeeMachine
After the generalization there is often an opportunity for Restructuring: Why dont we move the methods totalReceipts, collectMoney(), makeChange(), dispenseBeverage() into the super class?
CoffeeMachine totalReceipts numberOfCups coffeeMix collectMoney() makeChange() heatWater() dispenseBeverage() addSugar() addCreamer()
CoffeeMachine
numberOfCups coffeeMix heatWater() addSugar() addCreamer()
An Example of a Specialization
VendingMachine totalReceipts collectMoney() makeChange() dispenseBeverage()
CandyMachine is a new product and designed as a subclass of the superclass VendingMachine A change of names might now be useful: dispenseItem() instead of dispenseBeverage() and dispenseSnack()
SodaMachine
cansOfBeer cansOfCola chill() dispenseItem()
Inheritance
Analysis activity
Taxonomy Inheritance for Reuse
Object Design
Specification Inheritance
Implementation Inheritance
Implementation inheritance
Extend an applications functionality by reusing functionality from the super class Inherit from an existing class with some or all operations already implemented
Specification Inheritance
Inherit from a specification The specification is an abstract class with all operations specified, but not yet implemented.
A very similar class is already implemented that does almost the same as the desired class implementation List Example:
I have a List class, I need a Stack class How about subclassing the Stack class from the List class and implementing Push(), Pop(), Top() with Add() and Remove()?
Add() Remove()
Already implemented
Stack
Inheritance: Extending a Base class by a new operation or overriding an operation. Delegation: Catching an operation and sending it to another object. Which of the following models is better?
List
+Add() +Remove()
Stack
+Push() +Pop() +Top()
List
Add() Remove()
Stack
+Push() +Pop() +Top()
Delegation Delegation is a way of making composition as powerful for reuse as inheritance In delegation two objects are involved in handling a request from a Client
The Receiver object delegates operations to the
Delegate object
The Receiver object makes sure, that the Client does not
Client
calls
Receiver delegates to
Delegate
Design patterns
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!
Design patterns helps keep system models simple.
Finding Objects
Object identification
Subsystem Identification
Additional solution objects
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 Extract objects from Use Case scenarios (dynamic model)
System Design
Object Design
The recurring aspects of designs are called design patterns [Gamma et al 1995]. A pattern is the outline of a reusable solution to a general problem encountered in a particular context. It describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same twice. Many of them have been systematically documented for all software developers to use.
A software system consists of subsystems which are either other subsystems or collection of classes
The software lifecycle consists of a set of development activities which are either other actitivies or collection of tasks
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
User
Software System
Class
Subsystem
children
Line
Draw()
Circle
Draw()
Picture
Draw() Add(Graphic g) RemoveGraphic) GetChild(int)
Children
We do not simply use a picture from the CASE tool and dump it in front of the user The key is to navigate through the model so the user can follow it Start with the key abstractions Then decorate the model with additional classes
If the model is still too complex, we show subclasses on a separate slide We make sure to use the name of the patterns.
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
Inheritance or Delegation
Adapter Pattern
Client ClientInterface Request() LegacyClass ExistingRequest()
adaptee Inheritance
Adapter
Request()
Delegation
Adapter behaves as expected by Client The adapter pattern uses inheritance as well as delegation: Interface inheritance is used to specify the interface of the Adapter class. Delegation is used to bind the Adapter and the Adaptee Adapter works with LegacyClass and all its subclasses A new Adapter needs to be written for each specialization
Adapter Pattern
The adapter pattern lets classes work together that couldnt otherwise because of incompatible interfaces
Convert the interface of a class into another interface expected by a client class. Used to provide a new interface to existing legacy components (Interface engineering, reengineering). Class adapter:
Uses multiple inheritance to adapt one interface to another Uses single inheritance and delegation
Object adapter:
Object adapters are much more frequent. We cover only object adapters (and call them adapters).
Bridge Pattern
Use a bridge to decouple an abstraction from its implementation so that the two can vary independently
Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy. Beyond encapsulation, to insulation
Also know as a Handle/Body pattern Allows different implementations of an interface to be decided upon dynamically.
Bridge Pattern
Decouples an abstraction from its implementation so that the two can vary independently This allows to bind one from many different implementations of an interface to a client dynamically Design decision that can be realized any time during the runtime of the system
However, usually the binding occurs at start up time of the system (e.g. in the constructor of the interface class)
Using a Bridge
The bridge pattern can be used to provide multiple implementations under the same interface
Adapter vs Bridge
Similarities:
Both are used to hide the details of the underlying implementation. The adapter pattern is geared towards making unrelated components work together
Difference:
Applied to systems after theyre designed (reengineering, interface engineering). Inheritance followed by delegation
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. Delegation followed by inheritance
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
Design Example
Subsystem 1
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 Cant expect the caller to understand how the subsystem works or the complex relationships within the subsystem. We can be assured that the subsystem will be misused, leading to non-portable code
AIM
SA/RT
an interface object a set of application domain objects (entity objects) modeling real entities or existing systems
We can use design patterns to realize this subsystem structure Realization of the Interface Object: Facade
Provides the interface to the subsystem Provides the interface to existing system (legacy system) The existing system is not necessarily object-oriented!
A faade should be offered by all subsystems in a software system who provides services
The faade delegates requests to the appropriate components within the subsystem. The faade usually does not have to be changed, when the components are changed
Example: A smart card software system should use an adapter for a smart card reader from a specific manufacturer
where the full set of objects is not completely known at analysis or design time. when a subsystem or component must be replaced later after the system has been deployed and client programs use it in the field.
Summary
such as separating an interface from a number of alternate implementations wrapping around a set of legacy classes protecting a caller from changes associated with specific platforms uses delegation and inheritance provides a modifiable design solution
These classes can be adapted and refined for the specific system under construction
The Proxy object is a local representative for an object in a different address space Good if information does not change too often Object is too expensive to create or too expensive to download. Good if the real object is not accessed too often
The proxy object provides protection for the real object Good when different actors should have different access and viewing rights for the same object
You want to build a user interface You want to provide menus You want to make the menus reusable across many applications
The applications only know what has to be done when a command from the menu is selected You dont want to hardcode the menu commands for the various applications
Such a user interface can easily be implemented with the Command Pattern.
The command pattern can be nicely used to decouple boundary objects from control objects:
Boundary objects such as menu items and buttons, send messages to the command objects (I.e. the control objects) Only the command objects modify entity objects
When the user interface is changed (for example, a menu bar is replaced by a tool bar), only the boundary objects are modified. Command pattern is often used in a Model/View/Controller software architecture.
parameterize clients with different requests queue or log requests support undoable operations
Uses:
Portfolio * Stock
Problem:
Requirements:
The system should maintain consistency across the (redundant) views, whenever the state of the observed object changes The system design should be highly extensible
Observer
*
update()
ConcreteSubject
state getState() setState()
ConcreteObserver
observeState
update()
The Subject (Publisher) represents the entity object Observers (Subscribers) attach to the Subject by calling subscribe() Each Observer has a different view of the state of the entity object
The state is contained in the subclass ConcreteSubject The state can be obtained and set by subclasses of type ConcreteObserver.
Observer Pattern
Connects the state of an observed object, the subject with many observing objects, the observers Maintaining consistency across redundant states Optimizing a batch of changes to maintain consistency Push Notification: Every time the state of the subject changes, all the observers are notified of the change
Usage:
Push-Update Notification: The subject also sends the state that has been changed to the observers
Pull Notification: An observer inquires about the state the of the subject
Strategy Pattern
We can switch between the algorithms at run time Different collision strategies for objects in video games Parsing a set of tokens into an abstract syntax tree (Bottom up, top down) Sorting a list of customers (Bubble sort, mergesort, quicksort)
Examples of tasks:
If we need a new algorithm, we can add it without disturbing the application or the other algorithms.
Strategy Pattern
Policy
Context
ContextInterface()
Strategy
AlgorithmInterface
ConcreteStrategyA
AlgorithmInterface()
ConcreteStrategyB
AlgorithmInterface()
ConcreteStrategyC
AlgorithmInterface()
SortInterface
Sort()
BubbleSort
Sort() Sort()
QuickSort
Sort()
MergeSort
Consider a user interface toolkit that supports multiple looks and feel standards for different operating systems:
How can you write a single user interface and make it portable across the different look and feel standards for these window managers?
Consider a facility management system for an intelligent house that supports different control systems:
How can you write a single control system that is independent from the manufacturer?
Abstract Factory
AbstractFactory Client CreateProductA CreateProductB ProductA1 ProductA2 AbstractProductA
ConcreteFactory1
AbstractProductB
CreateProductA CreateProductB
ProductB1 ConcreteFactory2 CreateProductA CreateProductB ProductB2
Initiation Assocation: Class ConcreteFactory2 initiates the associated classes ProductB2 and ProductA2
Independence from Initialization or Representation Manufacturer Independence Constraints on related products Cope with upcoming change
Match
Statistics
TTTMatch
ChessMatch
TTTStats
ChessStats
Text: must interface to several systems, some of them to be developed in the future, an early prototype must be demonstrated
=>Bridge Pattern
Summary
Realize new functionality from old functionality, Provide flexibility and extensibility
Conclusion
Design patterns
provide solutions to common problems lead to extensible models and code can be used as is or as examples of interface inheritance and delegation apply the same principles to structure and to behavior
Pattern-oriented development