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

工厂模式:

按照以往的创造出新的物体的情况,我们通常要 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);
            }
        }
    }
}

打印出来的结果:

猜你喜欢

转载自blog.csdn.net/qq_41579634/article/details/86699562
今日推荐