⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 adpter 、Singlen、Template设计模式的实现
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace Template
{
   public abstract class AbstractDisplay { // 抽象类AbstractDisplay
        public abstract void open();        // 由子类实现的抽象方法(1)open
        public abstract void print();       // 由子类实现的抽象方法(2)print
        public abstract void close();       // 由子类实现的抽象方法(3)close
        public   void display()
        {       // 在此抽象类中实现的方法display
            open();                         // 先open…
            for (int i = 0; i < 5; i++) {   // 反复5次print…
              print();
            }
            close();         // …最后close起來。这就是实现display方法时的內容
        }
    };
    public class CharDisplay :AbstractDisplay {  // CharDisplay是AbstractDisplay
                                             // 的子类。
        private
            char ch;		// 应该输出的字符。
        
        public CharDisplay(char ch) {	// 把以构造子传递过来的字符ch
            this.ch = ch;	// 储存在属性内。
        }
        public override void open()
        {	// 本来在父类是抽象方法。
	    // 这里过载(override)后实现。
           Console.Out.Write("<<");	// 输出"<<"做为开始字串。
        }
        public override void print()
        {	// print方法也是在此实现。它是从display
				    // 被重复调用出来。
          Console.Out.Write(ch);	// 输出1个储存在属性内的字符。
        }
        public override void close()
        {	// close方法也是在此实现。
            Console.Out.WriteLine(">>");	// 输出结束字串">>"。
        }
    };

    public class StringDisplay :AbstractDisplay {  // StringDisplay也是
                                                        // AbstractDisplay的子类。
        private String str;                   // 应输出的字串。
        private int width;                        // 以byte为单位所求出的字串「宽度」。

        public  StringDisplay(String str)
        {     // 把以构造子传递过来的字串string
            this.str = str;                 // 储存在属性內。
            this.width = str.Length;// 接着把以byte为单位的字元宽幅
                                                  // 储存在属性,后面会使用到。
        }
        public override void open() {                      // 先复写后再定义的open方法。
            printLine();                          // 以此类的方法printLine
                                                  // 画出线段。
        }
        public override void print()
        {                        // print方法则在
          Console.Out.WriteLine("|" + this.str + "|");  // 属性所储存的字串前后
                                                     //加上"|",然后输出到画面。
        }
        public override void close()
        {		// close方法则同
           Console.Out.WriteLine();			// open,利用printLine方法画出线段。
        }
        private void printLine() {		// 这是open和close所调用的printLine方法。
					    // 因为这是private,所以只能在此类內部使用。
            Console.Out.Write("+");		// 输出"+"记号表示边框位置。
            for (int i = 0; i < width; i++) {	// 输出width个"-",
              Console.Out.Write("-");		// 当作线段。
            }
            Console.Out.WriteLine("+");		//输出"+"记号表示边框位置。
        }
    };



    class Program
    {
       public static void Main(string[] args)
        {
            // 建立1个有'H'的CharDisplay的对象实例。
        AbstractDisplay d1 = new CharDisplay('H');
	// 建立1个有"Hello, world."的StringDisplay的对象实例。
        AbstractDisplay d2 = new StringDisplay("Hello, world.");
	// 建立1个有"你好。"的StringDisplay的对象实例。
        AbstractDisplay d3 = new StringDisplay("你好。");
        d1.display();	// d1,d2,d3都是AbstractDisplay的子类的对象实例,
			// 因此,
        d2.display();	// 可以调用继承到的display方法。
        d3.display();	// 实际动作则是规定在类CharDisplay、StringDisplay。
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -