Structural patterns

himanshuhora 6,812 views 28 slides Jan 16, 2016
Slide 1
Slide 1 of 28
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28

About This Presentation

Structural patterns (Design Pattern)


Slide Content

Structural PatternStructural Pattern
Dr. Himanshu Hora
SRMS College of Engineering & Technology
Bareilly (UP) INDIA
Dr. Himanshu Hora
SRMS College of Engineering & Technology
Bareilly (UP) INDIA
01/16/16
Structural Pattern
1

ContentContent
•History of Design Pattern
•Definitions of Design Pattern
•Types of Pattern
•Adapter
•Bridge
•Composite
•Decorator
•Facade
•Flyweight
•Proxy
•Benefits and Possible problems
•History of Design Pattern
•Definitions of Design Pattern
•Types of Pattern
•Adapter
•Bridge
•Composite
•Decorator
•Facade
•Flyweight
•Proxy
•Benefits and Possible problems
01/16/16 Structural Pattern 2

History of Design PatternHistory of Design Pattern
•In 1994, Design Patterns: Elements of Reusable Object-
Oriented Software by Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides explained the usefulness of
patterns and resulted in the widespread popularity of design
patterns.
•These four authors together are referred to as the Gang of Four
(GoF).
•In 1994, Design Patterns: Elements of Reusable Object-
Oriented Software by Erich Gamma, Richard Helm, Ralph
Johnson and John Vlissides explained the usefulness of
patterns and resulted in the widespread popularity of design
patterns.
•These four authors together are referred to as the Gang of Four
(GoF).
01/16/16 Structural Pattern 3

Definitions of Design PatternDefinitions of Design Pattern

•Design patterns are recurring solutions to software design
problems you find again and again in real-world application
development
OR
•Design patterns represent solutions to problems that arise
when developing software within a particular context
OR
•Design patterns are standard solutions to common problems in
software design

•Design patterns are recurring solutions to software design
problems you find again and again in real-world application
development
OR
•Design patterns represent solutions to problems that arise
when developing software within a particular context
OR
•Design patterns are standard solutions to common problems in
software design
01/16/16 Structural Pattern 4

Types of Pattern Types of Pattern
There are 3 types of pattern
•Creational: address problems of creating an object in a
flexible way. Separate creation, from operation/use.
•Structural: address problems of using O-O constructs like
inheritance to organize classes and objects
•Behavioral: address problems of assigning responsibilities to
classes. Suggest both static relationships and patterns of
communication (use cases)
There are 3 types of pattern
•Creational: address problems of creating an object in a
flexible way. Separate creation, from operation/use.
•Structural: address problems of using O-O constructs like
inheritance to organize classes and objects
•Behavioral: address problems of assigning responsibilities to
classes. Suggest both static relationships and patterns of
communication (use cases)
01/16/16 Structural Pattern 5

Types of Pattern Types of Pattern
Creational Patterns
(concerned with abstracting the object-instantiation process)
•Factory Method Abstract Factory Singleton
•Builder Prototype
Structural Patterns
(concerned with how objects/classes can be combined to form larger
structures)
•Adapter Bridge Composite
•Decorator Facade Flyweight
•Proxy
Behavioral Patterns
(concerned with communication between objects)
•Command Interpreter Iterator
•Mediator Observer State
•Strategy Chain of Responsibility Visitor
•Template Method
Creational Patterns
(concerned with abstracting the object-instantiation process)
•Factory Method Abstract Factory Singleton
•Builder Prototype
Structural Patterns
(concerned with how objects/classes can be combined to form larger
structures)
•Adapter Bridge Composite
•Decorator Facade Flyweight
•Proxy
Behavioral Patterns
(concerned with communication between objects)
•Command Interpreter Iterator
•Mediator Observer State
•Strategy Chain of Responsibility Visitor
•Template Method
01/16/16 Structural Pattern 6

Adapter Adapter
•Convert the interface of a class into another interface clients
expect
•Adapter lets classes work together that couldn't otherwise
because of incompatible interfaces
•Use the Adapter pattern when:
–you want to use an existing class and its interface does not
match the one you need
–you need to use several existing subclasses, but it's
impractical to adapt their interface by subclassing
everyone. An object adapter can adapt the interface of its
parent class
•Convert the interface of a class into another interface clients
expect
•Adapter lets classes work together that couldn't otherwise
because of incompatible interfaces
•Use the Adapter pattern when:
–you want to use an existing class and its interface does not
match the one you need
–you need to use several existing subclasses, but it's
impractical to adapt their interface by subclassing
everyone. An object adapter can adapt the interface of its
parent class
01/16/16 Structural Pattern 7

Adapter Adapter
01/16/16 Structural Pattern 8

BridgeBridge
•Decouple an abstraction from its implementation so that the
two can vary independently
•Use the Bridge pattern when:
–you want run-time binding of the implementation
–you want to share an implementation among multiple
objects
•Decouple an abstraction from its implementation so that the
two can vary independently
•Use the Bridge pattern when:
–you want run-time binding of the implementation
–you want to share an implementation among multiple
objects
01/16/16 Structural Pattern 9

BridgeBridge
01/16/16 Structural Pattern 10

Composite Composite
•Compose objects into tree structures to represent whole-part
hierarchies. Composite lets clients treat individual objects and
compositions of objects uniformly
•Use this pattern whenever you have "composites that contain
components, each of which could be a composite".
•Compose objects into tree structures to represent whole-part
hierarchies. Composite lets clients treat individual objects and
compositions of objects uniformly
•Use this pattern whenever you have "composites that contain
components, each of which could be a composite".
01/16/16 Structural Pattern 11

