zoukankan      html  css  js  c++  java
  • 装饰模式

      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         }

  • 相关阅读:
    Docker用途 & 和tomcat的区别
    Ubuntu安装Redis
    Ubuntu查看和设置Root账户
    Oracle常用语句
    Redis知识总结
    Blazor学习笔记01: 使用BootstrapBlazor组件 创建一个具有单表维护功能的表格页面
    NET Core之积沙成塔01: 解决Visual Studio 2019 代码提示为英文
    MySQL系统自带的数据库information schema
    Windows安装mysql方法
    数据库之概念
  • 原文地址:https://www.cnblogs.com/CHUR/p/5069662.html
Copyright © 2011-2022 走看看