[Decorator with Design Mode Exercise C # Series]

zhaozj2021-02-16  50

Name: Decorator Chinese Name: Decorative Type: Structure Description: I want to make a song like Hou SIR when you really understand this mode. Darkness is delicate. Of course, the exquisite is mainly In the implementation of the language. Generally, this mode is used to add responsibilities to an object. Like many other modes, replace "pure" inheritance object system design. Note that "pure" can also be said to be "static "Because Decorator's implementation relies on inheritance. But it is dynamic for the addition of duties, not by pre-designing a large number of requirements for demand. Its architecture object system guidance concept is gradually" superposition "by small objects "(" Action ") constitutes a large object of the final demand. [C #] declaration: Here is due to the workout of C #. So do not use the consecutive cociation in the example:

// Created on 2003-3-18 at 20: // Environment Tool: SharpDevelop .... // Member Object: Component ConcreteComponent Decorator Condecorator

Using system;

Abstract Class Component {public abstract void draw ();

Class ConcreteComponent: Component {Private String M_STR; Public ConcreteComponent (String M_Strinparam) {m_str = m_strinparam;}

Public override void Draw () {console.writeline ("I am contreteComponent ....... / n {0}", m_str);}}

Class Decorator: Component {Protected Component DecoratRedObject;

Public void setcomponent (component m_objinparam) {decorativeObject = m_objinparam;} public override void draw () {if (decoratorEDObject! = null) DecoratorEDObject.draw ();}}

class ConcreteDecorator: Decorator {private string strDecoratorName; public ConcreteDecorator (string m_STRinparam) {strDecoratorName = m_STRinparam;} public override void Draw () {CustomDecoration (); base.Draw ();} void CustomDecoration () {Console.WriteLine ( "In ConcreteDecorator ........ "); console.writeline (" {0} ", strDecoratorname);}}

public class DesignPattern_Decorator_Test {public static void Main (string [] args) {ConcreteComponent m_DECobj1 = new ConcreteComponent ( "I am the component which will be decorated ........ / n"); ConcreteDecorator m_DECobj2 = new ConcreteDecorator ( " I will decorate the component .......... ");

m_decobj2.setcomponent (m_decobj1); Component Result = m_Decobj2; result.draw ();}}

Comments: Abstract here is also C # to the original support of abstract classes. Here is an interface because of the need for "multiple" overload. 2. In the case of Concretedecorator, you can incorporate Decorator into ConcreteDecorator. 2. Small Object gradually component large objects, in many cases, suitable use Strategy. For example, the interface defined by Component is too bloated, then the burden of Concretedecorator will be too heavy. And use small objects Strategy, step by step to implement large objects, and Strategy The advantage can also generate: Decorator diversification is achieved through a small object interface.

-------------------------------------------------- ------------------- Declaration: This program focuses on presentation C # basic usage does not consider the application of other problems in the actual environment. Such as: Multi-thread. Series articles Declaration, please see first: [Use Design Mode Exercise C # Basic Power Series] Singleton, Bridge ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------------------------------

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

New Post(0)