设计模式学习之路(八) ------ 模板模式(TempleMethod)

论坛 期权论坛 脚本     
匿名技术用户   2020-12-22 14:58   11   0

理论学习:

  • http://blog.csdn.net/lovelion/article/details/8299794
  • http://meigesir.iteye.com/blog/1506484

UML类图:


(1) AbstractClass(抽象类)在抽象类中定义了一系列基本操作(PrimitiveOperations),这些基本操作可以是具体的,也可以是抽象的,每一个基本操作对应算法的一个步骤,在其子类中可以重定义或实现这些步骤。同时,在抽象类中实现了一个模板方法(Template Method),用于定义一个算法的框架,模板方法不仅可以调用在抽象类中实现的基本方法,也可以调用在抽象类的子类中实现的基本方法,还可以调用其他对象中的方法。

(2) ConcreteClass(具体子类)它是抽象类的子类,用于实现在父类中声明的抽象基本操作以完成子类特定算法的步骤,也可以覆盖在父类中已经实现的具体基本操作。

原型实现:

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class DM04TempleMethod:MonoBehaviour
{
    void Start()
    {
        IPeople people = new NorthPeople();
        people.Eat();
    }
}

public abstract class IPeople
{
    public void Eat() //吃东西的方法
    {
        OrderFoods();  //点单
        EatSomething(); //吃
        PayBill(); //付账
    }
    private void OrderFoods()
    {
        Debug.Log("点单");
    }
    protected virtual void EatSomething() //吃的虚方法
    {

    }
    private void PayBill()
    {
        Debug.Log("买单");
    }
}
//实现吃的方法
public class NorthPeople : IPeople
{
    protected override void EatSomething()
    {
        Debug.Log("我在吃面条");
    }
}

public class SouthPeople : IPeople
{
    protected override void EatSomething()
    {
        Debug.Log("我在吃米饭");
    }
}

实例实现:

武器开火(不同武器开火)使用模板方法实现:

   public void Fire(Vector3 targetPosition)  //在武器抽象类中开火功能
    {
        //显示枪口特效
        PlayMuzzleEffect();

        //显示子弹轨迹特效
        PlayBulletEffect(targetPosition);

        //设置特效显示时间
        SetEffetDisplayTime();

        //播放声音
        PlaySound();
    }
   protected virtual void PlayMuzzleEffect()
    {
        mPariticle.Stop();
        mPariticle.Play();
        mLight.enabled = true;
    }
   protected abstract void PlayBulletEffect(Vector3 targetPosition);
   protected abstract void SetEffetDisplayTime();
   protected void DoPlayBulletEffect(float width,Vector3 targetPosition)
    {
        mLine.enabled = true;
        mLine.startWidth = width; mLine.endWidth = width;
        mLine.SetPosition(0, mGameObject.transform.position);
        mLine.SetPosition(1, targetPosition);
    }

    protected abstract void PlaySound();
    protected void DoPlaySound(string clipName)
    {
        AudioClip clip = FactoryManager.assetFactory.LoadAudioClip(clipName);
        mAudio.clip = clip;
        mAudio.Play();
    }   
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class WeaponRifle:IWeapon //长枪实现
{
    public WeaponRifle(WeaponBaseAttr baseAttr, GameObject gameObject) : base(baseAttr, gameObject) { }
    protected override void PlayBulletEffect(Vector3 targetPosition)
    {
        DoPlayBulletEffect(0.1f, targetPosition);
    }

    protected override void PlaySound()
    {
        DoPlaySound("RifleShot");
    }

    protected override void SetEffetDisplayTime()
    {
        mEffectDisplayTime = 0.3f;
    }
}
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class WeaponGun:IWeapon  //短枪实现
{
    public WeaponGun(WeaponBaseAttr baseAttr , GameObject gameObject) : base(baseAttr, gameObject) { }
    protected override void PlayBulletEffect(Vector3 targetPosition)
    {
        DoPlayBulletEffect(0.05f, targetPosition);
    }

    protected override void PlaySound()
    {
        DoPlaySound("GunShot");
    }

    protected override void SetEffetDisplayTime()
    {
        mEffectDisplayTime = 0.2f;
    }
}





分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP