Design PatternsDesign patterns are classified by two criteria:Purpose and scope.Creational: Concerns the process of object creationStructural: Deals with the composition of classes orobjectsBehavioral: Characterizes the ways in which classesor objects interact and distribute responsibility
Design PatternsPurpose: Reflects what a pattern doesScope: Specifies whether the pattern applies primarily to classes or to objec
Structural Design Patterns Deals with the composition of classes or objectsStructural patterns are concerned with how classes andobjects are composed to form larger structures.Structural class patterns use inheritance to composeinterfaces or implementations.Example:Multiple inheritance mixes two or more classes intoone.Class form of the Adapter pattern: In general, anadapter makes one interface (the adaptees) conform toanother, thereby providing a uniform abstraction ofdifferent interfaces.
Structural Design Patterns :Adapter Deals with the composition of classes or objectsAlso Known As WrapperConvert the interface of a class into another interfaceclients expect. Adapter lets classes work together thatcouldnt otherwise because of incompatible interfaces.What is an Adapter/Wrapper?For Example: Shape + TextView = TextShape (Adapter)Drawing editor: Draws lines/polygons, text; Pictures anddiagramsKey abstraction: Graphical object (which has an editable shape andcan draw itself)The interface for graphical objects: an abstract class “Shape”The editor defines a subclass of Shape for each kind of graphicalobject: a LineShape class for lines, a PolygonShape class forpolygons
Structural Design Patterns :Adapter Deals with the composition of classes or objectsTextShape subclass: Display and edit, so considerably more difficultto implement as complicated screen update and buffer managementare requiredHow to write this ‘TextShape’ subclass?User interface toolkit (Already provide a sophisticated TextView class fordisplaying and editing text)A problem: Toolkit wasnt designed with Shape classes in mind. So we cantuse TextView and Shape objects interchangeably. How can existing andunrelated classes like TextView work in an application that expectsclasses with a different and incompatible interface?Solution: A new class “TextShape” Adapts the TextView interface toShapes An Adapter (Class version-by Inheriting Object version-by composing)
Structural Design Patterns :Adapter Deals with the composition of classes or objects Identify?? Class or Object
Structural Design Patterns :Adapter Deals with the composition of classes or objects
Structural Design Patterns :Adapter Deals with the composition of classes or objects Participants Target (Shape) defines the domain-specific interface that Client uses. Client (DrawingEditor) collaborates with objects conforming to the Target interface. Adaptee (TextView) defines an existing interface that needs adapting. Adapter (TextShape) adapts the interface of Adaptee to the Target interface. Collaborations Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request.
Structural Design Patterns :Adapter Deals with the composition of classes or objectsConsequencesClass and object adapters have different trade-offs.A class adapteradapts Adaptee to Target by committing to a concrete Adapter class.As a consequence, a class adapter wont work when we want to adapta class and all its subclasses.lets Adapter override some of Adaptees behavior, since Adapter is asubclass of Adaptee.introduces only one object, and no additional pointer indirection isneeded to get to the adaptee.An object adapterlets a single Adapter work with many Adaptees—that is, the Adapteeitself and all of its subclasses (if any). The Adapter can also addfunctionality to all Adaptees at once.makes it harder to override Adaptee behavior. It will requiresubclassing Adaptee and making Adapter refer to the subclass ratherthan the Adaptee itself.
Structural Design Patterns :Adapter Deals with the composition of classes or objectsApplicabilityYou want to use an existing class, and its interface does not matchthe one you need.You want to create a reusable class that cooperates with unrelatedor unforeseen classes, that is, classes that dont necessarily havecompatible interfaces.(object adapter only) You need to use several existing subclasses,but its impractical to adapt their interface by subclassing every one.An object adapter can adapt the interface of its parent class.Other issues to consider when using the Adapter pattern:How much adapting does Adapter do?Pluggable adapters (By building interface adaptation into aclass, you eliminate the assumption that other classes see thesame interface)Using two-way adapters to provide transparency. (Two
Structural Design Patterns :Adapter Deals with the composition of classes or objectsProject Example: Data Adapters (How??)