Decoration mode in design mode: decoration mode in Design Mode

Source: Internet
Author: User

Decoration mode in design mode: decoration mode in Design Mode

1. Class Diagram

2. Create a project

..............................

3. VisualComponent: abstract interface component class, acting as abstract component class

To highlight the core Code related to the mode, the space code is simplified in basic instances.

Namespace DecoratorSample

{

/// <Summary>

/// Abstract component class

/// </Summary>

Abstract class VisualComponent

{

/// <Summary>

/// Abstract method: Display Control

/// </Summary>

Public abstract void Display ();

}

}

4. Window: Form class, acting as a specific component class

Using System;

Namespace DecoratorSample

{

/// <Summary>

/// Specific component class: Windows, which implements the basic functions of the build class

/// </Summary>

Class Window: VisualComponent

{

Public override void Display ()

{

Console. WriteLine ("form component -- display form! ");

}

}

}

5. TextBox: TextBox class, which acts as a component class.

Using System;

Namespace DecoratorSample

{

/// <Summary>

/// Specific component class: TextBox, which implements the basic functions of the build class

/// </Summary>

Class TextBox: VisualComponent

{

Public override void Display ()

{

Console. WriteLine ("display text box! ");

}

}

}

6. ListBox: ListBox class, acting as a specific component class

Using System;

Namespace DecoratorSample

{

/// <Summary>

/// Specific component class: ListBox, which implements the basic functions of the build class

/// </Summary>

Class ListBox: VisualComponent

{

Public override void Display ()

{

Console. WriteLine ("display list box! ");

}

}

}

7. ComponentDecorator: component decoration class that acts as an abstract decoration class.

Namespace DecoratorSample

{

/// <Summary>

/// Abstract decoration class, which is a subclass of the abstract component class and is responsible for implementing complex functions of the component.

/// </Summary>

Class ComponentDecorator: VisualComponent

{

Private VisualComponent component; // maintain reference to the abstract component type object

 

/// <Summary>

/// Inject an object of the abstract component type, that is, inject a specific component class. This constructor is very important.

/// </Summary>

/// <Param name = "component"> </param>

Public ComponentDecorator (VisualComponent component)

{

This. component = component;

}

/// <Summary>

/// Implement the basic functions of the injection component

/// </Summary>

Public override void Display ()

{

Component. Display (); // call the method of injecting a specific component

}

}

}

8. ScrollBarDecorator: scroll bar decoration class, acting as a specific decoration class

Using System;

Namespace DecoratorSample

{

/// <Summary>

/// Scroll bar decoration class, which is a specific decoration class

/// </Summary>

Class ScrollBarDecorator: ComponentDecorator

{

Public ScrollBarDecorator (VisualComponent component)

: Base (component)

{

}

/// <Summary>

/// Rewrite the basic method of the base class to implement the decoration of the decorative component: that is, the basic method for implementing the component and the newly added method.

/// </Summary>

Public override void Display ()

{

This. SetScrollBar ();

Base. Display ();

}

/// <Summary>

/// Set the scroll bar. This method is a new method added to the decoration.

/// </Summary>

Public void SetScrollBar ()

{

Console. WriteLine ("add a scroll bar for the component! ");

}

}

}

9. BlackBorderDecorator: black border decoration class, acting as a specific decoration class

Using System;

Namespace DecoratorSample

{

/// <Summary>

/// Decoration class with a black border, which is a subclass of the abstract component class

/// </Summary>

Class BlackBorderDecorator: ComponentDecorator

{

Public BlackBorderDecorator (VisualComponent component)

: Base (component)

{

}

/// <Summary>

/// Rewrite the basic method of the base class to implement the decoration of the decorative component: that is, the basic method for implementing the component and the newly added method.

/// </Summary>

Public override void Display ()

{

This. SetBlackBorder ();

Base. Display ();

}

/// <Summary>

/// Newly added method to implement black border

/// </Summary>

Public void SetBlackBorder ()

{

Console. WriteLine ("add a black border for the component! ");

}

}

}

10. Program: client test class

Using System;

Namespace DecoratorSample

{

Class Program

{

Static void Main (string [] args)

{

// The following shows how to use transparent decoration.

 

VisualComponent component; // abstract component

VisualComponent componentScrollBar; // decorative component with a scroll bar

VisualComponent componentBlackBorder; // decorative component with a black border

Component = new Window (); // defines the specific component

Console. WriteLine ("prepare component" + component. ToString ());

ComponentScrollBar = new ScrollBarDecorator (component); // you can specify the component to be decorated as a scroll bar.

ComponentBlackBorder = new BlackBorderDecorator (componentScrollBar); // inject the objects decorated once into another decoration class for the second decoration

ComponentBlackBorder. Display ();

Console. Read ();

}

}

}

11. Results and Analysis

 

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.