Structural Design Pattern

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

CS6502

OBJECT ORIENTED ANALYSIS


AND DESIGN

OOAD is a software engineering approach

that models and designs a system

as a group of interacting objects.


UNIT II DESIGN PATTERNS

GRASP: Designing objects with responsibilities –


Creator – Information expert – Low Coupling – High
Cohesion – Controller - Design Patterns –
creational - factory method - structural – Bridge –
Adapter - behavioral – Strategy – observer
Types of Design Pattern
Structural Design Pattern

Design patterns that deal with


how classes and objects are composed
to form larger structures.
Structural Design Pattern

• Structural class patterns concern class and object composition.

• Structural class patterns


– Use inheritance to compose interfaces or implementations.

– Define ways to compose objects to obtain new functionality.

• A simple example:
Consider how multiple inheritance mixes two or more classes into one.
– The result is a class that combines the properties of its parent classes.

• This pattern is particularly useful for


– Making independently developed class libraries work together.
Types of Structural Design Patterns

• Adapter allows classes with incompatible interfaces to work together by


wrapping its own interface around that of an already existing class.

• Bridge decouples an abstraction from its implementation so that the two


can vary independently.

• Composite composes zero-or-more similar objects so that they can be


manipulated as one object.

• Decorator dynamically adds/overrides behaviour in an existing method of


an object.
Types of Structural Design Patterns

• Facade provides a simplified interface to a large body of code.

• Flyweight reduces the cost of creating and manipulating a large number


of similar objects.

• Proxy provides a placeholder for another object to control access, reduce


cost, and reduce complexity.
Adaptor

• Adapter pattern works as a bridge between two incompatible interfaces.

• This type of design pattern comes under structural pattern


– As this pattern combines the capability of two independent interfaces.

• This pattern involves


– A single class which is responsible to join functionalities of independent or
incompatible interfaces.

• In real world we have adapters for power supplies, adapters for camera
memory cards, and so on
Adaptor

• Allows the interface of an existing class to be used as another interface.

• It is often used
– To make existing classes work with others without modifying their source code.

• Intent
– Convert the interface of a class into another interface clients expect.

– Adapter lets classes work together, that could not otherwise because of
incompatible interfaces.
Adaptor
• Implementation

The classes/objects participating in adapter pattern:


Target - defines the domain-specific interface that Client uses.
Adapter - adapts the interface Adaptee to the Target interface.
Adaptee - defines an existing interface that needs adapting.
Client - collaborates with objects conforming to the Target interface.
Adaptor
• An audio player device can play mp3 files only
– Wants to use an advanced audio player capable of playing vlc and mp4 files.
Adapter class MediaAdapter
implements the MediaPlayer interface & Client
uses AdvancedMediaPlayer objects
to play the required format.

Adaptee

Target
Adapter
Bridge

• Also known as Handle/Body

• Intent
– Decouple an abstraction from its implementation

So that the two can vary Independently

• Bridge pattern
– Separates the abstract elements of a class from its technical implementation.

• This allows the implementation details to be changed easily.


Bridge

• The class itself can be thought of as the abstraction


– And what the class can do as the implementation.

• Bridge pattern decouples implementation class and abstract class


– By providing a bridge structure b/w them.

• Bridge uses encapsulation, aggregation, and can use inheritance


– To separate responsibilities into different classes

• Bridge pattern is useful when both the class and what it does vary often.
Bridge
• Implementation

• Abstraction (abstract class) – defines the abstract interface

– Maintains the Implementor reference.

• RefinedAbstraction (normal class) – extends the interface defined by Abstraction


• Implementor (interface) – defines the interface for implementation classes
• ConcreteImplementor (normal class) - implements the Implementor interface
Bridge - Example

Before Bridge Design Pattern


After Bridge Design Pattern
Bridge
• A circle can be drawn in different colors using same abstract class method
– But different bridge implementer classes.

Implementor
Abstraction

Client

Refined Abstration concrete classes

You might also like