Composite Composite
01/16/16 Structural Pattern 12

Decorator Decorator
•Attach additional responsibilities to an object dynamically
•Decorators provide a flexible alternative to subclassing for
extending functionality
•Attach additional responsibilities to an object dynamically
•Decorators provide a flexible alternative to subclassing for
extending functionality
01/16/16 Structural Pattern 13

ProblemsProblems
•Several classes with a similar operation (method), but
different behavior.
•We want to use many combinations of these behaviors
•Several classes with a similar operation (method), but
different behavior.
•We want to use many combinations of these behaviors
01/16/16 Structural Pattern 14

Example - Automated HospitalExample - Automated Hospital
•People come to the reception with problems
•They describe their problems
•A special doctoRobot is created that is specialized to treat
their special situations.
•People come to the reception with problems
•They describe their problems
•A special doctoRobot is created that is specialized to treat
their special situations.
01/16/16 Structural Pattern 15

Automated Hospital - Solution 1Automated Hospital - Solution 1
DoctoRobot
Cure (p : Patient)
DentistRobot
Cure (p : Patient)
DermaRobot
Cure (p : Patient)
PsychoRobot
Cure (p : Patient)
DentistDermaRobot DentistPsychoRobot DermaPsychoRobot
01/16/16 Structural Pattern 16

Problems with solution-1Problems with solution-1
•Sometimes we don’t have multiple inheritance.
•Even if we have, if is problematic, and bad design.
•Sometimes we don’t have multiple inheritance.
•Even if we have, if is problematic, and bad design.
01/16/16 Structural Pattern 17

A Better idea: Use DecoratorA Better idea: Use Decorator
ConcreteComponent
Operation( )
ConcreteDecoratorA
addedState
Operation( )
ConcreteDecoratorB
Operation( )
Decorator
Operation( )
Component
Operation( )
component
01/16/16 Structural Pattern 18

Decorator in our caseDecorator in our case
DentistRobot
Cure (p : Patient)
DermaRobot
Cure (p : Patient)
PhsychoRobot
Cure (p : Patient)
DoctorRobotDecorator
innerDoctor
Cure (p : Patient)
DoctoRobot
Cure (p : Patient)
component
01/16/16 Structural Pattern 19

Facade Facade
•Provide a unified interface to a set of interfaces in a subsystem
•Facade defines a higher-level interface that makes the
subsystem easier to use
•Create a class that is the interface to the subsystem
•Clients interface with the Facade class to deal with the
subsystem
•It hides the implementation of the subsystem from clients
•It promotes weak coupling between the subsystems and its
clients
•It does not prevent clients from using subsystems class, should
it?
•Provide a unified interface to a set of interfaces in a subsystem
•Facade defines a higher-level interface that makes the
subsystem easier to use
•Create a class that is the interface to the subsystem
•Clients interface with the Facade class to deal with the
subsystem
•It hides the implementation of the subsystem from clients
•It promotes weak coupling between the subsystems and its
clients
•It does not prevent clients from using subsystems class, should
it?
01/16/16 Structural Pattern 20

Facade Facade
01/16/16 Structural Pattern 21

Flyweight Flyweight
•Use sharing to support large numbers of fine-grained objects
efficiently
•The pattern can be used when:
–The program uses a large number of objects and
–The program does not use object identity (==)
•Use sharing to support large numbers of fine-grained objects
efficiently
•The pattern can be used when:
–The program uses a large number of objects and
–The program does not use object identity (==)
01/16/16 Structural Pattern 22

Flyweight Flyweight
01/16/16 Structural Pattern 23

Proxy Proxy
•Provide a surrogate or placeholder for another object to
control access to it.
•The proxy has the same interface as the original object
•Virtual Proxy:
–Creates/accesses expensive objects on demand
–You may wish to delay creating an expensive object until it
is really accessed
–It may be too expensive to keep entire state of the object in
memory at one time
•Provide a surrogate or placeholder for another object to
control access to it.
•The proxy has the same interface as the original object
•Virtual Proxy:
–Creates/accesses expensive objects on demand
–You may wish to delay creating an expensive object until it
is really accessed
–It may be too expensive to keep entire state of the object in
memory at one time
01/16/16 Structural Pattern 24

•Protection Proxy
–Provides different objects different level of access to
original object
•Cache Proxy (Server Proxy)
–Multiple local clients can share results from expensive
operations: remote accesses or long computations
•Firewall Proxy
–Protect local clients from outside world
•Protection Proxy
–Provides different objects different level of access to
original object
•Cache Proxy (Server Proxy)
–Multiple local clients can share results from expensive
operations: remote accesses or long computations
•Firewall Proxy
–Protect local clients from outside world
01/16/16 Structural Pattern 25

Proxy Proxy
01/16/16 Structural Pattern 26

Benefits
•Flexible
•Don’t have to foresee all combinations
•Little objects
Possible problems
•Performance
•Decorators are not necessarily always cummutative
(surgeon and Anastasiolic)
01/16/16 Structural Pattern 27

Thank You
01/16/16 Structural Pattern 28
Dr. Himanshu Hora
SRMS College of Engineering & Technology
Bareilly (UP) INDIA
Dr. Himanshu Hora
SRMS College of Engineering & Technology
Bareilly (UP) INDIA