Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Various design patterns in software engineering, including abstract classes, adapter (wrapper), bridge, and strategy. It provides problem descriptions, examples, and class diagrams for each pattern. The sources used for this discussion are E. Gamma et al.'s book 'Design Patterns: Elements of Reusable Object-Oriented Software' and Bruegge and Dutoit's book. The document also mentions Wikipedia as a good resource for further discussion and code samples.
Typology: Lecture notes
1 / 44
William Y. Arms
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.
Design pa;erns are template designs that can be used in a variety of systems. They are parTcularly appropriate in situaTons where classes are likely to be reused in a system that evolves over Tme.
ClassName class name in italic indicates an abstract class dependency delegaTon inheritance whole/part associaTon whole part
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/
Problem descrip1on: Convert the 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 o(en occurs during a transiTonal period, when the long-‐ term plan is to phase out the legacy system. Example: How do you use a web browser to access an informaTon retrieval system that was designed for a different client?
LegacyClass exisTngRequest() NewClient OldClient NewClass request() dependency During the transiTon, how can NewClient be used with LegacyClass?
ClientInterface request() Adapter request() LegacyClass exisTngRequest() Client abstract class shown in italic delegaTon inheritance
The following consequences apply whenever the Adapter design pa4ern is used.
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).
Client ConcreteImplementorA ConcreteImplementorB alternaTve implementaTons
Implementor Client ConcreteImplementorA ConcreteImplementorB
AbstracTon Implementor Client ConcreteImplementorA ConcreteImplementorB whole/part associaTon Note the similarity to the strategy design pa4ern (described later)
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.
AbstracTon RefinedAbstracTon Implementor Client ConcreteImplementorA ConcreteImplementorB new abstracTon
Consequences: Client is shielded from abstract and concrete implementaTons Interfaces and implementaTons can be tested separately
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.
NetworkInterface open() close() send() ApplicaTon Ethernet open() close() send() WirelessNet open() close() send()
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
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.
Context contextInterface() Strategy algorithmInterface() Client ConcreteStrategy2 Policy ConcreteStrategy1 Policy class selects ConcreteStrategy Note the similarity to the bridge design pa4ern (described above)
Consequences: ConcreteStrategies can be subsTtuted transparently from Context. Policy decides which Strategy is best, given the current circumstances. New policy algorithms can be added without modifying Context or Client.
Name: Facade design pa4ern Problem descrip1on: Reduce coupling between a set of related classes and the rest of the system. 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. Solu1on: A single Facade class implements a high-‐level interface for a subsystem by invoking the methods of the lower-‐level classes.
Facade service() Class1 service1() Class2 service2() Class3 service3() Facade
Consequences: