• 模板方法模式,基于继承实现的简单的设计模式(设计模式与开发实践 P11)


    模板方法模式是一种基于继承的设计模式,由两部分构成:

    • 抽象父类(一般封装了子类的算法框架)
    • 具体的实现子类

    实现

    简单地通过继承就可以实现

    举例

    足球赛 和 篮球赛 都有 3 个步骤,初始化,开始游戏,结束游戏

    我们发现他们都有这个过程,就可以把相同的点提取出来,设置成一个模板!

    这里我们举例的 3 个方法都是抽象方法,有时如果子类的行为是一致的,可以直接实现具体方法~

    using System;
    
    public abstract class Game
    {
        public void Play()
        {
            Initialize();
            StartGame();
            EndGame();
        }
    
        protected abstract void Initialize();
    
        protected abstract void StartGame();
    
        protected abstract void EndGame();
    }
    
    public class FootballGame : Game
    {
        protected override void Initialize()
        {
            Console.WriteLine("Football game initialized. Setting up teams and players.");
        }
    
        protected override void StartGame()
        {
            Console.WriteLine("Football game started. Kickoff!");
        }
    
        protected override void EndGame()
        {
            Console.WriteLine("Football game ended. Final score and statistics displayed.");
        }
    }
    
    public class BasketballGame : Game
    {
        protected override void Initialize()
        {
            Console.WriteLine("Basketball game initialized. Setting up teams and players.");
        }
    
        protected override void StartGame()
        {
            Console.WriteLine("Basketball game started. Tip-off!");
        }
    
        protected override void EndGame()
        {
            Console.WriteLine("Basketball game ended. Final score and statistics displayed.");
        }
    }
    
    public class Program
    {
        public static void Main(string[] args)
        {
            Game footballGame = new FootballGame();
            footballGame.Play();
    
            Console.WriteLine();
    
            Game basketballGame = new BasketballGame();
            basketballGame.Play();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    应用

    常被架构师用来搭建项目的框架,程序员负责往里面填空

    比如 Java 程序员经常用 HttpServlet 来开发项目,他包含 7 个生命周期,每个生命周期都对应一个 do 方法,这些方法就需要 HttpServlet 的子类进行 具体实现

    钩子 Hook

    根据上面的例子,如果有一种非常特别的球赛不需要 Initialize 就可以开始呢?

    我们可以在容易变化的方法处设置一个 Hook,他可以有一个默认的实现,需不需要 Hook 挂钩则由子类自行决定,这样程序就有了变化的可能~

    using System;
    
    public abstract class Game
    {
        public void Play()
        {
            Initialize();
            StartGame();
            EndGame();
        }
    
        protected virtual void Initialize()
        {
            Console.WriteLine("Game initialized. Setting up teams and players.");
            // 在这里添加挂钩(Hook)行为
            AdditionalInitialization();
        }
    
        protected abstract void StartGame();
    
        protected abstract void EndGame();
    
        protected virtual void AdditionalInitialization()
        {
            // 默认的挂钩行为为空
        }
    }
    
    public class FootballGame : Game
    {
        protected override void StartGame()
        {
            Console.WriteLine("Football game started. Kickoff!");
        }
    
        protected override void EndGame()
        {
            Console.WriteLine("Football game ended. Final score and statistics displayed.");
        }
    
        protected override void AdditionalInitialization()
        {
            Console.WriteLine("Additional initialization for Football game.");
        }
    }
    
    public class BasketballGame : Game
    {
        protected override void StartGame()
        {
            Console.WriteLine("Basketball game started. Tip-off!");
        }
    
        protected override void EndGame()
        {
            Console.WriteLine("Basketball game ended. Final score and statistics displayed.");
        }
    
        protected override void AdditionalInitialization()
        {
            Console.WriteLine("Additional initialization for Basketball game.");
        }
    }
    
    public class Program
    {
        public static void Main(string[] args)
        {
            Game footballGame = new FootballGame();
            footballGame.Play();
    
            Console.WriteLine();
    
            Game basketballGame = new BasketballGame();
            basketballGame.Play();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
  • 相关阅读:
    一网打尽异步神器CompletableFuture
    在职读博|中国社科院-英国斯特大学灵合作办学双证管理学博士
    cnpm安装和使用
    nacos配置中心
    【性能测试】Jmeter+InfluxDB+Grafana 搭建性能监控平台
    MyEclipse2019配置TomCat8.0
    最新AI智能创作系统源码V2.6.2/AI绘画系统/支持GPT联网提问/支持Prompt应用
    Access,Trunk,Hybrid的一些接触知识以及实验
    JSD-2204-Seata(续)-Sentinel-SpringGateway网关-Day04
    《想要守护书的猫》阅读笔记
  • 原文地址:https://blog.csdn.net/Littlelumos/article/details/133630224