好久没有更新设计模式系列教程了
本文给大家介绍建造者模式
博客地址:blog.liujunliang.com.cn
写在前面
在创建游戏对象时往往一个对象伴随着多个属性或创建逻辑
此时为了构建这个复杂的游戏对象将各个部分的逻辑进行分离(与工厂模式最主要的作用区别)
案例分析
建造者类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 建造者模式的建造者角色
/// </summary>
public abstract class IBuilder
{
public ICharacter character { get; set; }
public abstract void AddAttribute();
public abstract void AddCharacterGameobject();
public abstract void AddWeaponGameObject();
public abstract ICharacter ResultCharacter();
}
具体士兵建造者类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SliderBuilder : IBuilder
{
public SliderBuilder()
{
character = new SliderCharacter();
}
public override void AddAttribute()
{
character.SetAttribute();
}
public override void AddCharacterGameobject()
{
character.SetCharacter();
}
public override void AddWeaponGameObject()
{
character.SetWeapon();
}
public override ICharacter ResultCharacter()
{
Debug.Log("成功创建士兵");
return character;
}
}
具体敌人建造者类
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBuilder : IBuilder
{
public EnemyBuilder()
{
character = new EnemyCharacter();
}
public override void AddAttribute()
{
character.SetAttribute();
}
public override void AddCharacterGameobject()
{
character.SetCharacter();
}
public override void AddWeaponGameObject()
{
character.SetWeapon();
}
public override ICharacter ResultCharacter()
{
Debug.Log("成功创建敌人");
return character;
}
}
导演类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 建造者模式的导演角色
/// </summary>
public class Director : MonoBehaviour
{
public static ICharacter ConstructCharacter(IBuilder builder)
{
builder.AddAttribute();
builder.AddCharacterGameobject();
builder.AddWeaponGameObject();
return builder.ResultCharacter();
}
}
产品类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 建造者模式的产品角色
/// </summary>
public abstract class ICharacter
{
public abstract void SetAttribute();
public abstract void SetCharacter();
public abstract void SetWeapon();
}
士兵类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SliderCharacter:ICharacter
{
public string name { get; set; }
public string characterPrefabPath { get; set; }
public string weaponPrefabPath { get; set; }
public GameObject characterGameObject { get; set; }
public GameObject weaponGameObject { get; set; }
public override void SetAttribute()
{
name = "Slider";
characterPrefabPath = "SliderCharacterPrefabPath";
weaponPrefabPath = "SliderWeaponPrefabPath";
}
public override void SetCharacter()
{
// characterGameObject = GameObject.Instantiate(Resources.Load(characterPrefabPath) as GameObject);
}
public override void SetWeapon()
{
// characterGameObject = GameObject.Instantiate(Resources.Load(weaponPrefabPath) as GameObject);
}
}
敌人类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyCharacter : ICharacter
{
public string name { get; set; }
public string characterPrefabPath { get; set; }
public string weaponPrefabPath { get; set; }
public GameObject characterGameObject { get; set; }
public GameObject weaponGameObject { get; set; }
public override void SetAttribute()
{
name = "Enemy";
characterPrefabPath = "EnemyCharacterPrefabPath";
weaponPrefabPath = "EnemyWeaponPrefabPath";
}
public override void SetCharacter()
{
// characterGameObject = GameObject.Instantiate(Resources.Load(characterPrefabPath) as GameObject);
}
public override void SetWeapon()
{
// characterGameObject = GameObject.Instantiate(Resources.Load(weaponPrefabPath) as GameObject);
}
}
测试
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BuilderDesigner : MonoBehaviour
{
private void Start()
{
ICharacter slider = Director.ConstructCharacter(new SliderBuilder());
ICharacter enemy = Director.ConstructCharacter(new EnemyBuilder());
}
}
模式分析
模式定义:建造者模式是设计模式的一种,将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
模式优点:封装性很好、很容易的进行扩展
与工厂模式的对比:
同:
一般来说,创建的对象比较复杂时候用工厂模式,更复杂时候就用建造者模式
异:
工厂模式是将一个对象的全部创建过程封装在工厂类中,由于工厂类向客户端提供最终的产品;
而建造者模式中,建造者类一般只提供产品类中各个组件的建造,而将具体建造逻辑过程交付给导演类。
由于导演类负责将各个组件按照特定的规则组件为产品,然后将组件好的产品对象交付给客户端