Bernd Bruegge Allen H Dutoit 1 ObjectOriented Software
Bernd Bruegge & Allen H. Dutoit 1 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Midterm Q 2 common mistakes • Astronomical facts: • I am not penalizing much; except very obvious mistakes • “Galaxy is part of Planetary. System” • UML mistakes: • Missing and/or incorrect multiplicities • Missing and/or incorrect hierarchy types • Mostly notational mistakes vs. • Only arrow, no triangle/diamond and no label on edge • Triangles and diamonds attached to the wrong class even if the hierarchy type is correct • Missing classes: a few people missed Earth Bernd Bruegge & Allen H. Dutoit 2 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Midterm Q 2 common mistakes • Logic errors: • “Star is part of Galaxy” , “Star is part of Planetary. System”, “Planetary. System is part of Galaxy” • Although syntactically correct, it’s also redundant • Violates decomposition/hierarchy • Assign one class as part of only one other class • “Planetary. System is part of Galaxy” • “Star is part of Galaxy” Bernd Bruegge & Allen H. Dutoit 3 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Q 2 Bernd Bruegge & Allen H. Dutoit 4 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Announcements / reminders • Design reports: July 17 th • Feedback on analysis reports will be ready within this week. Email kemalcagri 67@gmail if you have questions • Quiz 3: July 18 th • Final: July 31 st • 09: 00 to 12: 00 (we might change as 10: 00 to 12: 00) Bernd Bruegge & Allen H. Dutoit 5 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Chapter 8, Object Design: Reuse and Patterns
Object Design • Purpose of object design: • Prepare for the implementation of the system model based on design decisions • Transform the system model (optimize it) • Investigate alternative ways to implement the system model • Use design goals: minimize execution time, memory and other measures of cost. • Object design serves as the basis of implementation. Bernd Bruegge & Allen H. Dutoit 7 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Terminology: Naming of Design Activities Methodology: Object-oriented software engineering (OOSE) • System Design • Decomposition into subsystems, etc • Object Design • Data structures and algorithms chosen • Implementation language is chosen Bernd Bruegge & Allen H. Dutoit 8 Object-Oriented Software Engineering: Using UML, Patterns, and Java
System Development as a Set of Activities Problem System Model Application objects Analysis Design Solution objects Custom objects - Object Design Off-the-Shelf Components - System Design Bernd Bruegge & Allen H. Dutoit 9 Existing Machine Object-Oriented Software Engineering: Using UML, Patterns, and Java
Object Design consists of 4 Activities 1. Reuse: Identification of existing solutions • Use of inheritance • Off-the-shelf components and additional solution objects • Design patterns 2. Interface specification • Describes precisely each class interface 3. Object model restructuring • Transforms the object design model to improve its understandability and extensibility 4. Object model optimization • Transforms the object design model to address performance criteria such as response time or memory utilization. Bernd Bruegge & Allen H. Dutoit 10 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Object Design Activities Select Subsystem Specification Identifying missing attributes & operations Reuse Identifying components Specifying visibility Adjusting components Specifying types & signatures Identifying patterns Specifying constraints Specifying exceptions Bernd Bruegge & Allen H. Dutoit 11 Adjusting patterns Object-Oriented Software Engineering: Using UML, Patterns, and Java
Detailed View of Object Design Activities (ctd) Check Use Cases Restructuring Optimization Revisiting inheritance Optimizing access paths Collapsing classes Caching complex computations Realizing associations Delaying complex computations Bernd Bruegge & Allen H. Dutoit 12 Object-Oriented Software Engineering: Using UML, Patterns, and Java
One Way to do Object Design 1. Identify the missing components in the design gap 2. Make a build or buy decision to obtain the missing component => Component-Based Software Engineering: The design gap is filled with available components (“ 0 % coding”). • Special Case: COTS-Development • • COTS: Commercial-off-the-Shelf The design gap is completely filled with commercial-off-the -shelf-components. => Design with standard components. Bernd Bruegge & Allen H. Dutoit 13 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Identification of new Objects during Object Design Requirements Analysis (Language of Application Domain) Incident Report Text box Menu Object Design (Language of Solution Domain) Bernd Bruegge & Allen H. Dutoit 14 Object-Oriented Software Engineering: Using UML, Patterns, and Java Scrollbar
Application Domain vs Solution Domain Objects Requirements Analysis (Language of Application Domain) Subject observers Observer * subscribe(subscriber) unsubscribe(subscriber) notify() update() Concrete. Subject Concrete. Observer state observe. State get. State() set. State() update() Object Design (Language of Solution Domain) Bernd Bruegge & Allen H. Dutoit 15 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Other Reasons for new Objects • The implementation of algorithms may necessitate objects to hold values • New low-level operations may be needed during the decomposition of high-level operations • Example: Erase. Area() in a drawing program • Conceptually very simple • Implementation is complicated: • Area represented by pixels • We need a Repair() operation to clean up objects partially covered by the erased area • We need a Redraw() operation to draw objects uncovered by the erasure • We need a Draw() operation to erase pixels in background color not covered by other objects. Bernd Bruegge & Allen H. Dutoit 16 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Modeling of the Real World • Modeling of the real world leads to a system that reflects today’s realities but not necessarily tomorrow’s. • There is a need for reusable and flexible designs • Design knowledge complements application domain knowledge and solution domain knowledge. Bernd Bruegge & Allen H. Dutoit 17 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Reuse of Code • I have a list, but my customer would like to have a stack • 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 reuse the existing car software in a home stereo system? Bernd Bruegge & Allen H. Dutoit 18 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Reuse of existing classes • I have an implementation for a list of elements of type int • Can I reuse this list to build • a list of customers • a spare parts catalog • a flight reservation schedule? • I have developed a class “Addressbook” in another project • 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? Bernd Bruegge & Allen H. Dutoit 19 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Customization: Build Custom Objects • Problem: Close the object design gap • Develop new functionality • Main goal: • Reuse knowledge from previous experience • Reuse functionality already available • Composition (also called Black Box Reuse) • New functionality is obtained by aggregation • The new object with more functionality is an aggregation of existing objects • Inheritance (also called White-box Reuse) • New functionality is obtained by inheritance Bernd Bruegge & Allen H. Dutoit 20 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Inheritance comes in many Flavors Inheritance is used in four ways: • • Specialization Generalization Specification Inheritance Implementation Inheritance. Bernd Bruegge & Allen H. Dutoit 21 Object-Oriented Software Engineering: Using UML, Patterns, and Java
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 sub class is discovered first. • Specialization: the discovery of an inheritance relationship between two classes, where the super class is discovered first. Bernd Bruegge & Allen H. Dutoit 22 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Generalization Example: Modeling a Coffee Machine Vending. Machine Generalization: The class Coffee. Machine is discovered first, then the class Soda. Machine, then the superclass Vending. Machine Coffee. Machine total. Receipts number. Of. Cups coffee. Mix collect. Money() make. Change() heat. Water() dispense. Beverage() add. Sugar() add. Creamer() Bernd Bruegge & Allen H. Dutoit 23 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Restructuring of Attributes and Operations is often a Consequence of Generalization Vending. Machine Called Remodeling if done on the model level; Called Refactoring if done on the source code level. Vending. Machine total. Receipts collect. Money() make. Change() dispense. Beverage() Coffee. Machine total. Receipts number. Of. Cups coffee. Mix collect. Money() make. Change() heat. Water() dispense. Beverage() add. Sugar() add. Creamer() Bernd Bruegge & Allen H. Dutoit 24 Coffee. Machine number. Of. Cups coffee. Mix heat. Water() add. Sugar() Object-Oriented Software Engineering: Using UML, Patterns, and Java add. Creamer() Soda. Machine cans. Of. Beer cans. Of. Cola chill()
An Example of a Specialization Vending. Machine total. Receipts collect. Money() make. Change() dispense. Beverage() Coffee. Machine number. Of. Cups coffee. Mix heat. Water() add. Sugar() add. Creamer() Bernd Bruegge & Allen H. Dutoit 25 Soda. Machine cans. Of. Beer cans. Of. Cola chill() Candy. Machine is a new product and designed as a sub class of the superclass Vending. Machine A change of names might now be useful: dispense. Item() instead of dispense. Beverage() and dispense. Snack() Candy. Machine bagsof. Chips number. Of. Candy. Bars dispense. Snack() Object-Oriented Software Engineering: Using UML, Patterns, and Java
Example of a Specialization (2) Vending. Maschine total. Receipts collect. Money() make. Change() dispense. Item() Coffee. Machine number. Of. Cups coffee. Mix heat. Water() add. Sugar() add. Creamer() dispense. Item() Bernd Bruegge & Allen H. Dutoit 26 Soda. Machine cans. Of. Beer cans. Of. Cola chill() dispense. Item() Candy. Machine bagsof. Chips number. Of. Candy. Bars dispense. Item() Object-Oriented Software Engineering: Using UML, Patterns, and Java
Meta-Model for Inheritance Analysis activity Taxonomy Inheritance detected by specialization Bernd Bruegge & Allen H. Dutoit 27 Inheritance detected by generalization Object Design Inheritance for Reuse Specification Inheritance Object-Oriented Software Engineering: Using UML, Patterns, and Java Implementation Inheritance
For Reuse: Implementation Inheritance and Specification Inheritance • Implementation inheritance • Also called class inheritance • Goal: • 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 • Also called subtyping • Goal: • Inherit from a specification • The specification is an abstract class with all operations specified, but not yet implemented. Bernd Bruegge & Allen H. Dutoit 28 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Example for Implementation Inheritance • 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 Push() Pop() Top() Problem with implementation inheritance: � • The inherited operations might exhibit unwanted behavior. • Example: What happens if the Stack user calls Remove() instead of Pop()? Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29
Delegation instead of Implementation Inheritance • 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 List +Push() +Pop() +Top() Add() Remove() +Push() +Pop() +Top() Bernd Bruegge & Allen H. Dutoit 30 Object-Oriented Software Engineering: Using UML, Patterns, and Java
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 misuse the Delegate object. Client Bernd Bruegge & Allen H. Dutoit 31 calls Receiver delegates to Object-Oriented Software Engineering: Using UML, Patterns, and Java Delegate
Revised Metamodel for Inheritance Analysis activity Taxonomy Inheritance detected by specialization Bernd Bruegge & Allen H. Dutoit 32 Inheritance detected by generalization Object Design Inheritance for Reuse Specification Inheritance Strict Inheritance Object-Oriented Software Engineering: Using UML, Patterns, and Java Implementation Inheritance Contraction
Documenting Object Design: ODD Conventions • Each subsystem in a system provides a service • Describes the set of operations provided by the subsystem • Specification of the service operations • Signature: Name of operation, fully typed parameter list and return type • Abstract: Describes the operation • Pre: Precondition for calling the operation • Post: Postcondition describing important state after the execution of the operation • Use Java. Doc and Contracts for the specification of service operations Bernd Bruegge & Allen H. Dutoit 33 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Package it all up • Pack up design into discrete units that can be edited, compiled, linked, reused • Construct physical modules • Ideally use one package for each subsystem • System decomposition might not be good for implementation. • Two design principles for packaging • Minimize coupling: • Classes in client-supplier relationships are usually loosely coupled • Avoid large number of parameters in methods to avoid strong coupling (should be less than 4 -5) • Avoid global data • Maximize cohesion: Put classes connected by associations into one package. Bernd Bruegge & Allen H. Dutoit 34 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Packaging Heuristics • Each subsystem service is made available by one or more interface objects within the package • Start with one interface object for each subsystem service • Try to limit the number of interface operations (7+-2) • If an interface object has too many operations, reconsider the number of interface objects • If you have too many interface objects, reconsider the number of subsystems • Interface objects vs Java interface: • Interface object: Used during requirements analysis, system design, object design. Denotes a service or API • Java interface: Used during implementation in Java (May or may not implement an interface object). Bernd Bruegge & Allen H. Dutoit 35 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Chapter 8, Object Design Introduction to 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. Bernd Bruegge & Allen H. Dutoit 37 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Finding Objects • The hardest problems in object-oriented system development are: • Identifying objects • Decomposing the system into objects • Requirements Analysis focuses on application domain: • Object identification • System Design addresses both application and implementation domains: • Subsystem Identification • Object Design focuses on implementation domain: • Additional solution objects Bernd Bruegge & Allen H. Dutoit 38 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Techniques for Finding 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 • Subsystem decomposition • Try to identify layers and partitions • Object Design • Find additional objects by applying implementation domain knowledge Bernd Bruegge & Allen H. Dutoit 39 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Another Source for Finding Objects : Design Patterns • What are Design Patterns? • 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. Studying patterns is an effective way to learn from the experience of others Bernd Bruegge & Allen H. Dutoit 40 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Introducing the Composite Pattern • An abstract class (Component) is the roof of all objects • The Composite classes are subclass of Component, which represent aggregates • The Composite Pattern lets client treat individual objects and compositions of these objects uniformly Client Component Leaf Operation() Bernd Bruegge & Allen H. Dutoit 42 * Composite Operation() Add. Component Remove. Component() Get. Child() Object-Oriented Software Engineering: Using UML, Patterns, and Java Children
Modeling a Software System with a Composite Pattern Software System User Class Bernd Bruegge & Allen H. Dutoit 43 * Subsystem Object-Oriented Software Engineering: Using UML, Patterns, and Java children
Graphic Applications also use Composite Patterns • The Graphic Class represents both primitives (Line, Circle) and their containers (Picture) Client Line Draw() Bernd Bruegge & Allen H. Dutoit 44 Graphic Circle Draw() * Picture Draw() Add(Graphic g) Remove. Graphic) Get. Child(int) Object-Oriented Software Engineering: Using UML, Patterns, and Java Children
Reducing the Complexity of Models • To communicate a complex model we use navigation and reduction of complexity • 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 • We start with a very simple model • Start with the key abstractions • Then decorate the model with additional classes • To reduce the complexity of the model further, we • Look for inheritance (taxonomies) • If the model is still too complex, we show subclasses on a separate slide • Then we identify or introduce patterns in the model • We make sure to use the name of the patterns. Bernd Bruegge & Allen H. Dutoit 45 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Example: A Complex Model Taxonomies Basic Abstractions Composite Patterns Bernd Bruegge & Allen H. Dutoit 46 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Many design patterns use a combination of inheritance and delegation Bernd Bruegge & Allen H. Dutoit 47 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Adapter Pattern Client. Interface Legacy. Class Request() Existing. Request() adaptee Inheritance Adapter Delegation Request() 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 Bernd Bruegge & Allen H. Dutoit 48 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Adapter Pattern • The adapter pattern lets classes work together that couldn’t 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). • Two adapter patterns: • Class adapter: • Uses multiple inheritance to adapt one interface to another • Object adapter: • Uses single inheritance and delegation • Object adapters are much more frequent. • We cover only object adapters (and call them adapters). Bernd Bruegge & Allen H. Dutoit 49 Object-Oriented Software Engineering: Using UML, Patterns, and Java
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. • Also know as a Handle/Body pattern • Allows different implementations of an interface to be decided upon dynamically. Bernd Bruegge & Allen H. Dutoit 50 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Bridge Pattern Taxonomy in Application Domain Bernd Bruegge & Allen H. Dutoit 51 Taxonomy in Solution Domain Object-Oriented Software Engineering: Using UML, Patterns, and Java
Why the Name Bridge Pattern? Taxonomy in Application Domain Bernd Bruegge & Allen H. Dutoit 52 Taxonomy in Solution Domain Object-Oriented Software Engineering: Using UML, Patterns, and Java
Motivation for the 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) Bernd Bruegge & Allen H. Dutoit 53 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Using a Bridge • The bridge pattern can be used to provide multiple implementations under the same interface Bernd Bruegge & Allen H. Dutoit 54 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Example use of the Bridge Pattern: Support multiple Database Vendors Arena League. Store Stub Store Implementor Bernd Bruegge & Allen H. Dutoit 55 imp League. Store. Implementor XML Store Implementor JDBC Store Implementor Object-Oriented Software Engineering: Using UML, Patterns, and Java
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 • Applied to systems after they’re 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” Bernd Bruegge & Allen H. Dutoit 56 Object-Oriented Software Engineering: Using UML, Patterns, and Java
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 Bernd Bruegge & Allen H. Dutoit 57 Object-Oriented Software Engineering: Using UML, Patterns, and Java
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 Subsystem 2 Seat • Why is this bad? Card • Can’t 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 Bernd Bruegge & Allen H. Dutoit 58 AIM Object-Oriented Software Engineering: Using UML, Patterns, and Java SA/RT
Subsystem Design with Façade, Adapter, Bridge • The ideal structure of a subsystem consists of • an interface object • a set of application domain objects (entity objects) modeling real entities or existing systems • Some of the application domain objects are interfaces to existing systems • one or more control objects • We can use design patterns to realize this subsystem structure • Realization of the Interface Object: Facade • Provides the interface to the subsystem • Interface to existing systems: Adapter or Bridge • Provides the interface to existing system (legacy system) • The existing system is not necessarily object-oriented! Bernd Bruegge & Allen H. Dutoit 59 Object-Oriented Software Engineering: Using UML, Patterns, and Java
When should you use these Design Patterns? • A façade should be offered by all subsystems in a software system who a services • The façade delegates requests to the appropriate components within the subsystem. The façade usually does not have to be changed, when the components are changed • The adapter design pattern should be used to interface to existing components • Example: A smart card software system should use an adapter for a smart card reader from a specific manufacturer • The bridge design pattern should be used to interface to a set of objects • 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. Bernd Bruegge & Allen H. Dutoit 60 Object-Oriented Software Engineering: Using UML, Patterns, and Java
Summary • Design patterns are partial solutions to common problems 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 • A design pattern consists of a small number of classes • uses delegation and inheritance • provides a modifiable design solution • 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 61 Object-Oriented Software Engineering: Using UML, Patterns, and Java
- Slides: 60