Design mode C # description - Decorator mode

zhaozj2021-02-16  53

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

* This article refers to some of the "Java and Mode", suitable for beginners for design patterns.

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

{

PRIVATECIENT 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 Sample");

}

} // end class definition concretecomponent

Concretedecorator1:

Public Class Concretecorator1: Decorator

{

PUBLIC ConcreteTedecorator1 (Component Component): Base (Component)

{

}

Override public void sampleOperation ()

{

Base.sampleOperation ();

Console.writeline ("ConcreteDecorator1 Sample");

}

} // End class definition concretedecorator1

ConcreteDecorator2:

Public Class Concretecorator2: Decorator

{

PUBLIC Concretedecorator2: 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 ConcreteDecorator1 (Component); // Packaging

Component Concretecorator2 = New ConcreteDecorator2 (ConcreteDecorator1);

ConcreteDecorator2.sampleOperation ();

}

The program output is as follows:

ConcreteComponent SampleOperation

ConcreteDecorator1 SampleOperation

Concretedecorator2 SampleOperation

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

New Post(0)