C # Design Pattern Series Tutorials-Decorative mode _c# Tutorial

Source: Internet
Author: User
Tags abstract

1. Overview

Dynamically adding some additional responsibilities to an object, the decorative pattern is more flexible than the build subclass to add functionality.

Principle: Add a cosmetic class to wrap the original class, wrapping the way generally by using the original object as the Modifier class constructor parameters. The decoration class implements the new functionality, but it can directly invoke the methods in the original class where no new features are needed. The cosmetic class must have the same interface as the original class.

2. Role in the pattern

2.1 Abstract Build (Component): Define an abstract interface to dynamically add responsibilities to these objects.

2.2 Concrete Build (Concretecomponent): Define a specific object, or add some responsibility to this object.

2.3 Decorative Class (decorator): Decorative abstract class, inherits the component, extends the function of component class from the outer class.

2.4 Specific Decorators (Concretordecorator): Responsible for adding responsibilities to the build object.

3. Pattern Interpretation

3.1 Generalization class diagram of decoration mode

3.2 Generalization code for decorative patterns

 <summary>///defines an object interface that can dynamically add responsibilities to these objects///</summary> public abstract class Component {public Abst
 ract void Opration (); ///<summary>///Specific Objects///</summary> public class Concretecomponent:component {public override Vo
 ID opration () {//Concrete object Operations}}///<summary>///abstract Decorative class, it cannot initialize object.

  </summary> public abstract class Decorator:component {protected Component Component; <summary>///Settings Component///</summary>///<param name= "component" ></param> public V
  OID setcomponent (Component Component) {this.component = Component;
  ///<summary>///Rewrite operation, the actual execution is component operation. </summary> public override void Opration () {if (component!= null) {component.
   Opration ();  }} public class Concretedecoratora:decorator {private void Specialopration () {//This class-specific functionality} public override void Opration () {//FirstFirst run the original component operation (), perform the functions of this class, equivalent to the original component was decorated base.

   Opration (); This.
  Specialopration ();  } public class Concretedecoratorb:decorator {private void Specialoprationa () {//This class features special a} private void Specialoprationb () {//This class-specific feature B} public override void Opration () {///First run operation () of the original component, in the The function of this class, equivalent to the original component was decorated base.

   Opration (); This.
   Specialoprationa (); This.
  Specialoprationb ();

 }
 }

4. Model Summary

4.1 Advantages

4.1.1 Each decorated object is concerned only with its own function and does not need to be concerned with how it is added to the object chain. It is implemented by decorator's SetComponent method, so their responsibilities are singular.

The core responsibilities of the 4.1.2 class are separate from the dynamically added responsibilities. If you add new functionality to the main class, one violates the open closure principle and the other increases the complexity of the main class.

4.1.3 is more flexible than static inheritance the decorator model provides a more flexible way to add responsibilities to objects, using additions and separations to add and remove responsibilities at run time with decorations.

4.2 Disadvantages

4.2.1 produces many small objects, and using the decorator pattern for system design often produces many seemingly similar objects that differ only in the way they connect to each other.

4.3 Applicable scenarios

4.3.1 When you need to dynamically add more functionality to existing features.

The core features of the 4.3.2 class need not be changed, just when new features are added.

5. Application Example: Armed soldiers! Without any equipment (core function) can use fists, equipped with rifles, can be normal shooting, equipped with heavy machine guns, can fire, equipped with rocket launcher, can air defense.

5.1 Class diagram Design

5.2 Code Implementation

 <summary>///Equipment class, equivalent to component///</summary> public abstract class Equipment {public abstract V
 OID Attack (); ///<summary>///Soldier Class, inherited from equipment///</summary> public class Soldier:equipment {public soldier (
  {//constructor}///<summary>///without any weaponry under the core functions///</summary> public override void Attack () {Console.WriteLine ("Attack with fists!")
  ");

  } public abstract class Equipdecorator:equipment {protected equipment equipment;
  <summary>///Additional equipment, use this method to dynamically increase the equipment for soldiers///</summary>///<param name= "Equipment" ></param>
  public void setcomponent (equipment equipment) {this.equipment = equipment; ///<summary>///attack///</summary> public override void Attack () {//if equipped, use equipment to attack if (EQ Uipment!= null) {equipment.
   Attack ();
}}///<summary>///rifle///</summary> public class Rifleequipment:equipdecorator {  public override void Attack () {base.

   Attack (); Console.WriteLine ("Rifle shot, PA!")
  "); }///<summary>///machine gun///</summary> public class Machinegunequipment:equipdecorator {public ov erride void Attack () {base.

   Attack (); Console.WriteLine ("Machine-gun fire, sudden dodo!")
  "); }///<summary>///rocket launcher///</summary> public class Rocketgunequipment:equipdecorator {public ov erride void Attack () {base.

   Attack (); Console.WriteLine ("Rocket fire, swish ...!")
  ");

 }
 }

5.3 Client Invocation

 Class program
 {
  static void Main (string[] args)
  {
   //define recruit
   Soldier soldier = New Soldier ();

   Three kinds of equipment
   rifleequipment rifle = new Rifleequipment ();
   Machinegunequipment machinegun = new machinegunequipment ();
   Rocketgunequipment Rocketgun = new Rocketgunequipment ();

   All three kinds of equipment were given to the recruits
   rifle. SetComponent (soldier);
   Machinegun.setcomponent (rifle);
   Rocketgun.setcomponent (machinegun);

   Attacks, in addition to fist kungfu, recruits can also use rifles, machine guns, rockets. The final execution is Rocketgun.attack ().
   Rocketgun.attack ();

   Console.read ();
  }
 

5.4 Running Results

Hit him with fists!
Rifle shot, PA!
Machine-gun fire, sudden dodo!
Rocket fire, swish ...!

The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.

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.