CSharp: Bridge Pattern in donet 6

 

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public abstract class Document
    {
        /// <summary>
        ///
        /// </summary>
        protected readonly IFormatter _formatter;
        /// <summary>
        ///
        /// </summary>
        /// <param name="formatter"></param>
        protected Document(IFormatter formatter)
        {
            _formatter = formatter;
        }
        /// <summary>
        ///
        /// </summary>
        public abstract void Print();
    }
 
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public interface IFormatter
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        string Format(string key, string value);
    }
 
 /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public class TermPaper : Document
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="formatter"></param>
        public TermPaper(IFormatter formatter)
            : base(formatter)
        {
        }
        /// <summary>
        ///
        /// </summary>
        public string Class { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public string Student { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public string Text { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public string References { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public override void Print()
        {
            Console.WriteLine(_formatter.Format("班级", Class));
            Console.WriteLine(_formatter.Format("学生", Student));
            Console.WriteLine(_formatter.Format("文本", Text));
            Console.WriteLine(_formatter.Format("目录", References));
            Console.WriteLine();
        }
    }
 
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public class FAQ : Document
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="formatter"></param>
        public FAQ(IFormatter formatters)
            : base(formatters)
        {
            Questions = new Dictionary<string, string>();
        }
 
        public string Title { get; set; } = string.Empty;
        public Dictionary<string, string> Questions { get; set; }
 
        public override void Print()
        {
            Console.WriteLine(_formatter.Format("标题", Title));
 
            foreach (var question in Questions)
            {
                Console.WriteLine(_formatter.Format("   问题", question.Key));
                Console.WriteLine(_formatter.Format("   回答", question.Value));
            }
 
            Console.WriteLine();
        }
    }
 
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public class Book : Document
    {
 
        /// <summary>
        ///
        /// </summary>
        /// <param name="formatter"></param>
        public Book(IFormatter formatters)
            : base(formatters)
        {
        }
        /// <summary>
        ///
        /// </summary>
        public string Title { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public string Author { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public string Text { get; set; } = string.Empty;
        /// <summary>
        ///
        /// </summary>
        public override void Print()
        {
            Console.WriteLine(_formatter.Format("标题", Title));
            Console.WriteLine(_formatter.Format("作者", Author));
            Console.WriteLine(_formatter.Format("文本", Text));
            Console.WriteLine();
        }
 
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public class BackwardsFormatter : IFormatter
    {
        public string Format(string key, string value) =>
            $"{key}: {new string(value.Reverse().ToArray())}";
    }
 
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public class FancyFormatter : IFormatter
    {
        public string Format(string key, string value) =>
            $"-= {key} ----- =- {value}";
    }
 
    /// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public class StandardFormatter : IFormatter
    {
        public string Format(string key, string value) =>
            $"{key}: {value}";
    }
 
/// <summary>
    /// 桥接模式 Bridge Pattern- Structural Pattern
    /// </summary>
    public static class DocumentsAndFormattersExecutor
    {
 
        /// <summary>
        ///
        /// </summary>
        public static void Execute()
        {
            ConsoleExtension.WriteSeparator("桥接模式 Bridge Pattern- Documents and formatters demo");
 
            var documents = new List<Document>();
            var formatter = new FancyFormatter();
 
            var faq = new FAQ(formatter);
            faq.Title = "桥接模式常见问题解答;";
            faq.Questions.Add("这是什么模式?", "桥接模式.");
            faq.Questions.Add("什么时候用此模式?", "当您需要将抽象与实现分离时.");
 
            var book = new Geovin.Du.DuBridge.DocumentsAndFormatters.Documents.Book(formatter)
            {
                Title = "C# 设计模式",
                Author = "佚名",
                Text = "内容是...",
            };
 
            var paper = new Geovin.Du.DuBridge.DocumentsAndFormatters.Documents.TermPaper(formatter)
            {
                Class = "桥接模式 Bridge Pattern- Design Patterns",
                Student = "geovindu",
                Text = "关于设计模式的文本...",
                References = "GOF",
            };
 
            documents.Add(faq);
            documents.Add(book);
            documents.Add(paper);
 
            foreach (var document in documents)
            {
                document.Print();
            }
        }
    }

  

调用:

 

 

1
Geovin.Du.DuBridge.DocumentsAndFormatters.DocumentsAndFormattersExecutor.Execute();

 输出:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
桥接模式 Bridge Pattern- Documents and formatters demo
--------------------------------------------------
-= 标题 ----- =- 桥接模式常见问题解答;
-=    问题 ----- =- 这是什么模式?
-=    回答 ----- =- 桥接模式.
-=    问题 ----- =- 什么时候用此模式?
-=    回答 ----- =- 当您需要将抽象与实现分离时.
 
-= 标题 ----- =- C# 设计模式
-= 作者 ----- =- 佚名
-= 文本 ----- =- 内容是...
 
-= 班级 ----- =- 桥接模式 Bridge Pattern- Design Patterns
-= 学生 ----- =- geovindu
-= 文本 ----- =- 关于设计模式的文本...
-= 目录 ----- =- GOF

  

 

 

 

posted @   ®Geovin Du Dream Park™  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2010-11-13 css 自定义字体 Internet Explorer,Firefox,Opera,Safari
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示