TemplateMethod模式(模板方法模式)

模式简介

一个基类提供可重写的方法给派生类而可重写方法的调用由基类负责,这种模式就叫TemplateMethod模式。

模式UML图

代码示例(C#)

提示:可在本栏目的资源篇“设计模式代码示例合集”下载所有完整代码资源。

namespace Test1
{
    //测试类
    class Test44
    {
        public void Test()
        {
            Father carefulFather = new CarefulFather();
            Father carelessFather = new CarelessFather();
            Baby baby = new Baby(BabyStatus.Hungry);
            carefulFather.TakeCare(baby);
            carelessFather.TakeCare(baby);
        }
    }

    public enum BabyStatus
    {
        Sleepy, Hungry
    }

    public class Baby
    {
        public BabyStatus status { get; private set; }
        public Baby(BabyStatus p_status) { status = p_status; }
        public void Cry()
        {
            Console.WriteLine("The baby is crying,she feels " + status.ToString().ToLower() + ".");
        }
    }

    public class CarefulFather : Father
    {
        protected override void Option1(BabyStatus p_status)
        {
            if (p_status.Equals(BabyStatus.Hungry))
            {
                Console.WriteLine("Acording to the crying of baby,the careful father knows that the baby is hungry.");
                Console.WriteLine("Therefore,the father feeds the baby milk powder.");
            }
        }

        protected override void Option2(BabyStatus p_status)
        {
            if (p_status.Equals(BabyStatus.Sleepy))
            {
                Console.WriteLine("Acording to the crying of baby,the careful father knows that the baby is sleepy.");
                Console.WriteLine("Therefore,the father taps the baby to sleep.");
            }
        }
    }

    public class CarelessFather : Father
    {
        protected override void Option1(BabyStatus p_status)
        {
            if (p_status.Equals(BabyStatus.Sleepy))
            {
                Console.WriteLine("Acording to the crying of baby,the careless father mistakes the baby for hungry.");
                Console.WriteLine("Therefore,the father feeds the baby milk powder.");
            }
        }

        protected override void Option2(BabyStatus p_status)
        {
            if (p_status.Equals(BabyStatus.Hungry))
            {
                Console.WriteLine("Acording to the crying of baby,the careless father mistakes the baby for sleep.");
                Console.WriteLine("Therefore,the father taps the baby to sleep.");
            }
        }
    }

    public abstract class Father
    {
        public void TakeCare(Baby p_baby)
        {
            p_baby.Cry();
            Option1(p_baby.status);
            Option2(p_baby.status);
        }

        protected abstract void Option1(BabyStatus p_status);
        protected abstract void Option2(BabyStatus p_status);
    }
}

代码解说

基于这个示例,有一个Father基类,两个派生类CarefulFather和CarelessFather继承该基类,二者分别对Option1方法和Option2方法进行了重写,但是两个Option方法没有进行显示调用,因为两个方法的调用已经交给了Father基类负责,只需要调用TakeCare方法就会自动调用两个Option方法。

如果这篇文章对你有帮助,请给作者点个赞吧!

猜你喜欢

转载自blog.csdn.net/hgf1037882434/article/details/128405062