using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassSimpleFactoryTest:MonoBehaviour{// Use this for initializationvoid Start (){//简单工厂模式 //通过不同的类型 产生或者返回 相对应的对象//继承 抽象 接口//MobilePhone mobilePhone = MobilePhoneFactory.CreateMobilePhone(PhoneType.Iphone);//mobilePhone.Print();//上传具体手机类型MobilePhone xiaomiMobilePhone = MobilePhoneFactory.CreateMobilePhone(PhoneType.XiaoMI);
xiaomiMobilePhone.Print();//调用实现类方法}// Update is called once per framevoid Update (){}}/// <summary>/// 抽象类/// </summary>publicabstractclassMobilePhone{publicabstractvoidPrint();}/// <summary>/// 实现类/// </summary>publicclassIphone:MobilePhone{publicoverridevoidPrint(){
Debug.Log("我是苹果手机");}}publicclassXiaoMi:MobilePhone{publicoverridevoidPrint(){
Debug.Log("我是小米手机");}}publicclassSmratisan:MobilePhone{publicoverridevoidPrint(){
Debug.Log("我是锤子手机");}}/// <summary>/// 枚举/// </summary>publicenum PhoneType
{
Iphone,
XiaoMI,
Smratisan,}publicclassMobilePhoneFactory{/// <summary>/// 外部传过来一个手机类型/// </summary>/// <returns>返回一部手机的对象</returns>publicstaticMobilePhoneCreateMobilePhone(PhoneType type){MobilePhone mobilePhone =null;switch(type){case PhoneType.Iphone:
mobilePhone =newIphone();break;case PhoneType.XiaoMI:
mobilePhone =newXiaoMi();break;case PhoneType.Smratisan:
mobilePhone =newSmratisan();break;default:break;}return mobilePhone;}}
抽象工厂
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassAbstractFactoryTest:MonoBehaviour{// Use this for initializationvoid Start (){AbstractFactory xiaomiFactory=newXiaomiFactory();MotherBoard motherBoard = xiaomiFactory.CreateMotherBoard();
motherBoard.Print();Screen screen = xiaomiFactory.CreateScreen();
screen.Print();}// Update is called once per framevoid Update (){}}/// <summary>/// 抽象工厂类/// </summary>publicabstractclassAbstractFactory{publicabstractScreenCreateScreen();publicabstractMotherBoardCreateMotherBoard();}/// <summary>/// 抽象屏幕类/// </summary>publicabstractclassScreen{publicabstractvoidPrint();}/// <summary>/// 抽象主板类/// </summary>publicabstractclassMotherBoard{publicabstractvoidPrint();}publicclassAppleScreen:Screen{publicoverridevoidPrint(){
Debug.Log("苹果手机屏幕");}}publicclassXiaoMiScreen:Screen{publicoverridevoidPrint(){
Debug.Log("小米屏幕");}}publicclassAppleMotherBoard:MotherBoard{publicoverridevoidPrint(){
Debug.Log("苹果主板");}}publicclassXiaoMiMotherBoard:MotherBoard{publicoverridevoidPrint(){
Debug.Log("小米主板");}}publicclassAppleFactory:AbstractFactory{publicoverrideMotherBoardCreateMotherBoard(){returnnewAppleMotherBoard();}publicoverrideScreenCreateScreen(){returnnewAppleScreen();}}publicclassXiaomiFactory:AbstractFactory{publicoverrideMotherBoardCreateMotherBoard(){returnnewXiaoMiMotherBoard();}publicoverrideScreenCreateScreen(){returnnewXiaoMiScreen();}}
对象池
using System.Collections;using System.Collections.Generic;using UnityEngine;publicclassObjectPool:MonoBehaviour{//monster的模板publicGameObject mMonster;//monster的对象池private Stack<GameObject> monsterPool;//在当前场景中正在使用的 激活的monsterprivate Stack<GameObject> activeMonsterPool;// Use this for initializationvoid Start (){
monsterPool =newStack<GameObject>();
activeMonsterPool =newStack<GameObject>();}// Update is called once per framevoid Update (){if(Input.GetMouseButtonDown(0)){//获取一个monster对象GameObject monster =GetMonster();
monster.transform.position = Vector3.zero;
activeMonsterPool.Push(monster);}if(Input.GetMouseButtonDown(1)){if(activeMonsterPool.Count>0){PushGameObjectToPool(activeMonsterPool.Pop());}}}/// <summary>/// 获取一个monster/// </summary>/// <returns></returns>privateGameObjectGetMonster(){GameObject monster =null;if(monsterPool.Count >0){//池子中有对象
monster = monsterPool.Pop();
monster.SetActive(true);}else{//对象池中没有对象
monster =Instantiate(mMonster);}return monster;}privatevoidPushGameObjectToPool(GameObject monster){
monster.transform.SetParent(transform);
monster.SetActive(false);
monsterPool.Push(monster);}}