惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
T
Threatpost
T
Tenable Blog
有赞技术团队
有赞技术团队
大猫的无限游戏
大猫的无限游戏
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
C
Cisco Blogs
H
Heimdal Security Blog
Attack and Defense Labs
Attack and Defense Labs
A
About on SuperTechFans
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
V
V2EX
Cyberwarzone
Cyberwarzone
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
V
Vulnerabilities – Threatpost
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
PCI Perspectives
PCI Perspectives
P
Privacy International News Feed
D
Docker

博客园 - 青玄鸟

.NET 中优雅处理 Server-Sent Events 请求取消 vue3.0 + ts 实现上传工厂(oss与cos) Dapr 订阅者参数无法正确反序列化问题 .NET 代码整洁手册 Blazor项目通过docker和nginx部署为静态站点的步骤 Moq mock 方法返回null空指针异常 基于接口隔离原则的依赖注入实现 HttpClient with Stream HttpClient partial update HttpClient 基本使用 值对象的封装 只读集合类型属性实现 适配器模式 最少知识原则 单例模式 抽象工厂 简单工厂、工厂方法、抽象工厂 工厂方法模式 使用 Visual Studio Code创建和执行T-SQL
模板模式
青玄鸟 · 2020-04-04 · via 博客园 - 青玄鸟

定义

在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。

UML类图

实现

案例:以前后端开发流程为例,
得到需求 -> 打开工具编写代码 -> 测试

抽象开发类

    public abstract class Development
    {
        public void SolveProblems()
        {
            GetProblems();
            WriteCode();
            Testing();
        }

        public void GetProblems()
        {
            Console.WriteLine("获得新的需求");
        }

        public abstract void WriteCode();

        public void Testing()
        {
            Console.WriteLine("进行测试");
        }
    }

前端开发类

    public class Frontend : Development
    {

        public override void WriteCode()
        {
            Console.WriteLine("完成页面交互");
            Console.WriteLine("从后端接口获取数据");
            Console.WriteLine("数据绑定");
        }
    }

后端开发类

    public class Backend : Development
    {
        public override void WriteCode()
        {
            Console.WriteLine("业务逻辑开发");
            Console.WriteLine("存储操作");
            Console.WriteLine("接口封装");
        }
    }

抽象父类中的SolveProblems方法为模板方法,WriteCode定义为抽象方法,每个子类实现自己的算法。

模板方法还提供了钩子函数,用于控制模板方法中的流程或者重写父类中的某些行为(例如对模板方法返回的结果重新排序)。

比如

    public abstract class Development
    {
        public void SolveProblems()
        {
            GetProblems();
            WriteCode();
            Testing();
            if (IsGoodJob())
            {
                TeamBuilding();
            }
        }

        public void GetProblems()
        {
            Console.WriteLine("获得新的需求");
        }

        public abstract void WriteCode();

        public void Testing()
        {
            Console.WriteLine("进行测试");
        }

        public virtual bool IsGoodJob()
        {
            return true;
        }

        public void TeamBuilding()
        {
            Console.WriteLine("下班团建");
        }

    }
    public class FrontEnd : Development
    {

        public override void WriteCode()
        {
            Console.WriteLine("完成页面交互");
            Console.WriteLine("从后端接口获取数据");
            Console.WriteLine("数据绑定");
        }

        public override bool IsGoodJob()
        {
            return false;
        }
    }