Unity---游戏设计模式(10)之建造者模式
概述参考请看 参考博客
建造者模式:是将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式的流程不变,但每个流程实现的具体细节是经常变化的。
1、建造者模式原型
UML图
原型代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 建造者模式
/// </summary>
public class Client : MonoBehaviour
{
private void Start()
{
IBulider fatPersonBuilder = new FatPersonBuilder();
IBulider ThinPersonBuilder = new ThinPersonBuilder();
IPerson fatPerson = Director.GetPerson(fatPersonBuilder);
fatPerson.ShowAllPart();
}
}
/// <summary>
/// 需要建造的人物
/// </summary>
public class IPerson
{
private List<string> mParts = new List<string>();
/// <summary>
/// 构造人物身体部分
/// </summary>
public void AddPart(string part)
{
mParts.Add(part);
}
public void ShowAllPart()
{
foreach (string item in mParts)
{
Debug.Log(item);
}
}
}
public class FatPerson : IPerson
{
}
public class ThinPerson : IPerson
{
}
/// <summary>
/// 建造者接口
/// </summary>
public interface IBulider
{
void AddHead();
void AddBody();
void AddHand();
void AddFeet();
IPerson GetResult();
}
/// <summary>
/// 建造胖人
/// </summary>
public class FatPersonBuilder : IBulider
{
private IPerson mFatPerson;
public FatPersonBuilder()
{
mFatPerson = new FatPerson();
}
public void AddBody()
{
mFatPerson.AddPart("胖子身体");
}
public void AddFeet()
{
mFatPerson.AddPart("胖子脚");
}
public void AddHand()
{
mFatPerson.AddPart("胖子手");
}
public void AddHead()
{
mFatPerson.AddPart("胖子头部");
}
public IPerson GetResult()
{
return mFatPerson;
}
}
/// <summary>
/// 建造瘦人
/// </summary>
public class ThinPersonBuilder : IBulider
{
private IPerson mThinPerson;
public ThinPersonBuilder()
{
mThinPerson = new ThinPerson();
}
public void AddBody()
{
mThinPerson.AddPart("瘦子身体");
}
public void AddFeet()
{
mThinPerson.AddPart("瘦子脚");
}
public void AddHand()
{
mThinPerson.AddPart("瘦子手");
}
public void AddHead()
{
mThinPerson.AddPart("瘦子头部");
}
public IPerson GetResult()
{
return mThinPerson;
}
}
public class Director
{
public static IPerson GetPerson(IBulider bulider)
{
bulider.AddHead();
bulider.AddBody();
bulider.AddHand();
bulider.AddFeet();
return bulider.GetResult();
}
}
2、建造者模式实例
在游戏中的时候,同样的也可以用在建造人物角色上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 建造者模式
/// </summary>
public class TestMode : MonoBehaviour
{
private void Start()
{
ICharacter soldier = Direct.GetCharacter(new SoldierBuilder(new SoldierCharacter(), 5));
soldier.ShowAttribute();
ICharacter enemy = Direct.GetCharacter(new EnemyBuilder(new EnemyCharacter(), 10));
enemy.ShowAttribute();
}
}
/// <summary>
/// 角色
/// </summary>
public abstract class ICharacter
{
//角色有三个属性:标签、攻击力、等级(自由赋值)
//tag是自动赋值的属性,level是传递的属性,attack是根据level计算的属性。
protected string mTag;
public string Tag { set { mTag = value; } }
protected int mAttack;
public int Attack { set { mAttack = value; } }
protected int mLevel;
public int Level { get { return mLevel; } set { mLevel = value; } }
/// <summary>
/// 角色模型资源
/// </summary>
protected GameObject mGameObject;
public GameObject GameObject { set { mGameObject = value; } }
public void ShowAttribute()
{
Debug.Log("标签:" + mTag);
Debug.Log("攻击力:" + mAttack);
Debug.Log("等级:" + mLevel);
}
}
public class SoldierCharacter:ICharacter
{
}
public class EnemyCharacter : ICharacter
{
}
/// <summary>
/// 角色建造者
/// </summary>
public abstract class ICharacterBuilder
{
protected ICharacter mCharacter;
public ICharacterBuilder(ICharacter Character , int lv)
{
mCharacter = Character;
mCharacter.Level = lv;
}
public abstract void AddCharacterAttribute();
public abstract void AddGameObject();
public abstract ICharacter GetResult();
}
/// <summary>
/// 建造士兵
/// </summary>
public class SoldierBuilder : ICharacterBuilder
{
public SoldierBuilder(ICharacter Character, int lv)
:base(Character, lv)
{
}
public override void AddCharacterAttribute()
{
mCharacter.Tag = "战士";
mCharacter.Attack = mCharacter.Level * 5;
}
public override void AddGameObject()
{
mCharacter.GameObject = Resources.Load("SoldierGameobject") as GameObject;
}
public override ICharacter GetResult()
{
return mCharacter;
}
}
/// <summary>
/// 建造敌人
/// </summary>
public class EnemyBuilder : ICharacterBuilder
{
public EnemyBuilder(ICharacter Character, int lv)
: base(Character, lv)
{
}
public override void AddCharacterAttribute()
{
mCharacter.Tag = "敌人";
mCharacter.Attack = mCharacter.Level * 10;
}
public override void AddGameObject()
{
mCharacter.GameObject = Resources.Load("EnemyGameobject") as GameObject;
}
public override ICharacter GetResult()
{
return mCharacter;
}
}
public class Direct
{
public static ICharacter GetCharacter(ICharacterBuilder builder)
{
builder.AddCharacterAttribute();
builder.AddGameObject();
return builder.GetResult();
}
}
3、建造者模式优缺点
优点
- 有利于系统扩展,符合开闭原则
- 增强代码复用性
缺点
- 会产生很多的类