Mais conteúdo relacionado Semelhante a Design Patterns - 01 Introduction and Decorator Pattern (20) Design Patterns - 01 Introduction and Decorator Pattern3. Design Patterns - Definition Definition: a general reusable solution to a commonly occurring problem in software design (Wikipedia). Not a finished design Description or template for how to solve a problem that can be used in many different situations Shows relationships and interactions between classes and/or objects Not all software patterns are design patterns (e.g. algorithms) 4. Design Patterns – Classification Structural Pattern Ease the design by identifying a simple way to realize relationships between entities E.g. Decorator, Proxy Creational Pattern Deal with object creation mechanism E.g. Abstract Factory, Singleton Behavioral Pattern Deal with common communication between objects. E.g. Chain of Responsibility, Command Concurrency Pattern Deal with multi-threaded programming paradigm. E.g. Monitor Object, Thread Pool Not covered during this series © Prafulla Paraskar 2010 11. Decorator Pattern – Explained (1/2) Photo + Frame + Caption Guptas (2010) © Prafulla Paraskar 2010 12. Decorator Pattern – Explained (2/2) Adds functionality at Runtime. The object does not know it is being “decorated”. Three is no one big feature-laden class with all the options in it. The decorations are independent of each other. The decorations can be composed together in a mix-and-match fashion. © Prafulla Paraskar 2010 14. Decorator Pattern – Real World Samples Graphics world (as illustrated). I/O namespace of .NET System.IO.Stream System.IO.BufferedStream System.IO.FileStream System.IO.MemoryStream Cross platform applications (Mobile/Desktop) Actual decorator classes in .NET 3.0 System.Windows.Controls (Base Class) Border (Decorator) Viewbox (Decorator) © Prafulla Paraskar 2010 15. Decorator Pattern – Guidelines (1/2) You have: An existing component class that may be unavailable for subclassing. You want to: Attach additional state or behavior to an object dynamically. Make Changes to some objects of a class without affecting others. Avoid subclassing because too many classes could result. © Prafulla Paraskar 2010 16. Decorator Pattern – Guidelines (2/2) But consider using instead: The Adapter Pattern Sets up an interface between different classes The Composite Pattern Aggregates an object without also inheriting its interface. The Proxy Pattern Specifically controls access to objects. The Strategy Pattern Changes the original object rather than wrapping it. © Prafulla Paraskar 2010