Design Mode C # Language Description - Decoration (Decorator) Mode

zhaozj2021-02-16  45

Decorative mode is also named packaging mode to extend the functionality of the object to the client, which is an alternative to inheritance. It uses an example of a subclass of a class that is decorated, delegates the client's call to the decorative class, and the client does not think the object is different after the decoration and the decoration. Decorative mode should be used in the following situations: You need to extend a class of features or add additional responsibilities to a class. Dynamically add functions to an object, which can be reproduced again. It is necessary to increase a very large amount of functionality produced by some basic functions, so that the inheritance relationship is unrealistic.

?

The class diagram is as follows:

?

?

The decorative model includes the following roles:

Abstract Components: Give an abstract interface to standardize the object to receive additional responsibility.

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

Decorator: Hold an example of a component object and define an interface that is consistent with the abstract component interface.

Concrete Decorator: Responsible for the additional responsibility for "attaching" to the component object.

?

Component:

Public Interface Component

???? {

???????? void sampleoperation ();

????} // end interface definition component

?

Decorator:

???? public? Class Decorator: Component

???? {

???????? private company

?

???????? 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 contretedecorator1?: decorator

???? {

?

???????? public concretendecorator1 (Component): Base (Component)

???????? {

?????????????

????????}

???????? Override public? void sampleOperation ()

???????? {

???????? base.sampleOperation ();

???????? console.writeline ("Concretecorator1 SampleOperation);

????????}?

????} // End class definition concretedecorator1

?

ConcreteDecorator2:

???? public class contretedecorator2?: decorator

???? {

???????? public concretendecorator2: base (Component)

???????? {

????????}

???????? Override public void sampleOperation ()

???????? {

????????????? base.sampleOperation ();

????????????? console.writeline ("ConcreteDecorator2 Sample");

????????}

?

} // end class definition concretedecorator2

?

Client:

Static void

Main

(String [] ARGS)

???????? {

????????????? component component = new concretecomponent ();

????????????? Component Concretedecorator1 = new constledecorator1 (Component); // Packaging

???????????????????????????????????????????????????????N

?

????????????? concretedecorator2.sampleOperation ();

????}

?

The program output is as follows:

ConcreteComponent SampleOperation

ConcreteDecorator1 SampleOperation

Concretedecorator2 SampleOperation

转载请注明原文地址:https://www.9cbs.com/read-21177.html

New Post(0)