1 /*
2 * 一个咖啡店可以提供大杯(JorumCoffee)、中杯(MediumCoffee)、小杯(SmallCoffee)的咖
3 * 啡(Coffee),为了满足不同用户的口味,在咖啡中可以添加牛奶(Milk),或者糖(Sugar),
4 * 或者柠檬(Lemon),提供给用户不同口味的组合,如大杯咖啡加牛奶,中杯咖啡加糖,小
5 * 杯咖啡加柠檬,小杯咖啡加糖等。应用桥接模式,用C#控制台应用程序实现该设计。
6 */
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11
12 namespace Bridge
13 {
14 abstract class Taste
15 {
16 public abstract string Add();
17 }
18 class Milk : Taste
19 {
20 public override string Add()
21 {
22 return "牛奶";
23 }
24 }
25 class Suger : Taste
26 {
27 public override string Add()
28 {
29 return "糖";
30 }
31 }
32 class Lemon : Taste
33 {
34 public override string Add()
35 {
36 return "柠檬";
37 }
38 }
39
40 class Coffee
41 {
42 protected Taste taste;
43 public void SetTaste(Taste taste)
44 {
45 this.taste =
taste;
46 }
47 public virtual void Add()
48 {
49 Console.WriteLine(
"向咖啡中添加了" + taste.Add() +
"。");
50 }
51 }
52 class JorumCoffee : Coffee
53 {
54 public JorumCoffee()
55 {
56 Console.WriteLine(
"买了一大杯咖啡。");
57 }
58 public override void Add()
59 {
60 Console.WriteLine(
"向大杯咖啡中添加了" + taste.Add() +
"。");
61 }
62 }
63 class MediumCoffee : Coffee
64 {
65 public MediumCoffee()
66 {
67 Console.WriteLine(
"买了一中杯咖啡。");
68 }
69 public override void Add()
70 {
71 Console.WriteLine(
"向中杯咖啡中添加了" + taste.Add() +
"。");
72 }
73 }
74 class SmallCoffee : Coffee
75 {
76 public SmallCoffee()
77 {
78 Console.WriteLine(
"买了一小杯咖啡。");
79 }
80 public override void Add()
81 {
82 Console.WriteLine(
"向小杯咖啡中添加了" + taste.Add() +
"。");
83 }
84 }
85
86 class Program
87 {
88 static void Main(
string[] args)
89 {
90 Milk milk =
new Milk();
91 Suger suger =
new Suger();
92 Lemon lemon =
new Lemon();
93
94 Coffee jc =
new JorumCoffee();
95 jc.SetTaste(milk);
96 jc.Add();
97
98 Coffee mc =
new MediumCoffee();
99 mc.SetTaste(suger);
100 mc.Add();
101
102 Coffee sc =
new SmallCoffee();
103 sc.SetTaste(lemon);
104 sc.Add();
105 }
106 }
107 }
转载于:https://www.cnblogs.com/CheeseZH/archive/2012/05/16/2505569.html
相关资源:数据结构—成绩单生成器
转载请注明原文地址: https://win8.8miu.com/read-1494751.html