zoukankan      html  css  js  c++  java
  • 用C#改写Head First Design PatternsStrategy策略模式(原创)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Strategy
    {
        /// <summary>
        /// 具有飞行行为
        /// </summary>
        public interface FlyBehavior
        {
            void fly();
        }

        /// <summary>
        /// 具有吃饭行为
        /// </summary>
        public interface EatBehavior
        {
            void eat();
        }

        /// <summary>
        /// 用翅膀飞
        /// </summary>
        public class FlyWithWings: FlyBehavior
        {


            #region FlyBehavior 成员

            void FlyBehavior.fly()
            {
                System.Console.WriteLine("用翅膀飞");
            }

            #endregion
        }

        /// <summary>
        /// 用翅膀飞
        /// </summary>
        public class FlyNoWay : FlyBehavior
        {


            #region FlyBehavior 成员

            void FlyBehavior.fly()
            {
                System.Console.WriteLine("飞不了");
            }

            #endregion
        }


        public class EatMeat : EatBehavior
        {

            #region EatBehavior 成员

            void EatBehavior.eat()
            {
                System.Console.WriteLine("吃肉");
            }

            #endregion
        }

        public class EatAll : EatBehavior
        {

            #region EatBehavior 成员

            void EatBehavior.eat()
            {
                System.Console.WriteLine("什么都吃,杂食");
            }

            #endregion
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Strategy
    {
        class AnimalClass
        {
            public abstract class Animal
            {
                protected FlyBehavior fb;
                protected EatBehavior eb;
                public  string name;

                public Animal() { }

                public void Fly()
                {
                    fb.fly();
                }

                public void Eat()
                {
                    eb.eat();
                }
           
            }

            public class Tiger : Animal
            {
                public Tiger()
                {
                    fb=  new FlyNoWay();
                    eb = new EatMeat();
                    name = "老虎";
                }
            }

            public class Eagle : Animal
            {

                public Eagle()
                {
                    fb = new FlyWithWings();
                    eb = new EatMeat();
                    name = "老鹰";
                }
            }

            public class Human : Animal
            {
                public Human()
                {
                    fb = new FlyNoWay();
                    eb = new EatAll();
                    name = "人类";
                }
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Strategy
    {
        class Program
        {
            static void Main(string[] args)
            {
                Strategy.AnimalClass.Tiger t = new AnimalClass.Tiger();
                System.Console.WriteLine(t.name);
                t.Eat();
                t.Fly();
                Strategy.AnimalClass.Eagle e = new AnimalClass.Eagle();
                System.Console.WriteLine(e.name);
                e.Eat();
                e.Fly();
                Strategy.AnimalClass.Human h = new AnimalClass.Human();
                System.Console.WriteLine(h.name);
                h.Eat();
                h.Fly();
                System.Console.ReadLine();
            }
        }
    }

    关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
    目前维护的开源产品:https://gitee.com/475660
  • 相关阅读:
    jQuery插件:用于获取元素自身的HTML内容
    自定义 Web 部件用户界面简介
    在MOSS2010中实现OU下的用户的上下级组织关系
    sharepoint2010人性化的地方--员工离职AD账号禁用(个人网站自动提醒上级经理功能)
    SharePoint2010文档归档策略(2)-从放置库转移到自己定义的文档库
    SharePoint2010文档归档策略
    如何用VS2010在SharePoint中创建自定义字段类型(以eWebEditor为例)
    如何实现SP文档库类似百度文档库的效果 (副标题:如何在SP2013文档库的SWF文件用FlexPager显示)
    查看SharePoint文档库是,显示层次目录,可以点击返回层次
    安装和配置SharePoint 2013 with SP1 Workflow
  • 原文地址:https://www.cnblogs.com/starcrm/p/1519232.html
Copyright © 2011-2022 走看看