博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用C#改写Head First Design Patterns--Strategy策略模式(原创)
阅读量:4135 次
发布时间:2019-05-25

本文共 2045 字,大约阅读时间需要 6 分钟。

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();
        }
    }
}

 

转载地址:http://fvpvi.baihongyu.com/

你可能感兴趣的文章
Android使用webservice客户端实例
查看>>
[转]C语言printf
查看>>
C 语言 学习---获取文本框内容及字符串拼接
查看>>
C 语言学习 --设置文本框内容及进制转换
查看>>
C 语言 学习---判断文本框取得的数是否是整数
查看>>
C 语言 学习---ComboBox相关、简单计算器
查看>>
C 语言 学习---ComboBox相关、简易“假”管理系统
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
C 语言 学习---复选框及列表框的使用
查看>>
第十一章 - 直接内存
查看>>
JDBC核心技术 - 上篇
查看>>
一篇搞懂Java反射机制
查看>>
application/x-www-form-urlencoded、multipart/form-data、text/plain
查看>>
Longest Common Prefix -最长公共前缀
查看>>
Letter Combinations of a Phone Number
查看>>
Single Number II --出现一次的数(重)
查看>>
Valid Parentheses --括号匹配
查看>>
Count and Say
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>