software design patterns

17
Software Design Patterns Structural Design Patterns By Mohammed Haneef Mohammed Haneef 1M09IS054 ISE, 8’B’

Upload: mohammed-haneef

Post on 09-Nov-2014

159 views

Category:

Documents


3 download

DESCRIPTION

Topic : Structural design patterns

TRANSCRIPT

Page 1: Software Design Patterns

Software Design Patterns

Structural Design Patterns

By Mohammed Haneef

Mohammed Haneef1M09IS054ISE, 8’B’

Page 2: Software Design Patterns

Structural Design Patterns

• Structural Design Patterns are concerned with how classes and objects are composed together to form larger structures.

• These patterns ease the design of a program by identifying a simple way to realize relationships between entities.

• Structural class patterns use inheritance to compose interfaces or implementations.

• Structural object patterns describe ways to compose objects to realize new functionality.

By Mohammed Haneef

Page 3: Software Design Patterns

Types of Structural Design Patterns

• Adapter:– Adapts an interface for a class into one that a client

expects.

• Bridge:– Abstraction for binding one of many implementations– Decouple an abstraction from its implementation so that,

two can vary independently.

• Decorator:– Add additional functionality to a class at runtime, i.e Attach

additional responsibilities to an object dynamically.

• Facade:– Simplifies the interface for a subsystem

By Mohammed Haneef

Page 4: Software Design Patterns

Adapter Pattern

• An adapter helps two incompatible interfaces to work together, This is the real world definition for an adapter.

• Adapter design pattern is used when you want two different classes with incompatible interfaces to work together.

• Interfaces may be incompatible but the inner functionality should suit the need.

• An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface.

• The adapter is also responsible for transforming data into appropriate forms.

By Mohammed Haneef

Page 5: Software Design Patterns

Adapter Pattern

Implementation of adapter design pattern

• Adapter design pattern can be implemented in two ways.

1. using the inheritance method.2. using the composition method.

By Mohammed Haneef

Page 6: Software Design Patterns

Adapter Pattern

1. Implementation using Inheritance Method

• When a class with incompatible method needs to be used with another class you can use inheritance to create an adapter class.

• The adapter class which is inherited will have new compatible methods.

• Using those new methods from the adapter the core function of the base class will be accessed. This is called “is-a” relationship.

By Mohammed Haneef

Page 7: Software Design Patterns

Adapter Pattern

2. Implementation using Composition Method

• Implementation using composition can be done by inheriting the base class by creating adapter and by having the base class as attribute inside the adapter.

• We can access all the methods by having it as an attribute. This is nothing but “has-a” relationship.

• In most scenarios, prefer composition over inheritance.

• Using composition you can change the behaviour of class easily if needed.

• It enables the usage of tools like dependency injection.

By Mohammed Haneef

Page 8: Software Design Patterns

Bridge Pattern

• Decouple an abstraction from its implementation so that two can vary independently. In strategy pattern we decouple the behavior but in Bridge we decouple the abstraction.

• Creates two different hierarchies. One for abstraction and another for implementation.

• Avoids permanent binding by removing the dependency between abstraction and implementation.

• We create a bridge that coordinates between abstraction and implementation.

• Abstraction and implementation can be extended separately.• Should be used when we have need to switch implementation at

runtime.• Client should not be impacted if there is modification in

implementation of abstraction.• Best used when you have multiple implementations.

By Mohammed Haneef

Page 9: Software Design Patterns

Bridge Pattern

Elements of Bridge Design Pattern

• Abstraction – core of the bridge design pattern and defines the crux. Contains a reference to the implementer.

• Refined Abstraction – Extends the abstraction takes the finer detail one level below. Hides the finer elements from implemetors.

• Implementer - This interface is the higer level than abstraction. Just defines the basic operations.

• Concrete Implementation – Implements the above implementer by providing concrete implementation.

By Mohammed Haneef

Page 10: Software Design Patterns

Bridge Pattern

By Mohammed Haneef

Before Bridge Design Pattern After Bridge Design Pattern

Page 11: Software Design Patterns

Decorator Pattern

• To extend or modify the behaviour of ‘an instance’ at runtime decorator design pattern is used.

• Inheritance is used to extend the abilities of ‘a class’.

• Unlike inheritance, you can choose any single object of a class and modify its behaviour leaving the other instances unmodified.

• In implementing the decorator pattern you construct a wrapper around an object by extending its behavior.

• The wrapper will do its job before or after and delegate the call to the wrapped instance

By Mohammed Haneef

Page 12: Software Design Patterns

Decorator Pattern

• The wrapping could be achieved by the following sequence of steps:

1. Subclass the original "Component" class into a "Decorator" class (see UML diagram).

2. In the Decorator class, add a Component pointer as a field.

3. Pass a Component to the Decorator constructor to initialize the Component pointer.

4. In the Decorator class, redirect all "Component" methods to the "Component" pointer. and

5. In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.

By Mohammed Haneef

Page 13: Software Design Patterns

Decorator Pattern

• Decorator UML class diagram

By Mohammed Haneef

Page 14: Software Design Patterns

Facade Pattern

• Facade provides a single interface.

• Programmers comfort is a main purpose of facade.

• Simplicity is the aim of facade pattern.

• Facade design pattern is used for promoting subsystem independence and portability.

• Subsystem may be dependent with one another. In such case, facade can act as a coordinator and decouple the dependencies between the subsystems.

• Translating data to suit the interface of a subsystem is done by the facade.

By Mohammed Haneef

Page 15: Software Design Patterns

Facade Pattern

By Mohammed Haneef

• A common design goal is to minimize the communication and dependencies between subsystems.

• As you can see from the simplified diagram below the facade pattern is one way to achieve this, it provides a unified class which reduces communication between the complex subsystem and its clients.

• Clients do not have to be aware of the actual implementation of the subsystem and are provided with a simpler interface to this subsystem.

Page 16: Software Design Patterns

Facade Pattern

By Mohammed Haneef

Applicability / Uses

• You want to provide a simple interface to a complex subsystem. The application of design patterns often results in a lot of small classes which makes subsystems more flexible and customizable. A facade can provide a default view for clients which don't need to customize.

• You want to reduce coupling between clients-subsystems or subsystems-subsystems.

• You want to layer your subsystems. Use a facade to define an entry point to each subsystem level and make them communicate only through their facades, this can simplify the dependencies between them.

Page 17: Software Design Patterns

Thank You

• Presentation By

Mohammed Haneef

1MS09IS054

ISE 8’B’

M.S.R.I.T

By Mohammed Haneef