zoukankan      html  css  js  c++  java
  • 1.策略模式

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
     
    namespace DesignPattern._1.StrategyPattern
    {
        public abstract class Duck
        {
            public IFlyBehavior flyBehavior = null;
            public IQuackBehavior quackBehavior = null;
     
            public Duck(){}
     
            public abstract void display();
     
            public void performFly()
            {
                flyBehavior.fly();
            }
     
            public void performQuack()
            {
                quackBehavior.quack();
            }
     
            public void swim()
            {
                Console.WriteLine("All ducks float, even decoys");
            }
     
            public void setFlyBehavior(IFlyBehavior fb)
            {
                flyBehavior = fb;
            }
     
            public void SetQuackBehavior(IQuackBehavior qb)
            {
                quackBehavior = qb;
            }
        }
     
        public class MallardDuck : Duck
        {
            public MallardDuck()
            {
                flyBehavior = new FlyWithWings();
                quackBehavior = new Quack();
            }
     
            public override void display()
            {
                Console.WriteLine("I'm a real Mallard Duck");
            }
     
        }
     
     
        public interface IFlyBehavior
        {
             void fly();
        }
     
        public class FlyWithWings : IFlyBehavior
        {
            public void fly()
            {
                Console.WriteLine("I'm flying");
            }
        }
     
        public class FlyNoWay : IFlyBehavior
        {
            public void fly()
            {
                Console.WriteLine("I can't fly");
            }
        }
     
     
        public interface IQuackBehavior
        {
            void quack();
        }
     
        public class Quack: IQuackBehavior
        {
            public void quack()
            {
                Console.WriteLine("Quack");
            }
        }
     
        public class MuteQuack:IQuackBehavior
        {
            public void quack()
            {
                Console.WriteLine("<<Silence>>");
            }
        }
     
        public class Sqleak:IQuackBehavior
        {
            public void quack()
            {
                Console.WriteLine("Squeak");
            }
        }
     
        public class ModelDuck : Duck
        {
            public ModelDuck()
            {
                flyBehavior = new FlyNoWay();
                quackBehavior = new Quack();
            }
     
            public override void display()
            {
                Console.WriteLine("I'm a model duck");
            }
        }
     
        public class FlyRockekPowered:IFlyBehavior
        {
            public void fly()
            {
                Console.WriteLine("I'm flying with a rocket!");
            }
        }
     
        public class StragegyPatternTestClass
        {
            public StragegyPatternTestClass()
            {
                Duck mallard = new MallardDuck();
                mallard.performQuack();
                mallard.performFly();
     
                Duck model = new ModelDuck();
                model.performFly();
     
                model.setFlyBehavior(new FlyRockekPowered());
                model.performFly();
            }
     
        }
    }
    


    namespace DesignPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                //1. Stragegy Pattern
                new StragegyPatternTestClass();//StragegyPattern
     
     
     
                System.Console.Read();
     
    			
                
            }
        }
    }
  • 相关阅读:
    css水平垂直居中
    JavaScript提供了哪几种“异步模式”?
    sort()
    后端开发中,可以在Cache-Control设置的常用指令
    纯函数
    react和vue
    npm(classnames) 更灵活使用类名
    shell知多少?
    常见的HTTP状态码
    axios
  • 原文地址:https://www.cnblogs.com/vihone/p/3220389.html
Copyright © 2011-2022 走看看