工厂模式和观察者模式综合应用的小例子

工厂模式:

按照以往的创造出新的物体的情况,我们通常要 Boss boss=new Boss();

而通过工厂模式设计我们把创造怪物设计为一个完整的方法Create()

Boss1,Boss2继承BossBase

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum BossType
{
    Boss1,
    Boss2
}

public class BossBase{

    public string Name;
    public int Attack;

    public static BossBase Create(BossType type)
    {
        BossBase bossbase = null;
        switch (type)
        {
            case BossType.Boss1:
                bossbase = new Boss1();
                break;
            case BossType.Boss2:
                bossbase = new Boss2();
                break;
        }
        return bossbase;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boss1 : BossBase {

    public Boss1()
    {
        Name = "大老鼠";
        Attack = 5;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boss2 : BossBase {

    public Boss2()
    {
        Name = "山贼";
        Attack = 20;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    private Boss1 boss1;
    private Boss2 boss2;

	void Start () {
        boss1 = (Boss1)BossBase.Create(BossType.Boss1);
        boss2 = (Boss2)BossBase.Create(BossType.Boss2);
    }
}

在GameManager中我们可以通过BossType.**,创建出我们需要的对象

观察者模式:

BossBase、Boss、Boss2、Player和“战场”GameManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum BossType
{
    Boss1,
    Boss2
}

public class BossBase{

    public string Name;
    public int Attack;

    public static BossBase Create(BossType type)
    {
        BossBase bossbase = null;
        switch (type)
        {
            case BossType.Boss1:
                bossbase = new Boss1();
                break;
            case BossType.Boss2:
                bossbase = new Boss2();
                break;
        }
        return bossbase;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boss1 : BossBase,IObserver {

    public Boss1()
    {
        Name = "大老鼠";
        Attack = 5;
    }

    public void PlayerHpChange(int hp)
    {
        if (hp<40)
        {
            Debug.Log("大老鼠自爆!");
        }
        if (hp<80)
        {
            Debug.Log("老鼠撕咬玩家");
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Boss2 : BossBase,IObserver {

    public Boss2()
    {
        Name = "山贼";
        Attack = 20;
    }

    public void PlayerHpChange(int hp)
    {
        if (hp < 40)
        {
            Debug.Log("山贼释放法术!");
        }
        if (hp < 80)
        {
            Debug.Log("山贼全都冲上来了!");
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IObserver
{
    void PlayerHpChange(int hp);
}

public class Player  {

    private int hp = 100;
    public List<IObserver> observers = new List<IObserver>();

    public int Hp
    {
        get
        {
            return hp;
        }

        set
        {
            hp = value;
        }
    }

    //public void SetHp(int hp)
    //{
    //    this.hp = hp;
    //    foreach (IObserver ob in observers)
    //    {
    //        ob.PlayerHpChange(this.hp);
    //    }
    //}

    public void AddHp(int hp)
    {
        this.Hp += hp;
        foreach (IObserver ob in observers)
        {
            ob.PlayerHpChange(this.Hp);
        }
    }
    //添加监听者
    public void AddObserver(IObserver ob)
    {
        observers.Add(ob);
    }
    //移除监听者
    public void RemoveObserver(IObserver ob)
    {
        observers.Remove(ob);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
    Player player;
    private Boss1 boss1;
    private Boss2 boss2;

	void Start () {
        boss1 = (Boss1)BossBase.Create(BossType.Boss1);
        boss2 = (Boss2)BossBase.Create(BossType.Boss2);
        //BossBase boss1 = BossBase.Create(BossType.Boss1);
        //BossBase boss2 = BossBase.Create(BossType.Boss2);
        player = new Player();
        player.AddObserver(boss1);
        player.AddObserver(boss2);
        //player.AddObserver((IObserver)boss1);
        //player.AddObserver((IObserver)boss2);
        Debug.Log(player.Hp);
    }
	
	void Update () {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (player.Hp>0)
            {
                Debug.Log("玩家掉了10滴血");
                player.AddHp(-10);
            }
            else
            {
                Debug.Log("玩家死亡");
                player.RemoveObserver(boss1);
                player.RemoveObserver(boss2);
            }
        }
    }
}

打印出来的结果:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值