Using UML Patterns and Java ObjectOriented Software Engineering
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Reuse and Patterns I
Object Design Where is architectural design? Cf. Shaw&Garlan - OO ¨ ¨ Object design is the process of adding details to the requirements analysis and making implementation decisions 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. Requirements Analysis: Use cases, functional and dynamic model deliver operations for object model Object Design: - Iterates on the models, in particular the object model and refine the models; Hence, OOAD (but without architecture) - Serves as the basis of implementation Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 2
Examples of Object Design Activities ¨ ¨ ¨ Identification of existing components Full definition of associations Full definition of classes: w System Design => Service, w Object Design => API ¨ ¨ ¨ ¨ Specifying the contract for each component Choosing algorithms and data structures Identifying possibilities of reuse Detection of solution-domain classes Optimization Increase of inheritance Decision on control (Re-)Packaging ¨Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 3
1. Reuse: Identification of existing solutions w Use of inheritance w Off-the-shelf components and additional solution objects w Design patterns Object Design lectures 2. Interface specification w Describes precisely each class interface 3. Object model restructuring w Transforms the object design model to improve its understandability and extensibility Mapping Models to Code lecture 4. Object model optimization w Transforms the object design model to address performance criteria such as response time or memory utilization. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 4
Outline of Today ¨ ¨ ¨ The use of inheritance Implementation vs Interface Inheritance Delegation Documenting the Object Design In Appendix Java. Doc In Appendix Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 5
Application Domain vs Solution Domain Objects Requirements Analysis (Language of Application Domain) Object Design (Language of Solution Domain) Incident Report Text box Menu Scrollbar Do the two Incident Reports have the same meaning? Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 6
The use of inheritance ¨ Inheritance is used to achieve two different goals w Description of Taxonomies w Interface Specification ¨ Identification of taxonomies w Used during requirements analysis. w Activity: identify application domain objects that are hierarchically related w Goal: make the analysis model more understandable ¨ Service specification w Used during object design w Activity: w Goal: increase reusability, enhance modifiability and extensibility ¨ Inheritance is found either by specialization or generalization Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 7
Taxonomy Example Mammal Tiger Wolf Wale Is this hierarchy for enhancing understandability or reuse? Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 8
Implementation Inheritance ¨ A very similar class is already implemented that does almost the same as the desired class implementation. Example: I have a List class, I need a Stack class. How about subclassing the Stack class from the List class and providing three methods, Push() and Pop(), Top()? List Add () Remove() “Already implemented” Stack Push () Pop() Top() Problem with implementation inheritance: Some of the inherited operations might exhibit unwanted behavior. What happens if the Stack user calls Remove() instead of Pop()? An elephant becomes a car! Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9
Implementation Inheritance vs Interface Inheritance ¨ Implementation inheritance w Also called class inheritance w Goal: Extend an applications’ functionality by reusing functionality in parent class w Inherit from an existing class with some or all operations already implemented Hurts understandability, maintainability, reliability, … ¨ Interface inheritance w Also called subtyping w Inherit from an abstract class with all operations specified, but not yet implemented Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10
Delegation as alternative to Implementation Inheritance ¨ In Delegation two objects are involved in handling a request w A receiving object delegates operations to its delegate. w The developer can make sure that the receiving object does not allow the client to misuse the delegate object Client calls Receiver Delegates to Delegate Cf. notion of modularization w. import/export - ADT Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11
Delegation instead of Implementation Inheritance ¨ ¨ ¨ Inheritance: Extending a Base class by a new operation or overwriting an operation. Delegation: Catching an operation and sending it to another object. Which of the following models is better for implementing a stack? List +Add() +Remove() Stack +Push() +Pop() +Top() Bernd Bruegge & Allen H. Dutoit Stack +Push() +Pop() +Top() List Remove() Add() What should Push() and Pop() be like? How can this be modelled in UML? Where did weanddiscuss Object-Oriented Software Engineering: Using UML, Patterns, Java UML “interface”? 12
Comparison: Delegation vs Implementation Inheritance ¨ Delegation w Pro: t Flexibility: Any object can be replaced at run time by another one (as long as it has the same type) w Con: t ¨ why? Inefficiency: Objects are encapsulated. Inheritance w Pro: t t t Straightforward to use Supported by many programming languages Easy to implement new functionality w Con: Inheritance exposes a subclass to the details of its parent class t Any change in the parent class implementation forces the subclass to change recompilation both) Bernd Bruegge & Allen H. Dutoit (which requires Object-Oriented Software Engineering: Usingof UML, Patterns, and Java 13 t
Lecture on Design Patterns Many design patterns use a combination of inheritance and delegation Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14
Frameworks ¨ A framework is a reusable partial application that can be specialized to produce custom applications. ¨ Frameworks are targeted to particular technologies, such as data processing or cellular communications, or to application domains, such as user interfaces or real-time avionics. Are J 2 EE and the. NET frameworks or architectural specs? ¨ The key benefits of frameworks are reusability and extensibility. w Reusability leverages of the application domain knowledge and prior effort of experienced developers w Extensibility is provided by hook methods, which are overwritten by the application to extend the framework. t Hook methods systematically decouple the interfaces and behaviors of an application domain from the variations required by an application in a particular context. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 15
Example: Framework for Building Web Applications Web. Objects Web. Browser Web. Server Static. HTML WOAdaptor Wo. Request Web. Objects. Application WORequest Template EOF Relational. Database Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 16
A Little Bit of Terminology: Activities ¨ Object-Oriented methodologies use these terms: w System Design Activity t Decomposition into subsystems w Object Design Activity t t ¨ Implementation language chosen Data structures and algorithms chosen Structured analysis/structured design uses these terms: w Preliminary Design Activity t t Decomposition into subsystems Data structures are chosen ? w Detailed Design Activity t t Algorithms are chosen Data structures are refined Implementation language is chosen Typically in parallel with preliminary design, not a separate activity Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 17
Summary ¨ ¨ ¨ Object design closes the gap between the requirements and the machine. Object design is the process of adding details to the requirements analysis and making implementation decisions Object design activities include: ü ü ü w w w ¨ Identification of Reuse Identification of Inheritance and Delegation opportunities Component selection Interface specification (next lecture) Object model restructuring Lecture on Mapping Models to Code Object model optimization Object design is documented in the Object Design Document, which can be automatically generated from a specification using tools such as Java. Doc. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 18
Additional Slides Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 19
Object Design: Closing the Gap Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 20
A More Detailed View of 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 Dutoit Adjusting patterns Object-Oriented Software Engineering: Conquering Complex and Changing Systems 21
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 Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 22
A 2: Application Server A 1: Application Server State Profil State Profil State Database Server (Database Framework) HTTP Web Browser (UI Framework) Bernd Bruegge & Allen Dutoit Web Server Object-Oriented Software Engineering: Conquering Complex and Changing Systems 23
Reuse ¨ Main goal: w Reuse knowledge from previous experience to current problem w Reuse functionality already available ¨ Composition (also called Black Box Reuse) w New functionality is obtained by aggregation w The new object with more functionality is an aggregation of existing components ¨ Inheritance (also called White-box Reuse) w New functionality is obtained by inheritance. ¨ Three ways to get new functionality: t t t Implementation inheritance Interface inheritance Delegation Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24
Reuse Heuristics ¨ Look for existing classes in class libraries w JSAPI, JTAPI, . . ¨ Select data structures appropriate to the algorithms w Container classes w Arrays, lists, queues, stacks, sets, trees, . . . ¨ Define new internal classes and operations only if necessary w Complex operations defined in terms of lower-level operations might need new classes and operations Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 25
Reuse Concepts ¨ ¨ ¨ Application objects versus solution objects Specification inheritance and implementation inheritance The Liskov Substitution Principle Delegation (Section 8. 3. 3) Delegation and inheritance in design patterns Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 26
Observation about Modeling of the Real World ¨ [Gamma et al 94]: Strict 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 Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 27
Application domain vs solution domain objects ¨ Application objects, also called domain objects, represent concepts of the domain that are relevant to the system. w They are identified by the application domain specialists and by the end users. ¨ Solution objects represent concepts that do not have a counterpart in the application domain, w They are identified by the developers w Examples: Persistent data stores, user interface objects, middleware. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 28
Metamodel for Inheritance ¨ Inheritance is used during analysis and object design Inheritance Object Design Analysis activity Taxonomy Inheritance detected by specialization Bernd Bruegge & Allen Dutoit Inheritance detected by generalization Inheritance for Reuse Specification Inheritance Object-Oriented Software Engineering: Conquering Complex and Changing Systems Implementation Inheritance 29
Implementation of Application Domain Classes ¨ New objects are often needed during object design: w The use of design patterns introduces new classes w The implementation of algorithms may necessitate objects to hold values w New low-level operations may be needed during the decomposition of high-level operations ¨ Example: The Erase. Area() operation in a drawing program. w Conceptually very simple w Implementation t t Area represented by pixels Repair () cleans up objects partially covered by the erased area Redraw() draws objects uncovered by the erasure Draw() erases pixels in background color not covered by other objects Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 30
Component Selection ¨ Select existing w off-the-shelf class libraries w frameworks or w components ¨ Adjust the class libraries, framework or components w Change the API if you have the source code. w Use the adapter or bridge pattern if you don’t have access ¨ Architecture Driven Design Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 31
Classification of Frameworks ¨ Frameworks can be classified by their position in the software development process. ¨ Frameworks can also be classified by the techniques used to extend them. w Whitebox frameworks w Blackbox frameworks Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 32
Frameworks in the Development Process ¨ Infrastructure frameworks aim to simplify the software development process w System infrastructure frameworks are used internally within a software project and are usually not delivered to a client. ¨ Middleware frameworks are used to integrate existing distributed applications and components. w Examples: MFC, DCOM, Java RMI, Web. Objects, Web. Sphere, Web. Logic Enterprise Application [BEA]. ¨ Enterprise application frameworks are application specific and focus on domains w Example domains: telecommunications, avionics, environmental modeling, manufacturing, financial engineering, enterprise business activities. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 33
White-box and Black-Box Frameworks ¨ Whitebox frameworks: w Extensibility achieved through inheritance and dynamic binding. w Existing functionality is extended by subclassing framework base classes and overriding predefined hook methods w Often design patterns such as the template method pattern are used to override the hook methods. ¨ Blackbox frameworks w Extensibility achieved by defining interfaces for components that can be plugged into the framework. w Existing functionality is reused by defining components that conform to a particular interface w These components are integrated with the framework via delegation. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 34
Class libraries and Frameworks ¨ Class Libraries: w Less domain specific w Provide a smaller scope of reuse. w Class libraries are passive; no constraint on control flow. ¨ Framework: w Classes cooperate for a family of related applications. w Frameworks are active; affect the flow of control. ¨ In practice, developers often use both: w Frameworks often use class libraries internally to simplify the development of the framework. w Framework event handlers use class libraries to perform basic tasks (e. g. string processing, file management, numerical analysis…. ) Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 35
Components and Frameworks ¨ Components w w w Self-contained instances of classes Plugged together to form complete applications. Blackbox that defines a cohesive set of operations, Can be used based on the syntax and semantics of the interface. Components can even be reused on the binary code level. t ¨ The advantage is that applications do not always have to be recompiled when components change. Frameworks: w Often used to develop components w Components are often plugged into blackbox frameworks. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 36
Documenting the Object Design: The Object Design Document (ODD) ¨ Object design document w Same as the Requirements Analysis Document (RAD) plus. . . w … additions to object, functional and dynamic models (from solution domain) w … navigational map for object model w … Javadoc documentation for all classes ¨ ODD Management issues w w ¨ Update the system models in the RAD? Should the ODD be a separate document? Who is the target audience for these documents (Customer, developer? ) If time is short: Focus on the Navigational Map and Javadoc documentation? ODD Template: w http: //www. oose. org Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 37
Documenting Object Design: ODD Conventions ¨ Each subsystem in a system provides a service (see Chapters on System Design) w Describes the set of operations provided by the subsystem ¨ Specifying a service operation as w w 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 Example: what should withdraw be like? Shouldn’t these be part of architectural spec? ¨ Use Java. Doc for the specification of service operations. More In Appendix Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 38
More In Appendix Java. Doc ¨ ¨ ¨ Add documentation comments to the source code. A doc comment consists of characters between /** and */ When Java. Doc parses a doc comment, leading * characters on each line are discarded. First, blanks and tabs preceding the initial * characters are also discarded. Doc comments may include HTML tags Example of a doc comment: /** * This is a <b> doc </b> comment */ Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 39
More on Java. Doc ¨ ¨ Doc comments are only recognized when placed immediately before class, interface, constructor, method or field declarations. When you embed HTML tags within a doc comment, you should not use heading tags such as <h 1> and <h 2>, because Java. Doc creates an entire structured document and these structural tags interfere with the formatting of the generated document. Class and Interface Doc Tags Constructor and Method Doc Tags Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 40
Class and Interface Doc Tags @author name-text w Creates an “Author” entry. @version-text w Creates a “Version” entry. @see classname w Creates a hyperlink “See Also classname” @since-text w Adds a “Since” entry. Usually used to specify that a feature or change exists since the release number of the software specified in the “since-text” @deprecated-text w Adds a comment that this method can no longer be used. Convention is to describe method that serves as replacement w Example: @deprecated Replaced by set. Bounds(int, int). Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 41
Constructor and Method Doc Tags ¨ Can contain @see tag, @since tag, @deprecated as well as: @parameter-name description Adds a parameter to the "Parameters" section. The description may be continued on the next line. @return description Adds a "Returns" section, which contains the description of the return value. @exception fully-qualified-class-name description Adds a "Throws" section, which contains the name of the exception that may be thrown by the method. The exception is linked to its class documentation. @see classname Adds a hyperlink "See Also" entry to the method. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 42
Example of a Class Doc Comment /** * A class representing a window on the screen. * For example: * <pre> * Window win = new Window(parent); * win. show(); * </pre> * * @author Sami Shaio * @version %I%, %G% * @see java. awt. Base. Window * @see java. awt. Button */ class Window extends Base. Window {. . . } Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 43
Example of a Method Doc Comment /** * Returns the character at the specified index. An index * ranges from <code>0</code> to <code>length() - 1</code>. * * @param index the index of the desired character. * @return the desired character. * @exception String. Index. Out. Of. Range. Exception * if the index is not in the range <code>0</code> * to <code>length()-1</code>. * @see java. lang. Character#char. Value() */ public char. At(int index) {. . . } Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 44
Example of a Field Doc Comment ¨ A field comment can contain only the @see, @since and @deprecated tags /** * The X-coordinate of the window. * * @see window#1 */ int x = 1263732; Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 45
Example: Specifying a Service in Java /** Office is a physical structure in a building. It is possible to create an instance of a office; add an occupant; get the name and the number of occupants */ public class Office { /** Adds an occupant to the office */ * @param NAME name is a nonempty string */ public void Add. Occupant(string name); /** @Returns the name of the office. Requires, that Office has been initialized with a name */ public string Get. Name(); . . } Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 46
Package it all up ¨ ¨ Pack up design into discrete physical units that can be edited, compiled, linked, reused Construct physical modules w Ideally use one package for each subsystem w System decomposition might not be good for implementation. ¨ Two design principles for packaging w Minimize coupling: t t t Classes in client-supplier relationships are usually loosely coupled Large number of parameters in some methods mean strong coupling (> 4 -5) Avoid global data w Maximize cohesion: t Classes closely connected by associations => same package Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 47
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 w Try to limit the number of interface operations (7+-2) ¨ ¨ ¨ If the subsystem service has too many operations, reconsider the number of interface objects If you have too many interface objects, reconsider the number of subsystems Difference between interface objects and Java interfaces w Interface object : Used during requirements analysis, system design and object design. Denotes a service or API w Java interface: Used during implementation in Java (A Java interface may or may not implement an interface object) Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 48
- Slides: 48