Decoration mode.

Source: Internet
Author: User

[+]

  1. 1. Introduction to the decoration mode Brief Introduction
  2. What to solve
  3. Analysis of Three decorative Modes
    1. Decorative Pattern Structure
    2. Source code
    3. Program running result
  4. Case study example
  5. Summary
1. Introduction to the decoration mode (Brief Introduction)

Dynamically add some additional responsibilities to an object.

Advantage: The decoration function in the class can be removed from the class, which can simplify the original class. Effectively separates the core functions of the class from the decorative functional area.

 

2. What to solve)

Objects that have been developed need to expand a lot of functions to the old objects due to business needs in the future. In this case, you can dynamically add new states or actions (that is, decoration mode) to the objects) instead of using subclass static inheritance.

For example, if you just bought a car

This car does not meet your individual requirements, such as the appearance is not beautiful enough, the engine horsepower is not enough, can not meet your speed passion, so you need to decorate the car's appearance, at the same time, the engine performance needs to be improved. These operations can solve the problem through the decoration mode. The personalized car shown in is finally obtained.

3. Decorative Pattern Analysis (analysis) 1. decorative pattern structure

Component defines an object interface to dynamically add responsibilities to these objects.

Comcretecomponent specifies a specific object. You can also add responsibilities to this specific object.

The decorator abstract decoration class inherits the component object interface and extends the component class functions from the external class. However, for component, you do not need to know the existence of the decorator.

The specific decoration object of concretedecoratora is used to add responsibilities to component.

The specific decoration object of concretedecoratorb to add responsibilities to component.

2. Source Code

1. object interface component. CS

Public abstract class component

{

Public abstract void operation ();

}

 

2. The concretecomponent. CS implementation class inherits from the component interface.

Public class concretecomponent: Component

{

Public override void operation ()

{

Console. writeline ("starting to execute a specific object ...");

}

}

 

3. decorator abstract class. CS inherits from the component interface.

Public abstract class decorator: Component

{

Private component m_component;

 

Public void setcomponent (Component component)

{

This. m_component = component;

}

 

Public override void operation ()

{

If (m_component! = NULL)

{

M_component.operation ();

}

}

}

 

4. The specific decoration object concretedecoratora. CS inherits from the decorator abstract class.

Public class concretedecoratora: decorator

{

Private string addedstate;

Public override void operation ()

{

Base. Operation ();

Addedstate = "state attribute decoration. ";

Console. writeline (addedstate );

}

}

Added the state attribute addedstate to the object interface component.

 

5. The specific decoration object concretedecoratorb. CS inherits from the decorator abstract class.

Public class concretedecoratorb: decorator

{

Public override void operation ()

{

Base. Operation ();

Addedbehavior ();

}

 

Private void addedbehavior ()

{

Console. writeline ("decorated with Operation behaviors. ");

}

}

 

 

Added the operation behavior addedbehavior () to the object interface component ()

 

5. client code

Static void main (string [] ARGs)

{

Concretecomponent cc = new concretecomponent ();

Concretedecoratora CDA = new concretedecoratora ();

Concretedecoratorb CDB = new concretedecoratorb ();

 

CDA. setcomponent (CC );

CDB. setcomponent (CDA );

CDB. Operation ();

Console. Read ();

}

Added the operation behavior addedbehavior () to the object interface component ()

3. program running result

Iv. Case Analysis (example)

In this case, border and scrollbar are decorated for the textview component. Make textview more in line with project requirements.

Summary)

Decoration mode: dynamically add additional responsibilities to an object. These responsibilities need to be determined by the user. The decoration mode provides an "out-of-the-box" way to determine when to add a function during running. The decoration mode is more flexible than the subclass generation.

1. Introduction to the decoration mode (Brief Introduction)

Dynamically add some additional responsibilities to an object.

Advantage: The decoration function in the class can be removed from the class, which can simplify the original class. Effectively separates the core functions of the class from the decorative functional area.

 

2. What to solve)

Objects that have been developed need to expand a lot of functions to the old objects due to business needs in the future. In this case, you can dynamically add new states or actions (that is, decoration mode) to the objects) instead of using subclass static inheritance.

For example, if you just bought a car

This car does not meet your individual requirements, such as the appearance is not beautiful enough, the engine horsepower is not enough, can not meet your speed passion, so you need to decorate the car's appearance, at the same time, the engine performance needs to be improved. These operations can solve the problem through the decoration mode. The personalized car shown in is finally obtained.

3. Decorative Pattern Analysis (analysis) 1. decorative pattern structure

Component defines an object interface to dynamically add responsibilities to these objects.

Comcretecomponent specifies a specific object. You can also add responsibilities to this specific object.

The decorator abstract decoration class inherits the component object interface and extends the component class functions from the external class. However, for component, you do not need to know the existence of the decorator.

The specific decoration object of concretedecoratora is used to add responsibilities to component.

The specific decoration object of concretedecoratorb to add responsibilities to component.

2. Source Code

1. object interface component. CS

Public abstract class component

{

Public abstract void operation ();

}

 

2. The concretecomponent. CS implementation class inherits from the component interface.

Public class concretecomponent: Component

{

Public override void operation ()

{

Console. writeline ("starting to execute a specific object ...");

}

}

 

3. decorator abstract class. CS inherits from the component interface.

Public abstract class decorator: Component

{

Private component m_component;

 

Public void setcomponent (Component component)

{

This. m_component = component;

}

 

Public override void operation ()

{

If (m_component! = NULL)

{

M_component.operation ();

}

}

}

 

4. The specific decoration object concretedecoratora. CS inherits from the decorator abstract class.

Public class concretedecoratora: decorator

{

Private string addedstate;

Public override void operation ()

{

Base. Operation ();

Addedstate = "state attribute decoration. ";

Console. writeline (addedstate );

}

}

Added the state attribute addedstate to the object interface component.

 

5. The specific decoration object concretedecoratorb. CS inherits from the decorator abstract class.

Public class concretedecoratorb: decorator

{

Public override void operation ()

{

Base. Operation ();

Addedbehavior ();

}

 

Private void addedbehavior ()

{

Console. writeline ("decorated with Operation behaviors. ");

}

}

 

 

Added the operation behavior addedbehavior () to the object interface component ()

 

5. client code

Static void main (string [] ARGs)

{

Concretecomponent cc = new concretecomponent ();

Concretedecoratora CDA = new concretedecoratora ();

Concretedecoratorb CDB = new concretedecoratorb ();

 

CDA. setcomponent (CC );

CDB. setcomponent (CDA );

CDB. Operation ();

Console. Read ();

}

Added the operation behavior addedbehavior () to the object interface component ()

3. program running result

Iv. Case Analysis (example)

In this case, border and scrollbar are decorated for the textview component. Make textview more in line with project requirements.

Summary)

Decoration mode: dynamically add additional responsibilities to an object. These responsibilities need to be determined by the user. The decoration mode provides an "out-of-the-box" way to determine when to add a function during running. The decoration mode is more flexible than the subclass generation.

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.