+ All Categories
Home > Documents > 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

Date post: 19-Dec-2015
Category:
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
44
1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3
Transcript
Page 1: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

1 CS 501 Spring 2005

CS 501: Software Engineering

Lecture 17

Object Oriented Design 3

Page 2: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

2 CS 501 Spring 2005

Administration

Second presentation and report next week

Final milestone: four weeks from handover-- design complete, implementation begun-- schedule for testing and revisions-- plan for handover to client

Sign up for meeting times

Quiz 3

During class on Thursday

Teaching Assistant

Lin Guo away until April 17

Page 3: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

3 CS 501 Spring 2005

Design Patterns

Design patterns:

E. Gamma, R. Helm, R. Johnson, and J. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1994

Page 4: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

4 CS 501 Spring 2005

Sources

The following discussion of design patterns is based on Gamma, et al., 1994, and Bruegge and Dutoit, 2004.

Page 5: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

5 CS 501 Spring 2005

Design Pattern

Design patterns are template designs that can be used in a variety of systems. They are particularly appropriate in situations where classes are likely to be reused in a system that evolves over time.

• Name. [Some of the names used by Gamma, et al. are becoming standard software terminology.]

• Problem description. Describes when the pattern might be used, often in terms of modifiability and extensibility.

• Solution. Expressed in terms of classes and interfaces.

• Consequences. Trade-offs and alternatives.

Page 6: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

6 CS 501 Spring 2005

Design for Reuse: Inheritance and Abstract Classes

Classes can be defined in terms of other classes using inheritance. The generalization class is called the superclass and the specialization is called the subclass.

If the inheritance relationship serves only to model shared attributes and operations, i.e., the generalization is not intended to be implemented, the class is called an abstract class

Page 7: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

7 CS 501 Spring 2005

Design for Reuse: Implementation and Specification Inheritance

Implementation Inheritance

Developers reuse code quickly by subclassing an existing class and refining its behavior. Is not good for reuse.

Specification Inheritance

The classification of concepts into type hierarchies, so that an object from a specified class can be replaced by an object from one of its subclasses.

Page 8: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

8 CS 501 Spring 2005

Design for Reuse: Specification Inheritance

Liskov Substitution Principle (strict inheritance)

If an object of type S can be substituted in all the places where an object of type T is expected, then S is a subtype of T.

Interpretation

The Liskov Substitution Principle means that if all classes are subtypes of their superclasses, all inheritance relationships are specification inheritance relationships. New subclasses of T can be added without modifying the methods of T. This leads to an extensible system.

Page 9: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

9 CS 501 Spring 2005

Design for Reuse: Delegation

Delegation

A class is said to delegate to another class if it implements an operation by resending a message to another class.

Delegation is an alternative to implementation inheritance that should be used when reuse is anticipated.

Page 10: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

10 CS 501 Spring 2005

Adapter: Wrapping Around Legacy Code

Problem description:

Convert the user interface of a legacy class into a different interface expected by the client, so that the client and the legacy class can work together without changes.

This problem often occurs during a transitional period, when the long-term plan is to phase out the legacy system.

Example:

How do you use a web browser to access an existing information retrieval system that was designed for a different client?

Page 11: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

11 CS 501 Spring 2005

Adapter Design Pattern

LegacyClass

existingRequest()

NewClient

OldClient

NewClass

request()

dependency

Page 12: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

12 CS 501 Spring 2005

Adapter Design Pattern: Solution Class Diagram

ClientInterface

request()

Adapter

request()

LegacyClass

existingRequest()

Client abstract class

delegationinheritance

Page 13: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

13 CS 501 Spring 2005

Adapter Design Pattern: Consequences

The following consequences apply whenever the Adapter design pattern in used.

• Client and LegacyClass work together without modification of either.

• Adapter works with LegacyClass and all of its subclasses.

• A new Adapter needs to be written if Client is replaced by a subclass.

Page 14: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

14 CS 501 Spring 2005

Bridge:Allowing for Alternate Implementations

Name: Bridge design pattern

Problem description:

Decouple an interface from an implementation so that a different implementation can be substituted, possibly at runtime (e.g., testing different implementations of the same interface).

Page 15: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

15 CS 501 Spring 2005

Bridge: Class Diagram

Client

ConcreteImplementorA

ConcreteImplementorB

alternative implementations

