模板方法模式:
顾名思义,模板方法模式就是有一个“模板方法”,在执行的时候,就按照模板来(模板中一些具体方法有不同的具体操作)例如:去饭店吃饭:点单,吃饭,买单。 这3个流程相对固定,形成一个模板,具体的吃饭细节可以有不同的表现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class DM04TempleMethod:MonoBehaviour
{
private void Start()
{
IPeople people = new NorthPeople();
people.Eat();
IPeople p2 = new SouthPeople();
p2.Eat();
}
}
public abstract class IPeople
{
public void Eat()
{
OrderFoods();
EatSomething();
PayBill();
}
private void OrderFoods()
{
Debug.Log("点单");
}
public abstract void EatSomething();
private void PayBill()
{
Debug.Log("买单");
}
}
public class NorthPeople : IPeople
{
public override void EatSomething()
{
Debug.Log("我在吃馒头");
}
}
public class SouthPeople:IPeople
{
public override void EatSomething()
{
Debug.Log("我吃米饭");
}
}
下面是武器系统中模板方法模式的简单实际应用:入口在抽象类的 模板方法Fire中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public abstract class IWeapon
{
protected int mAtk;
protected float mAtkRange;
protected int mAtkPlusValue;
protected GameObject mGameObject;
protected ICharacter mOwner;
protected ParticleSystem mParticleSystem;
protected LineRenderer mLine;
protected Light mLight;
protected AudioSource mAudio;
protected float mEffectDisplayTime;
public void Fire(Vector3 targetPosition)
{
PlayMuzzleEffect();
PlayBulletEffect(targetPosition);
SetEffectDisplayTime();
PlaySound();
}
public void Update()
{
if (mEffectDisplayTime >=0)
{
mEffectDisplayTime -= Time.deltaTime;
}else
{
DisableEffect();
}
}
public void DisableEffect()
{
mLight.enabled = false;
mLight.enabled = false;
}
public abstract void PlayMuzzleEffect();
protected void DoPlayMuzzleEffect()
{
//显示枪口特效
mParticleSystem.Stop();
mParticleSystem.Play();
mLight.enabled = true;
}
public abstract void PlayBulletEffect(Vector3 targetPosition);
protected void DoPlayBulletEffect(float width, Vector3 targetPosition)
{
//显示子弹轨迹特效
mLine.enabled = true;
mLine.startWidth = 0.1f; mLine.endWidth = 0.1f;
mLine.SetPosition(0, mGameObject.transform.position);
mLine.SetPosition(1, targetPosition);
}
public abstract void PlaySound();
protected void DoPlaySound(string name)
{
AudioClip clip = null;
mAudio.clip = clip;
mAudio.Play();
}
protected abstract void SetEffectDisplayTime();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class WeaponGun:IWeapon
{
public override void PlayBulletEffect(Vector3 targetPosition)
{
DoPlayBulletEffect(0.05f,targetPosition);
}
public override void PlayMuzzleEffect()
{
DoPlayMuzzleEffect();
}
public override void PlaySound()
{
DoPlaySound("GunShot");
}
protected override void SetEffectDisplayTime()
{
mEffectDisplayTime = 0.5f;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class WeaponRocket : IWeapon
{
public override void PlayBulletEffect(Vector3 targetPosition)
{
DoPlayBulletEffect(0.2f,targetPosition);
}
public override void PlayMuzzleEffect()
{
DoPlayMuzzleEffect();
}
public override void PlaySound()
{
DoPlaySound("RocketShot");
}
protected override void SetEffectDisplayTime()
{
mEffectDisplayTime = 0.5f;
}
}