zoukankan      html  css  js  c++  java
  • 类型对象模式

    个人理解:将类中 统一的,变化大的 部分抽象出来 特殊处理这样当需要大量派生子类处理差异的时候就避免了这个操作

    举个例子:游戏中的小怪 有的攻击可以产生一些Debuff,有的会产生粒子特效,有的只产生伤害  针对不同类型的小怪 需要派生大量的子类 为避免这种情况出现下面这种解决方案

    #region 继承方式的实现
    public abstract class Monster
        {
            public Monster(int startingHealth)
            {
                Health = startingHealth;
            }
      
            public int Health { get; private set; }
            public abstract string AttackString { get; }
        }
     
       public class Dragon : Monster
       {
            public Dragon(): base(500){}
     
            public override string AttackString
            {
            get { return "The dragon breathes fire!"; }
            }
       }
     
       public class Troll : Monster
       {
         public Troll():base(300){}
     
         public override string AttackString
         {
            get { return "The troll clubs you!"; }
         }
       }
     
       public class Client
       {
         public void TestCase2()
         {
           Monster dragon = new Dragon();
           Monster troll = new Troll();
         }
       }
    
    #endregion
    
    
    //类型对象模式的实现
    namespace YC_TypeObjectPatter_Sample {
        public class Player {
            public float x, y, z;
            public float health;
            public string buffStr;
        }
    
        public class Breed {
            public float health;
            public string attackStr;
            public delegate void AttackEffect(Player player);
            public AttackEffect attackEffect;
    
        }
        public class Monster {
            private Breed _breed;
            public Monster(Breed brd) {
                _breed = brd;
            }
            public float health {
                get {
                    return _breed.health;
                }
            }
            public void Attack(Player player) {
                if (_breed.attackEffect != null)
                    _breed.attackEffect(player);
            }
    
            public void DeathEffect() {
    
            }
        }
    
        public class Sample{
            public void StartGame() {
    
                #region  将这个怪物的特性管理起来
                Breed monster_1 = new Breed
                {
                    health = 100,
                    attackStr = "普通怪物攻击方式",
                    attackEffect = _ =>
                    {
                        _.health -= 10;
                    }
                };
    
                Breed monster_2 = new Breed
                {
                    health = 100,
                    attackStr = "精英怪物攻击方式",
                    attackEffect = _ =>
                    {
                        _.health -= 30;
                        _.buffStr = "debuff_speedCut";
                    }
                };
                Breed monster_3 = new Breed
                {
                    health = 100,
                    attackStr = "Boss怪物攻击方式",
                    attackEffect = _ =>
                    {
                        _.health -= 50;
                        //产生粒子之类的
                    }
                };
               
                #endregion
                Monster commonMst = new Monster(monster_1);
                Monster eliteMst = new Monster(monster_2);
                Monster BossMst = new Monster(monster_3);
    
            }
        }
    }
  • 相关阅读:
    HTML5 图片预览
    ubuntu JAVA 安装
    跟着老猫来搞GO内建容器Map
    面试造火箭系列,栽在了cglib和jdk动态代理
    跟着老猫来搞GO,"面向对象"
    跟着老猫来搞GO内建容器slice
    跟着老猫来搞GO,基础进阶
    JavaScript中利用doucument.writeline()方法预生成代码的小技巧
    javascript学习记录
    你的逻辑性强吗?
  • 原文地址:https://www.cnblogs.com/chenggg/p/11406094.html
Copyright © 2011-2022 走看看