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

    装饰着模式比较简单也比较实用当要扩展的时候只要传入要包装的对象就好了,比起扩展方法需要一个静态类和一个静态方法比较好用点也比较直观明白

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Reflection;
    namespace ConsoleApplication2
    {
        public interface Tank
        {
            void shot();
            void run();
        
        }
        public class tank2A : Tank
        {

            public void shot()
            {
                Console.WriteLine("tank20 shot");
              
            }

            public void run()
            {
                Console.WriteLine("tank20 run");
            }
        }
        public class tank3A : Tank
        {

            public void shot()
            {
                Console.WriteLine("tank30 shot");
            }

            public void run()
            {
                Console.WriteLine("tank30 run");
            }
        }
        public abstract class Decorata:Tank
        {
            Tank tank;
            public Decorata(Tank tank)
            {  
                this.tank = tank;
            
            }
            public void shot()
            {
                tank.shot();
                Console.WriteLine("shotA");

            }

            public void run()
            {
                tank.run();
                Console.WriteLine("runA");
            }



           
        }
        public  class DecorataA : Decorata
        {
            Tank tank;
           
            public DecorataA(Tank tank):base(tank)
            {

                this.tank = tank;
            }
            public void shot()
            {
                tank.shot();
                Console.WriteLine("shotA");

            }

            public void run()
            {
                tank.run();
                Console.WriteLine("runA");
            }



          
        }

        public class DecorataB : DecorataA
        {
            Tank tank;

            public DecorataB(Tank tank)
                : base(tank)
            {

                this.tank = tank;
            }
            public void shot()
            {
                tank.shot();
                Console.WriteLine("shotB");

            }

            public void run()
            {
                tank.run();
                Console.WriteLine("runB");
            }




        }
      public  class Program
        {
          
            static void Main(string[] args)
            {

                Tank tank = new tank2A();
                DecorataA da = new DecorataA(tank);
                DecorataB db = new DecorataB(da);
                db.shot();
                db.run();
                Console.ReadKey();
            }
        }
    }

  • 相关阅读:
    SQL SERVER 2005添加用户和编辑sa
    数组型参数和数组的区别
    Oracle数据库建库、建表空间,建用户
    oracle表空间操作详解
    Oracle10g的完全卸载(转载)
    Delphi format的用法
    AnImateWindow用法
    文本文件操作
    TStringList的用法
    Delphi网络函数
  • 原文地址:https://www.cnblogs.com/kexb/p/3670928.html
Copyright © 2011-2022 走看看