结构型模式,装饰模式
Definition
UML class diagram
Participants
Sample code in C#
This structural code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object.
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. |
Participants
The classes and/or objects participating in the Decorator pattern are:
|
This structural code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object.
// Decorator pattern -- Structural example |
using System;
{ // Methods abstract public void Operation(); }
{ // Methods override public void Operation() { Console.WriteLine("ConcreteComponent.Operation()"); } }
{ // Fields protected Component component;
// Methods public void SetComponent( Component component ) { this.component = component; }
{ if( component != null ) component.Operation(); } }
{ // Fields private string addedState;
// Methods override public void Operation() { base.Operation(); addedState = "new state"; Console.WriteLine("ConcreteDecoratorA.Operation()"); } }
{ // Methods override public void Operation() { base.Operation(); AddedBehavior(); Console.WriteLine("ConcreteDecoratorB.Operation()"); }
{ } }
/// Client test /// </summary> public class Client { public static void Main( string[] args ) { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB();
// Link decorators d1.SetComponent( c ); d2.SetComponent( d1 ); d2.Operation(); } } |
This real-world code demonstrates the Decorator pattern in which 'borrowable' functionality is added to existing library items (books and videos).
// Decorator pattern -- Real World example |
using System; using System.Collections;
{ // Fields private int numCopies; // Properties
{ get{ return numCopies; } set{ numCopies = value; } }
// Methods public abstract void Display(); }
{ // Fields private string author; private string title;
// Constructors public Book(string author,string title,int numCopies) { this.author = author; this.title = title; this.NumCopies = numCopies; }
// Methods public override void Display() { Console.WriteLine( "\nBook ------ " ); Console.WriteLine( " Author: {0}", author ); Console.WriteLine( " Title: {0}", title ); Console.WriteLine( " # Copies: {0}", NumCopies ); } }
{ // Fields private string director; private string title; private int playTime;
// Constructor public Video( string director, string title, int numCopies, int playTime ) { this.director = director; this.title = title; this.NumCopies = numCopies; this.playTime = playTime; }
// Methods public override void Display() { Console.WriteLine( "\nVideo ----- " ); Console.WriteLine( " Director: {0}", director ); Console.WriteLine( " Title: {0}", title ); Console.WriteLine( " # Copies: {0}", NumCopies ); Console.WriteLine( " Playtime: {0}", playTime ); } }
{ // Fields protected LibraryItem libraryItem;
// Constructors public Decorator ( LibraryItem libraryItem ) { this.libraryItem = libraryItem; }
// Methods public override void Display() { libraryItem.Display(); } }
{ // Fields protected ArrayList borrowers = new ArrayList();
// Constructors public Borrowable( LibraryItem libraryItem ) : base( libraryItem ) {}
// Methods public void BorrowItem( string name ) { borrowers.Add( name ); libraryItem.NumCopies--; }
{ borrowers.Remove( name ); libraryItem.NumCopies++; }
{ base.Display(); foreach( string borrower in borrowers ) Console.WriteLine( " borrower: {0}", borrower ); } } /// <summary> /// DecoratorApp test /// </summary> public class DecoratorApp { public static void Main( string[] args ) { // Create book and video and display Book book = new Book( "Schnell", "My Home", 10 ); Video video = new Video( "Spielberg", "Schindler's list", 23, 60 ); book.Display(); video.Display();
// Make video borrowable, then borrow and display Console.WriteLine( "\nVideo made borrowable:" ); Borrowable borrowvideo = new Borrowable( video ); borrowvideo.BorrowItem( "Cindy Lopez" ); borrowvideo.BorrowItem( "Samuel King" );
} |