duminică, 22 noiembrie 2009

[DesignPatterns] Decorator Pattern

The role of the Decorator pattern is to provide a way of attaching new state and behavior to an object dynamically.
Decorator pattern takes an existing object and adds to it.

• The original object is unaware of any decorations.
• There 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.

Components:
- Client (Component and Decorator has a Client)
- IComponent
- Component
- Decorator - extends Component + usually has a ctor that take as parameter an IComponent

Stat _stat = new Stat();
Tag _tag = new Tag (_stat, "Chassis1", "Card2", "Port3" );

The Decorator pattern’s key feature is that it does not rely on inheritance for extending behavior.
• Implement any methods in the interface, changing the initial behavior of the component
• Add any new state and behavior
• Access any public members via the object passed at construction

Uses:
- Decorator pattern fits well in the graphics world.
- I/O APIs of C#:
System.IO.Stream
System.IO.BufferedStream
System.IO.FileStream
System.IO.MemoryStream
System.Net.Sockets.NetworkStream
System.Security.Cryptography.CryptoStream
- web browsers and other mobile applications
- controls in Windows app




Niciun comentariu:

Trimiteți un comentariu