Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Structural patterns

2,858 views

Published on

Structural patterns (Design Pattern)

Published in: Education
  • Be the first to comment

Structural patterns

  1. 1. 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
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. Types of PatternTypes 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
  6. 6. Types of PatternTypes 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
  7. 7. AdapterAdapter • 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
  8. 8. AdapterAdapter 01/16/16 Structural Pattern 8
  9. 9. 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
  10. 10. BridgeBridge 01/16/16 Structural Pattern 10
  11. 11. CompositeComposite • 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
  12. 12. CompositeComposite 01/16/16 Structural Pattern 12
  13. 13. DecoratorDecorator • 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
  14. 14. 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
  15. 15. 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
  16. 16. 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
  17. 17. 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
  18. 18. 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
  19. 19. 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
  20. 20. FacadeFacade • 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
  21. 21. FacadeFacade 01/16/16 Structural Pattern 21
  22. 22. FlyweightFlyweight • 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
  23. 23. FlyweightFlyweight 01/16/16 Structural Pattern 23
  24. 24. ProxyProxy • 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
  25. 25. • 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
  26. 26. ProxyProxy 01/16/16 Structural Pattern 26
  27. 27. 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
  28. 28. 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

×