星空网 > 软件开发 > ASP.net

装饰者模式(C#)实现

  我喜欢户外,然后常常去逛户外装备的店子,一般买了自行车等大件,再买登山包,登山鞋等配件就会有优惠。比如买了辆Giant的车,然后买个bag就有优惠,本案例将配件作为装饰者,简单的实现装饰者模式,对于继承,多态掌握的还是很迷惑。有错误还请各位看官帮忙指出,仪器共同进步!

首先定义一个超类Buybike

1
2
3
4
5
6
7
8
9
public abstract class Buybike
   {
       protected string description="To Get a Bike";
       public virtual string getDescription()
       {
           return description;
       }
       public abstract string Cost();
   }



超类下面定义一个装饰者超类OtherDecorator(所有装饰者【案例中配件】的父类)

1
  



1
2
3
4
public abstract class OtherDecorator:Buybike
    {
        //只是为了将装饰者全部置于其下,无具体实现方法
    }



1
下面是两个被装饰的类Giant Merida(捷安特,美利达)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Merida:Buybike
    {
        public Merida()
        {
            description = "Merida";
        }
        public override string Cost()
        {
            return "2000RMB";
        }
    }
public class Giant : Buybike
    {
        public Giant()
        {
            description = "Giant";
        }
        public override string Cost()
        {
            return "3000RMB";
        }
 
    }



1
接下去是所有装饰者(配件),全部继承自OtherDecorator



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class AddShirt:OtherDecorator
    {
        private  Buybike buyBike;
        public AddShirt(Buybike buyBike)
        {
            this.buyBike = buyBike;
        }
        public override string getDescription()
        {
            return buyBike.getDescription() + "Add a shirt";
        }
 
        public override string Cost()
        {
            return buyBike.Cost() + "shirt's money:600RMB";
        }
    }
class AddShoes:OtherDecorator
    {
        private  Buybike buyBike;
        public AddShoes(Buybike buyBike)
        {
            this.buyBike = buyBike;
        }
         
        public override string getDescription()
        {
            return buyBike.getDescription() + "Add a shoes";
        }
 
        public override string Cost()
        {
            return buyBike.Cost() + "shoe's money:300RMB";
        }
    }
class AddBag:OtherDecorator
    {
        private  Buybike buyBike;
        public AddBag(Buybike buyBike)
        {
            this.buyBike = buyBike;
        }
        public override string getDescription()
        {
            return buyBike.getDescription() + "Add a bag";
        }
 
        public override string Cost()
        {
            return buyBike.Cost() + "bag's money:200RMB";
        }
    }  



程序运行如下:

1
2
3
4
5
6
7
8
9
10
11
class Program
{
    static void Main(string[] args)
    {
        Buybike a= new Giant();
        a = new AddBag(a);
        a = new AddShirt(a);
        Console.WriteLine(a.Cost());
        Console.WriteLine(a.getDescription());
    }
}



装饰者模式(C#)实现

此模式好处:装饰者和被装饰者从相同基类派生,类型之间的转化非常方便,适合多态的实现。

具体到例子,此处的a具体到是一个基类变量,把它指到下面的子类不必进行类型转化,而且a具体是什么类型,得在运行时才能确定,程序耦合度大大降低。




原标题:装饰者模式(C#)实现

关键词:C#

C#
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

印度电商开店费用:https://www.goluckyvip.com/tag/47815.html
印度电商开店流程:https://www.goluckyvip.com/tag/47816.html
印度电商入驻:https://www.goluckyvip.com/tag/47817.html
印度电商资讯:https://www.goluckyvip.com/tag/47818.html
土耳其站点:https://www.goluckyvip.com/tag/4782.html
印度广告报告下载:https://www.goluckyvip.com/tag/47820.html
NRA账户的开户主体包括:香港、美国、新加坡、欧盟等国家的详细解析 :https://www.xlkjsw.com/news/94338.html
湘西游轮六 湘江游轮夜游:https://www.vstour.cn/a/411226.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流