Page 16: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

16 CS 501 Spring 2005

Bridge:Allowing for Alternate Implementations

Solution:

The Abstraction class defines the interface visible to the client. Implementor is an abstract class that defines the lower-level methods available to Abstraction. An Abstraction instance maintains a reference to its corresponding Implementor instance.

Abstraction and Implementor can be refined independently.

Page 17: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

17 CS 501 Spring 2005

Bridge: Class Diagram

Abstraction Implementor

Client

ConcreteImplementorA

ConcreteImplementorB

whole/part association

Page 18: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

18 CS 501 Spring 2005

Bridge: Class Diagram

Abstraction

RefinedAbstraction

Implementor

Client

ConcreteImplementorA

ConcreteImplementorBnew abstraction

Page 19: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

19 CS 501 Spring 2005

Bridge: Consequences

Consequences:

Client is shielded from abstract and concrete implementations

Interfaces and implementations may be tested separately

Page 20: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

20 CS 501 Spring 2005

Strategy:Encapsulating Algorithms

Name: Strategy design pattern

Problem description:

Decouple a policy-deciding class from a set of mechanisms, so that different mechanisms can be changed transparently.

Example:

A mobile computer can be used with a wireless network, or connected to an Ethernet, with dynamic switching between networks based on location and network costs.

Page 21: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

21 CS 501 Spring 2005

Strategy:Encapsulating Algorithms

Solution:

A Client accesses services provided by a Context.

The Context services are realized using one of several mechanisms, as decided by a Policy object.

The abstract class Strategy describes the interface that is common to all mechanisms that Context can use. Policy class creates a ConcreteStrategy object and configures Context to use it.

Page 22: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

22 CS 501 Spring 2005

Strategy Example: Class Diagram for Mobile Computer

NetworkConnection

send()setNetworkInterface()

NetworkInterface

open()close()send()

Application LocationManager

Ethernet

open()close()send()

WirelessNet

open()close()send()

Note the similarities to Bridge pattern

Page 23: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

23 CS 501 Spring 2005

Strategy: Class Diagram

Context

contextInterface()Strategy

algorithmInterface()

Client

ConcreteStrategy2

Policy

ConcreteStrategy1

Page 24: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

24 CS 501 Spring 2005

Strategy: Consequences

Consequences:

ConcreteStrategies can be substituted transparently from Context.

Policy decides which Strategy is best, given the current circumstances.

New policy algorithms can be added without modifying Context or Client.

Page 25: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

25 CS 501 Spring 2005

Abstract Factory: Encapsulating Platforms

Name: Abstract Factory design pattern

Problem description:

Shield the client from different platforms that provide different implementations of the same set of concepts

Example:

A browser must have versions that implement the same set of concepts for several windowing systems, e.g., scroll bars, buttons, highlighting, etc.

Page 26: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

26 CS 501 Spring 2005

Abstract Factory: Encapsulating Platforms

Solution:

A platform (e.g., the application for a specific windowing system) is represented as a set of AbstractProducts, each representing a concept (e.g., button). An AbstractFactory class declares the operations for creating each individual product.

A specific platform is then realized by a ConcreteFactory and a set of ConcreteProducts.

Page 27: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

27 CS 501 Spring 2005

Abstract Factory: Class Diagram

Client AbstractFactory

createProductA createProductB

ConcreteFactory1

createProductA createProductB

AbstractProductA

ProductA

Classes for ProductB are not shown in this diagram.

There could be several ConcreteFactory classes, each a subclass of AbstractFactory,

Page 28: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

28 CS 501 Spring 2005

Abstract Factory: Consequences

Consequences:

Client is shielded from concrete products classes

Substituting families at runtime is possible

Adding new products is difficult since new realizations must be created for each factory

Page 29: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

29 CS 501 Spring 2005

Command:Encapsulating Control Flow

Name: Command design pattern

Problem description:

Encapsulates requests so that they can be executed, undone, or queued independently of the request.

Solution:

A Command abstract class declares the interface supported by all ConcreteCommands. ConcreteCommands encapsulate a service to be applied to a Receiver. The Client creates ConcreteCommands and binds them to specific Receivers. The Invoker actually executes a command.

Page 30: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

30 CS 501 Spring 2005

Command: Class Diagram

Receiver

Command

execute()

ConcreteCommand1

execute()

Invokerinvokes

