• CSharp: Bridge Pattern in donet 6


        /// <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();
                }
            }
        }
    

      

    调用:

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

     输出:

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

      

  • 相关阅读:
    HTML5 中的Nav元素详解
    Gevent中信号量的使用
    MemCache缓存multiget hole详解
    MemCache中的内存管理详解
    Php中的强制转换详解
    Python中类的特殊方法详解
    MemCache的LRU删除机制详解
    AngularJS事件绑定的使用详解
    Php数据类型之整型详解
    HTML基础知识
  • 原文地址:https://www.cnblogs.com/geovindu/p/16886909.html
Copyright © 2020-2023  润新知