游戏开发中,许多需要我们建造的东西,比如说 塔,子弹,人物等等。。。。。
那么频繁的去创建或者去回收的话,会让我们的代码看起来不是那么的利索,
下面来看看建造者模式的作用把:
在用户不知道对象的建造过程和细节的情况下就可以直接创建复杂的对象。

用户只需要给出指定复杂对象的类型和内容;
建造者模式负责按顺序创建复杂对象(把内部的建造过程和细节隐藏起来)

抽象工厂模式解决了“系列产品”的需求变化,而 建造者模式解决的是 “产品部分” 的需要变化。

方便用户创建复杂的对象(不需要知道实现过程)

代码复用性 、封装性(将对象构建过程和细节进行封装 & 复用)

例子:造汽车 & 买汽车

1.不同的工厂(建造者):负责制造相应的汽车(组装过程和细节在工厂内)

2. 销售者(指挥者):卖什么车(车的型号和内容,告诉厂家,这个车销量好,多造点)

3. 汽车购买者(用户):你只需要说出你需要的型号(对象的类型和内容),然后直接购买就可以使用了 (不需要知道汽车是怎么组装的(车轮、车门、发动机、方向盘等等))

下面的这个为塔的原型图UML:

设计模式-建造者模式(Builder)_ide


原型代码的话这里就不上了,网上大把:

我们主要看在 Unity 中怎么去实现它:

设计模式-建造者模式(Builder)_System_02


最后实现的一种效果就是这样的,完成了以后 点击生产即可进行旋转

//==========================
// - FileName: IBuilder.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:26:42
// - Region: China WUHAN
// - Description: 建造者接口
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public interface IBuilder
{
void AddCarBody();
void AddLight();
void AddWheel();
void AddPipe();
//获取车辆结果
ICar GetResult();
}

//==========================
// - FileName: BaseBuilder.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:42:29
// - Region: China WUHAN
// - Description: 建造者基类
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseBuilder : IBuilder
{
protected ICar car = new ICar();
public virtual void AddCarBody()
{

}

public virtual void AddLight()
{

}

public virtual void AddPipe()
{

}

public virtual void AddWheel()
{

}

public virtual ICar GetResult()
{
return car;
}

}
//==========================
// - FileName: Director.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:51:02
// - Region: China WUHAN
// - Description:
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Director
{
//指定给建造者构建
public ICar GetCar(IBuilder bulider)
{
bulider.AddCarBody();
bulider.AddLight();
bulider.AddPipe();
bulider.AddWheel();
return bulider.GetResult();
}
}
//==========================
// - FileName: ICar.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:41:00
// - Region: China WUHAN
// - Description:
//==========================
using System.Collections.Generic;
using UnityEngine;

public class ICar
{
private List<string> mParts = new List<string>();

/// <summary>
/// 添加部件
/// </summary>
public void AddPart(string part)
{
mParts.Add(part);
}

public void ShowAllPart()
{
Debug.Log("车辆组件创建情况");
foreach (string item in mParts)
{
Debug.Log(item);
}
}

}


public class JeepCar : ICar
{

}

