Design Pattern C # Description--Decorative (decorator) die

Source: Internet
Author: User
Tags abstract definition class definition inheritance client
Design design Pattern C # language Description--decoration (decorator) mode



* This article refers to "Java and the pattern" part of the content, suitable for the design pattern of beginners.



The decorative pattern, also known as the wrapper pattern, extends the function of the object transparently to the client, and is an alternative to the inheritance relationship. It uses an instance of a subclass of the previously decorated class to delegate the client's invocation to the decorated class, and the client does not feel that the object is different before and after the decoration. You should use decorative mode when you need to extend the functionality of a class or add additional responsibility to a class. Dynamically adding functionality to an object, these functions can be undone dynamically. The need to increase the number of functions generated by the permutations of some basic functions makes the inheritance relationship impractical.



The class diagram looks like this:






The decoration mode includes the following roles:

Abstract widget (Component): gives an abstract interface to the specification of the object to receive additional responsibility.

Concrete Widget (Concrete Component): Defines a class that will receive additional responsibility.

Decoration (decorator): Holds an instance of a Component object and defines an interface that is consistent with an abstract component interface.

Concrete Decoration (concrete decorator): Responsible for "affixed" to the Component Object "additional responsibility".



Component:

public interface Component

{

void Sampleoperation ();

}//End INTERFACE DEFINITION Component



Decorator:

public class Decorator:component

{

Private Component Component;



Public decorator (Component Component)

{

This.component=component;

}



public virtual void sampleoperation ()

{

Component.sampleoperation ();

}



}//End CLASS DEFINITION Decorator



Concretecomponent:

public class Concretecomponent:component

{



public void Sampleoperation ()

{

Console.WriteLine ("Concretecomponent sampleoperation");

}



}//End CLASS DEFINITION concretecomponent



ConcreteDecorator1:

public class Concretedecorator1:decorator

{



Public ConcreteDecorator1 (Component Component): Base (Component)

{



}

Override public void Sampleoperation ()

{

Base.sampleoperation ();

Console.WriteLine ("ConcreteDecorator1 sampleoperation");

}



}//End CLASS DEFINITION ConcreteDecorator1



ConcreteDecorator2:

public class Concretedecorator2:decorator

{

Public ConcreteDecorator2 (Component Component): Base (Component)

{

}

Override public void Sampleoperation ()

{

Base.sampleoperation ();

Console.WriteLine ("ConcreteDecorator2 sampleoperation");

}



}//End CLASS DEFINITION ConcreteDecorator2



Client:

static void Main (string[] args)

{

Component component=new concretecomponent ();

Component concretedecorator1=new ConcreteDecorator1 (Component); Packaging

Component concretedecorator2=new ConcreteDecorator2 (Concretedecorator1);



Concretedecorator2.sampleoperation ();

}



The program output is as follows:

Concretecomponent sampleoperation

ConcreteDecorator1 sampleoperation

ConcreteDecorator2 sampleoperation


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.