Docsity

Design Patterns in Software Engineering, Lecture notes of Compiler Design

Partial preview of the text

Download Design Patterns in Software Engineering and more Lecture notes Compiler Design in PDF only on Docsity!

Cornell University

Compu1ng and Informa1on Science

CS 5150 So(ware Engineering

Design Pa4erns

William Y. Arms

Design Pa4erns

Sources: E. Gamma, R. Helm, R. Johnson, and J. Vlissides, Design Pa*erns: Elements of Reusable Object-­‐Oriented So<ware. Addison-­‐Wesley, 1994 The following discussion of design pa4erns is based on Gamma, et al., 1994, and Bruegge and Dutoit, 2004. Wikipedia has good discussion of many design pa4erns, using UML and other notaTon, with code samples.

NotaTon

ClassName class name in italic indicates an abstract class dependency delegaTon inheritance whole/part associaTon whole part

Abstract Classes

Abstract class Abstract classes are superclasses which contain abstract methods and are defined such that concrete subclasses extend them by implemenTng the methods. Before a class derived from an abstract class can become concrete, i.e. a class that can be instanTated, it must implement parTcular methods for all the abstract methods of its parent classes. The incomplete features of an abstract class are shared by a group of subclasses which add different variaTons of the missing pieces. Wikipedia 4/2/

Adapter Design Pa4ern: The Problem

LegacyClass exisTngRequest() NewClient OldClient NewClass request() dependency During the transiTon, how can NewClient be used with LegacyClass?

Adapter Design Pa4ern: SoluTon Class Diagram

ClientInterface request() Adapter request() LegacyClass exisTngRequest() Client abstract class shown in italic delegaTon inheritance

Bridge: Allowing for Alternate ImplementaTons

Name: Bridge design pa4ern Problem descrip1on: Decouple an interface from an implementaTon so that a different implementaTon can be subsTtuted, possibly at runTme (e.g., tesTng different implementaTons of the same interface).

Bridge: Class Diagram

Client ConcreteImplementorA ConcreteImplementorB alternaTve implementaTons

Bridge:

Class Diagram

AbstracTon Implementor Client ConcreteImplementorA ConcreteImplementorB whole/part associaTon Note the similarity to the strategy design pa4ern (described later)

Bridge: Allowing for Alternate ImplementaTons

Solu1on: The AbstracTon class defines the interface visible to the client. Implementor is an abstract class that defines the lower-­‐level methods available to AbstracTon. An AbstracTon instance maintains a reference to its corresponding Implementor instance. AbstracTon and Implementor can be refined independently.

Bridge: Consequences

Consequences: Client is shielded from abstract and concrete implementaTons Interfaces and implementaTons can be tested separately

Strategy: EncapsulaTng Algorithms

Name: Strategy design pa4ern Example: A mobile computer can be used with a wireless network, or connected to an Ethernet, with dynamic switching between networks based on locaTon and network costs. Problem descrip1on: Decouple a policy-­‐deciding class from a set of mechanisms, so that different mechanisms can be changed transparently.

Strategy Example: Class Diagram for Mobile Computer

NetworkConnecTon open() close() send() setNetworkInterface() NetworkInterface open() close() send() ApplicaTon LocaTonManager Ethernet open() close() send() WirelessNet open() close() send() use locaTon informaTon to select network

Strategy: EncapsulaTng Algorithms

Solu1on: 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.