public class AUDI : ICar
{

}
//==========================
// - FileName: JeepCarBuilder.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:42:15
// - Region: China WUHAN
// - Description:
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JeepCarBuilder : BaseBuilder
{

private GameObject mJeepCarModel;
private GameObject mJeepCarBody;
private GameObject mJeepCarLight;
private GameObject mJeepCarPipe;
private GameObject mJeepCarWheel;

public JeepCarBuilder()
{
mJeepCarModel = ResourceManager.Instance.GetObj(StringManager.Jeep_Car);
mJeepCarModel.transform.SetParent(GameObject.Find("GameRoot").transform);
mJeepCarModel.transform.position = new Vector3(-2, 0, 0);
}

public override void AddCarBody()
{
base.AddCarBody();
mJeepCarBody = ResourceManager.Instance.GetObj(StringManager.Jeep_Car_Body);
mJeepCarBody.transform.SetParent(mJeepCarModel.transform);
mJeepCarBody.transform.position = new Vector3(-2, 0, 0);
}

public override void AddLight()
{
base.AddLight();
mJeepCarLight = ResourceManager.Instance.GetObj(StringManager.Jeep_Car_Light);
mJeepCarLight.transform.SetParent(mJeepCarModel.transform);
mJeepCarLight.transform.position = new Vector3(-2, 0, 0);
}

public override void AddPipe()
{
base.AddPipe();
mJeepCarPipe = ResourceManager.Instance.GetObj(StringManager.Jeep_Car_Pipe);
mJeepCarPipe.transform.SetParent(mJeepCarModel.transform);
mJeepCarPipe.transform.position = new Vector3(-2, 0, 0);

}

public override void AddWheel()
{
base.AddWheel();
mJeepCarWheel = ResourceManager.Instance.GetObj(StringManager.Jeep_Car_Wheel);
mJeepCarWheel.transform.SetParent(mJeepCarModel.transform);
mJeepCarWheel.transform.position = new Vector3(-2, 0, 0);

}

public override ICar GetResult()
{
Debug.Log("开始组装");

return base.GetResult();
}

}
//==========================
// - FileName: AUDICarBuilder.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:49:04
// - Region: China WUHAN
// - Description:
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AUDICarBuilder : BaseBuilder
{
private GameObject mAudiCarModel;
private GameObject mAudiCarBody;
private GameObject mAudiCarLight;
private GameObject mAudiCarPipe;
private GameObject mAudiCarWheel;

public AUDICarBuilder()
{
mAudiCarModel = ResourceManager.Instance.GetObj(StringManager.AUDI_Car);
mAudiCarModel.transform.SetParent(GameObject.Find("GameRoot").transform);
mAudiCarModel.transform.position = new Vector3(2, 0, 0);
}

public override void AddCarBody()
{
base.AddCarBody();
mAudiCarBody = ResourceManager.Instance.GetObj(StringManager.AUDI_Car_Body);
mAudiCarBody.transform.SetParent(mAudiCarModel.transform);
mAudiCarBody.transform.position = new Vector3(2, 0, 0);
}

public override void AddLight()
{
base.AddLight();
mAudiCarLight = ResourceManager.Instance.GetObj(StringManager.AUDI_Car_Light);
mAudiCarLight.transform.SetParent(mAudiCarModel.transform);
mAudiCarLight.transform.position = new Vector3(2, 0, 0);
}

public override void AddPipe()
{
base.AddPipe();
mAudiCarPipe = ResourceManager.Instance.GetObj(StringManager.AUDI_Car_Pipe);
mAudiCarPipe.transform.SetParent(mAudiCarModel.transform);
mAudiCarPipe.transform.position = new Vector3(2, 0, 0);
}

public override void AddWheel()
{
base.AddWheel();
mAudiCarWheel = ResourceManager.Instance.GetObj(StringManager.AUDI_Car_Whell);
mAudiCarWheel.transform.SetParent(mAudiCarModel.transform);
mAudiCarWheel.transform.position = new Vector3(2, 0, 0);
}

public override ICar GetResult()
{
return base.GetResult();
}
}
//==========================
// - FileName: BuilderTest.cs
// - Created: true.
// - CreateTime: 2020/05/23 19:52:14
// - Region: China WUHAN
// - Description: 客户端
//==========================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BuilderTest : MonoBehaviour
{
private GameObject mBuilderPanel;

private Button mBtn_CreateJeep;
private Button mBtn_Create_Jeep_Body;
private Button mBtn_Create_Jeep_Wheel;
private Button mBtn_Create_Jeep_Light;
private Button mBtn_Create_Jeep_Pipe;

private Button mBtn_CreateAUDI;
private Button mBtn_Create_AUDI_Body;
private Button mBtn_Create_AUDI_Wheel;
private Button mBtn_Create_AUDI_Light;
private Button mBtn_Create_AUDI_Pipe;

IBuilder Jeep;
IBuilder AUDI;
private void Start()
{
InitPanel();
GetMod();
BtnEvent();
Jeep = new JeepCarBuilder();
AUDI = new AUDICarBuilder();
}

private void GetMod()
{
//获取吉普
mBtn_CreateJeep = mBuilderPanel.transform.Find("Btn_CreateJeep").GetComponent<Button>();
mBtn_Create_Jeep_Body = mBuilderPanel.transform.Find("Btn_Create_Jeep_Body").GetComponent<Button>();
mBtn_Create_Jeep_Wheel = mBuilderPanel.transform.Find("Btn_Create_Jeep_Wheel").GetComponent<Button>();
mBtn_Create_Jeep_Light = mBuilderPanel.transform.Find("Btn_Create_Jeep_Light").GetComponent<Button>();
mBtn_Create_Jeep_Pipe = mBuilderPanel.transform.Find("Btn_Create_Jeep_Pipe").GetComponent<Button>();
//获取奥迪
mBtn_CreateAUDI = mBuilderPanel.transform.Find("Btn_CreateAUDI").GetComponent<Button>();
mBtn_Create_AUDI_Body = mBuilderPanel.transform.Find("Btn_Create_AUDI_Body").GetComponent<Button>();
mBtn_Create_AUDI_Wheel = mBuilderPanel.transform.Find("Btn_Create_AUDI_Wheel").GetComponent<Button>();
mBtn_Create_AUDI_Light = mBuilderPanel.transform.Find("Btn_Create_AUDI_Light").GetComponent<Button>();
mBtn_Create_AUDI_Pipe = mBuilderPanel.transform.Find("Btn_Create_AUDI_Pipe").GetComponent<Button>();

}

/// <summary>
/// 初始化 UI 面板
/// </summary>
private void InitPanel()
{
mBuilderPanel = ResourceManager.Instance.GetObj(StringManager.BuilderPanel);
mBuilderPanel.transform.SetParent(GameObject.Find("Canvas").transform);
mBuilderPanel.transform.localPosition = Vector3.zero;
}


private bool isOk = false;
/// <summary>
/// 按钮回调事件
/// </summary>
private void BtnEvent()
{
//Jeep
mBtn_Create_Jeep_Body.onClick.AddListener(() =>
{
mBtn_Create_Jeep_Body.interactable = false;
Jeep.AddCarBody();
});

mBtn_Create_Jeep_Wheel.onClick.AddListener(() =>
{
mBtn_Create_Jeep_Wheel.interactable = false;
Jeep.AddWheel();
});

mBtn_Create_Jeep_Light.onClick.AddListener(() =>
{
mBtn_Create_Jeep_Light.interactable = false;
Jeep.AddLight();
});
mBtn_Create_Jeep_Pipe.onClick.AddListener(() =>
{
mBtn_Create_Jeep_Pipe.interactable = false;
Jeep.AddPipe();
});

mBtn_CreateJeep.onClick.AddListener(() =>
{
isJeepRotate = true;
});


//AODI
mBtn_Create_AUDI_Body.onClick.AddListener(() =>
{
mBtn_Create_AUDI_Body.interactable = false;
AUDI.AddCarBody();
});

mBtn_Create_AUDI_Wheel.onClick.AddListener(() =>
{
mBtn_Create_AUDI_Wheel.interactable = false;
AUDI.AddWheel();
});

mBtn_Create_AUDI_Light.onClick.AddListener(() =>
{
mBtn_Create_AUDI_Light.interactable = false;
AUDI.AddLight();
});
mBtn_Create_AUDI_Pipe.onClick.AddListener(() =>
{
mBtn_Create_AUDI_Pipe.interactable = false;
AUDI.AddPipe();
});

mBtn_CreateAUDI.onClick.AddListener(() =>
{
isAUDIRotate = true;
});
}

private bool isJeepRotate = false;
private bool isAUDIRotate = false;
private void Update()
{
if (isJeepRotate)
{
CarRotate(GameObject.Find("Jeep_Car_Model(Clone)").transform);
}
if (isAUDIRotate)
{
CarRotate(GameObject.Find("AUDI_Car_Model(Clone)").transform);
}
}

//物体组合完成以后自转
private void CarRotate(Transform rotateObj)
{
rotateObj.transform.Rotate(Vector3.up * 0.3f, Space.World);
}


//private void OnGUI()
//{
// if (GUI.Button(new Rect(100,100,200,100),"生产Jeep"))
// {
// //Debug.Log("创建 Jeep 成功");
// IBuilder jeep = new JeepCarBuilder();
// ICar jeepCar = Director.GetPerson(jeep);
// jeepCar.ShowAllPart();
// }
// if (GUI.Button(new Rect(100, 300, 200, 100), "生产AUDI"))
// {
//
//
//
//
// }
//}
}