Traditional design pattern (iii) decorator pattern

Source: Internet
Author: User
Tags abstract tostring

When it comes to the project instance of this pattern bugs are also full of headaches. The so-called decorator pattern plainly attaches responsibility to the object dynamically. If you need a feature extension in a project scenario that derives a lot of subclasses from the base class, then the decorator pattern is certainly good. But in fact, in the actual project, often we do not directly derivative subclasses, but through the combination of the way, according to the logic of the various expansion of the superposition, the external disclosure is only a label a shell.

So in this chapter, the bugs just invent an example. Also take the electric business, Gong gift system.

Background:

1. All Gong, coupons, coupons, points inherit the same base class coupons

2. No type of coupons can mix and match

3. Points can be configured according to different scenarios different rules

4. Upgrade coupons are added on top of coupons

In general, you can design it this way.

<summary>///Kika///</summary> Public abstract class Basecard {public str
        ing decription;
           
    public abstract double ();  }///<summary>///point card a///</summary> public class Carda:basecard {public
        Carda () {decription = "Carda";
        Override double () {return 50.00;
        }///<summary>///coupon a///</summary> public class Couponsa:basecard {
        Public Couponsa () {decription = "Couponsa";
        Override double () {return 40.00;
        }///<summary>///coupon B///</summary> public class Couponsb:basecard {
        Public Couponsb () {decription = "COUPONSB";         public override double () {
            return 30.00;
        }}///<summary>///National Day voucher///</summary> public class Gqcard:basecard {
        Carda a = new Carda ();
        Couponsa CA = new Couponsa ();
        Public Gqcard () {decription = "gqcard";
        Override double () {return a.cost () + ca.cost ();
        }///<summary>///National Day upgrade a etc Gift Certificate///</summary> public class Gqacard:basecard {
        Carda a = new Carda ();
        Couponsa CA = new Couponsa ();
        COUPONSB cb = new COUPONSB ();
        Public Gqacard () {decription = "gqacard";
        public override double () {return a.cost () +ca.cost () + cb.cost (); }
    }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

The principle of the design pattern is that the class should be closed to the extension open to the modification, and that the above design will not be ideal for the large gift certificate system in the case of more and more types of voucher upgrades and the existing coupons have been updated frequently.

So, let's change our thinking.

<summary>///Kika///</summary> Public abstract class Basecard {public str
        ing decription;
           
    public abstract double (); }///<summary>///point card a///</summary> public class Carda:baseoupons {Base
        Card basec;
            Public Carda (Basecard basec) {this.basec = basec;
        Decription = "Carda," + basec.decription; 
        public override double () {return 50.00 + basec.cost ();
        }///<summary>///coupon a///</summary> public class Couponsa:baseoupons {
        Basecard basec;
            Public Couponsa (Basecard basec) {this.basec = basec;
        Decription = "Couponsa," + basec.decription;
        public override double () {return 40.00 + basec.cost (); }}///<summary>/Coupon B///</summary> public class Couponsb:baseoupons {Basecard basec;
            Public Couponsb (Basecard basec) {this.basec = basec;
        Decription = "COUPONSB," + basec.decription; 
        public override double () {return 30.00 + basec.cost ();
        }}///<summary>///base class gift certificate///</summary> public class Baseoupons:basecard {
        Basecard basec;
            Public baseoupons (Basecard basec) {this.basec = basec;
        Decription = basec.decription;
            Public baseoupons () {} public override double () {
            if (basec!= null) {return basec.cost ();
            else {return 0; }
        }
          
    }

Let's see how powerful the decorator pattern is.

Basecard a = new Baseoupons ();
            A = new Carda (a);
            A = new Couponsa (a);
            A = new COUPONSB (a);
    
            Console.WriteLine ("National Day Gift Certificate by" +a.decription+ "composition");
            Console.WriteLine (A.cost (). ToString ());
    
            Basecard B = New Carda (a);
               
            Console.WriteLine ("National Day Upgrade coupons by" + b.decription + "composition");
            Console.WriteLine (B.cost (). ToString ());
            Console.ReadLine ();

Summary: Inheritance is one of the extended forms, but it is not necessarily the best way to achieve flexible design, in our design we should allow behavior to be extended without modifying existing code. As the above example decorator pattern can also allow us to expand behavior. However, the decorator pattern also has drawbacks, which can lead to many small objects in the design, which can complicate the program if used excessively.

Author:cnblogs bugs that stay up late

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.