设计模式---Template Pattern

Template Pattern

  • 模板模式:将一个算法分成几个部分,分析算法中共同的部分和变化的部分,将算法的骨架抽象到一个类,变化的部分在子类中实现,可以为算法中共同部分设置默认的实现。

代码实现

/*
 *Template Method:模板模式
 */
using System;

namespace Pattern01
{
    class Program
    {
        static void Main(string[] args)
        {


            AbstractClass aA = new ConcreteClassA();
            aA.TemplateMethod();

            ///
            AbstractClass aB = new ConcreteClassB();
            aB.TemplateMethod();

            Console.ReadKey();
        }
    }

    abstract class AbstractClass
    {
        public abstract void PrimitiveOper1();
        public abstract void PrimitiveOper2();
        public void TemplateMethod()
        {
            PrimitiveOper1();
            PrimitiveOper2();
            Console.WriteLine(" ");
        }
    }
    class ConcreteClassA : AbstractClass
    {
        public override void PrimitiveOper1()
        {
            Console.WriteLine("ConcreteClassA.primitiveOper1");
        }
        public override void PrimitiveOper2()
        {
            Console.WriteLine("ConcreteClassA.primitiveOper2()");
        }
    }

    class ConcreteClassB : AbstractClass
    {
        public override void PrimitiveOper1()
        {
            Console.WriteLine("ConcreteClassB.primitiveOper1()");
        }
        public override void PrimitiveOper2()
        {
            Console.WriteLine("ConcreteClassB.primitiveOper2()");
        }
    }


}

  • 类图
    在这里插入图片描述

示例2

/*
 * Template Method Pattern
 */
using System;
using System.Collections;

namespace Pattern02
{
    class Program
    {

        static void Main(string[] args)
        {

            Game game = new FootBall();
            game.Play();

            game = new Swiming();
            game.Play();

            Console.ReadLine();
        }

    }
    abstract class Game
    {
        public virtual void Initialize()
        {
            Console.WriteLine("Game initialize() for ready....");
        }
        public abstract void Finished();
        public abstract void Start();
        /// <summary>
        /// 默认的实现方法,可以子类可重写,也可不重写。
        /// </summary>
        public virtual void GivenScore()
        {
            Console.WriteLine("Game score is given");
        }

        /// <summary>
        ///  The 'Template Method' 
        /// </summary>
        public  void  Play()
        {
            Initialize();
            Start();
            GivenScore();
            Finished();
            Console.WriteLine(" ");
        }
    }
    class FootBall : Game
    {
        public override void Initialize()
        {
            Console.WriteLine("football game initialize()");
        }
        public override void Start()
        {
            Console.WriteLine("football game start.....");
        }
        public override void Finished()
        {
            Console.WriteLine("football game finished....");
        }
    }

    class Swiming : Game
    {
        public override void Start()
        {
            Console.WriteLine("swiming game start()");
        }
        public override void Finished()
        {
            Console.WriteLine("swiming game finished()");
        }
    }
}

总结

  1. Analyze the target algorithm to see whether you can break it
    into steps. Consider which steps are common to all subclasses
    and which ones will always be unique.
  2. Create the abstract base class and declare the template
    method and a set of abstract methods representing the algorithm’s
    steps. Outline the algorithm’s structure in the template
    method by executing corresponding steps. Consider making
    the template method final to prevent subclasses from overriding
    it.
  3. It’s okay if all the steps end up being abstract. However, some
    steps might benefit from having a default implementation.
    Subclasses don’t have to implement those methods.
  4. Think of adding hooks between the crucial steps of the
    algorithm.
  5. For each variation of the algorithm, create a new concrete subclass.
    It must implement all of the abstract steps, but may also
    override some of the optional ones.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值