python做网站有什么优势,wordpress 电影网站,网站建设运营规划,太原网站优化多少钱文章目录 实现举例应用钩子 Hook 模板方法模式是一种基于继承的设计模式#xff0c;由两部分构成#xff1a;
抽象父类#xff08;一般封装了子类的算法框架#xff09;具体的实现子类
实现
简单地通过继承就可以实现
举例
足球赛 和 篮球赛 都有 3 个步骤#xff0c… 文章目录 实现举例应用钩子 Hook 模板方法模式是一种基于继承的设计模式由两部分构成
抽象父类一般封装了子类的算法框架具体的实现子类
实现
简单地通过继承就可以实现
举例
足球赛 和 篮球赛 都有 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();}
}应用
常被架构师用来搭建项目的框架程序员负责往里面填空
比如 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();}
}