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

文章目录

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

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

实现

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

举例

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

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

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

csharp 复制代码
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();
    }
}

应用

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

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

钩子 Hook

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

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

csharp 复制代码
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();
    }
}
相关推荐
zdr尽职尽责7 小时前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
BruceNeter2 天前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
Mh2 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊3 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
何以解忧唯有撸码3 天前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
dahaihua3333 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
国家一级摸鱼选手4 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交
汤姆宝库4 天前
BPE分词器的完整实现:字节对合并与词表构建
开发语言·人工智能·c#
Risehuxyc4 天前
C# 发展历史
开发语言·c#
wy_hhxx4 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent