• 装饰模式


      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace Decorate
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             Mobilephone apple = new Apple();
     14             char i; bool s = true;
     15             Console.WriteLine("--------------------------------------");
     16             Console.WriteLine(" ALL function:");
     17             Console.WriteLine(" Bluetooth----1.");
     18             Console.WriteLine(" GPS----2.");
     19             Console.WriteLine(" Camera----3.");
     20             Console.WriteLine(" End----0.");
     21             Console.WriteLine("Please input the choice:");
     22             while (s)
     23             {
     24                
     25                 i = (char)Console.Read();
     26                 if (i == '0') { s=false; }
     27                 if (i == '1')
     28                 {
     29                     Bluetooth bluetooth = new Bluetooth();
     30                     bluetooth.Decorate(apple);
     31                     bluetooth.SendMessage();
     32                     bluetooth.Call();
     33                 }
     34                 if (i == '2')
     35                 {
     36                     GPS gps = new GPS();
     37                     gps.Decorate(apple);
     38                     gps.SendMessage();
     39                     gps.Call();
     40                 }
     41                 if (i == '3')
     42                 {
     43                     Camera camera = new Camera();
     44                     camera.Decorate(apple);
     45                     camera.SendMessage();
     46                     camera.Call();
     47                 }
     48             }
     49         }
     50     }
     51 }
     52 
     53         abstract class Mobilephone
     54         {
     55             public Mobilephone() { }
     56             public abstract void SendMessage();
     57             public abstract void Call();
     58         }
     59         class Apple:Mobilephone
     60         {
     61             public Apple():base()
     62             { }
     63             public override void SendMessage()
     64             {
     65                 Console.WriteLine("Apple Sending Message");
     66             }
     67 
     68             public override void Call()
     69             {
     70                 Console.WriteLine("Apple Calling");
     71             }
     72         }
     73 
     74         class Mi : Mobilephone
     75         {
     76             public Mi()
     77                 : base()
     78             { }
     79             public override void SendMessage()
     80             {
     81                 Console.WriteLine("Mi Sending Message");
     82             }
     83 
     84             public override void Call()
     85             {
     86                 Console.WriteLine("Mi Calling");
     87             }
     88         }
     89 
     90         abstract class Function:Mobilephone
     91         {
     92             private Mobilephone _mobilephone;
     93             public Function() : base() { }
     94             public void Decorate(Mobilephone mobilephone)
     95             {
     96                 _mobilephone = mobilephone;
     97             }
     98             public override void SendMessage()
     99             {
    100                 _mobilephone.SendMessage();
    101             }
    102 
    103             public override void Call()
    104             {
    105                 _mobilephone.Call();
    106             }
    107         }
    108         class Bluetooth:Function
    109         {
    110             public Bluetooth() : base() { }
    111             public override void SendMessage()
    112             {
    113                 base.SendMessage();
    114                 AddBluetooth();
    115                 Connect();
    116             }
    117             public override void Call()
    118             {
    119                 base.Call();
    120                 AddBluetooth();
    121                 Connect();
    122             }
    123             public void AddBluetooth()
    124             {
    125                 Console.WriteLine("Bluetooth");
    126             }
    127 
    128             public void Connect()
    129             {
    130                 Console.WriteLine("Connecting");
    131             }
    132         }
    133         class GPS : Function
    134         {
    135             public GPS() : base() { }
    136             public override void SendMessage()
    137             {
    138                 base.SendMessage();
    139                 AddGPS();
    140                 location();
    141             }
    142             public override void Call()
    143             {
    144                 base.Call();
    145                 AddGPS();
    146                 location();
    147             }
    148             public void AddGPS()
    149             {
    150                 Console.WriteLine("GPS");
    151             }
    152 
    153             public void location()
    154             {
    155                 Console.WriteLine("Locating");
    156             }
    157         }
    158         class Camera : Function
    159         {
    160             public Camera() : base() { }
    161             public override void SendMessage()
    162             {
    163                 base.SendMessage();
    164                 AddCamera();
    165                 Came();
    166             }
    167             public override void Call()
    168             {
    169                 base.Call();
    170                 AddCamera();
    171                 Came();
    172             }
    173             public void AddCamera()
    174             {
    175                 Console.WriteLine("Camera");
    176             }
    177 
    178             public void Came()
    179             {
    180                 Console.WriteLine("Camera is on");
    181             }
    182         }

  • 相关阅读:
    教你透彻了解红黑树(转)
    算法描述伪代码
    B 树、B+ 树、B* 树
    Java四种引用类型
    Java程序以后台方式在Linux上运行(nohup命令)
    java 文档注释
    Reactor模式和NIO
    Immutable模式与string类的实现
    mina 入门
    索引和优化查询
  • 原文地址:https://www.cnblogs.com/CHUR/p/5069662.html
Copyright © 2020-2023  润新知