<<binds>>

ConcreteCommand2

execute()

Page 31: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

31 CS 501 Spring 2005

Command: Class Diagram for Match

GameBoard

Move

play()replay()

Game1Move

play()replay()

Matchinvokes

<<binds>>

Game1Move

play()replay()

Page 32: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

32 CS 501 Spring 2005

Command: Consequences

Consequences:

The object of the command (Receiver) and the algorithm of the command (ConcreteCommand) are decoupled.

Invoker is shielded from specific commands.

ConcreteCommands are objects. They can be created and stored.

New ConcreteCommands can be added without changing existing code.

Page 33: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

33 CS 501 Spring 2005

Composite:Representing Recursive Hierarchies

Name: Composite design pattern

Problem description:

Represents a hierarchy of variable width and depth, so that the leaves and composites can be treated uniformly through a common interface.

Solution:

The Component interface specifies the services that are shared between Leaf and Composite. A Composite has an aggregation association with Components and implements each service by iterating over each contained Component. The Leaf services do the actual work.

Page 34: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

34 CS 501 Spring 2005

Composite: Class Diagram

Compositeleaves

Leaf

Component*

Client

Page 35: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

35 CS 501 Spring 2005

Composite: Consequences

Consequences:

Client uses the same code for dealing with Leaves or Composites.

Leaf-specific behavior can be changed without changing the hierarchy.

New classes of Leaves can be added without changing the hierarchy.

Page 36: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

36 CS 501 Spring 2005

Facade:Encapsulating Subsystems

Name: Facade design pattern

Problem description:

Reduce coupling between a set of related classes and the rest of the system.

Solution:

A single Facade class implements a high-level interface for a subsystem by invoking the methods of the lower-level classes.

Example. A Compiler is composed of several classes: LexicalAnalyzer, Parser, CodeGenerator, etc. A caller, invokes only the Compiler (Facade) class, which invokes the contained classes.

Page 37: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

37 CS 501 Spring 2005

Facade: Class Diagram

Facade

service()

Class1

service1()

Class2

service2()

Class3

service3()

Facade

Page 38: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

38 CS 501 Spring 2005

Facade: Consequences

Consequences:

Shields a client from the low-level classes of a subsystem.

Simplifies the use of a subsystem by providing higher-level methods.

Enables lower-level classes to be restructured without changes to clients.

Note. The repeated use of Facade patterns yields a layered system.

Page 39: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

39 CS 501 Spring 2005

Observer:Encapsulating Control Flow

Name: Observer design pattern

Problem description:

Maintains consistency across state of one Subject and many Observers.

Solution:

A Subject has a primary function to maintain some state (e.g., a data structure). One or more Observers use this state, which introduces redundancy between the states of Subject and Observer.

Observer invokes the subscribe() method to synchronize the state. Whenever the state changes, Subject invokes its notify() method to iteratively invoke each Observer.update() method.

Page 40: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

40 CS 501 Spring 2005

Observer: Class Diagram

Subject

subscribe()unsubscribe()notify()

subscribers

ConcreteSubject

state

getstate()setstate()

Observer

update()

ConcreteObserver

observeState

update()

1 *

Page 41: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

41 CS 501 Spring 2005

Observer: Consequences

Consequences:

Decouples Subject, which maintains state, from Observers, who make use of the state.

Can result in many spurious broadcasts when the state of Subject changes.

Page 42: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

42 CS 501 Spring 2005

Proxy:Encapsulating Expensive Objects

Name: Proxy design pattern

Problem description:

Improve performance or security of a system by delaying expensive computations, using memory only when needed, or checking access before loading an object into memory.

Solution:

The ProxyObject class acts on behalf of a RealObject class. Both implement the same interface. ProxyObject stores a subset of the attributes of RealObject. ProxyObject handles certain requests, whereas others are delegated to RealObject. After delegation, the RealObject is created and loaded into memory.

Page 43: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

43 CS 501 Spring 2005

Proxy: Class Diagram

Object

filename

op1()op2()

RealObject

data:byte[]

op1()op2()

ProxyObject

filename

op1()op2()

1

0..1

Client

Page 44: 1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.

44 CS 501 Spring 2005

Proxy: Consequences

Consequences:

Adds a level of indirection between Client and RealObject.

The Client is shielded from any optimization for creating RealObjects.


Recommended