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

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Blog of Author Tim Ferriss
B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
AWS News Blog
AWS News Blog
MongoDB | Blog
MongoDB | Blog
Y
Y Combinator Blog
Google Online Security Blog
Google Online Security Blog
T
Troy Hunt's Blog
I
InfoQ
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
C
Cisco Blogs
G
GRAHAM CLULEY
The Register - Security
The Register - Security
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
H
Hacker News: Front Page
小众软件
小众软件
雷峰网
雷峰网
The Hacker News
The Hacker News
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Tor Project blog
博客园 - 聂微东
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
腾讯CDC
P
Palo Alto Networks Blog
Scott Helme
Scott Helme

博客园 - Cameo

解决VSTO项目重定向到.NET Framework 4的编译和运行时错误 Excel 应用程序如何创建数据透视表 值类型与引用类型 Visual studio tools for office体系结构 C#类型转换 VSTO开发团队成立 15. Command 命令(行为型模式) 13. Proxy代理(结构型模式) 12. Flyweight享元(结构型模式) 11. Facade外观[门面模式](结构型模式) 10. Decorator 装饰(结构型模式) 9. Composite 组合(结构型模式) 8. Bridge 桥接(结构型模式) 7. Adapter 适配器(结构型模式) 6. Prototype 原型(创建型模式) 5. Factory Method 工厂方法(创建型模式的基础) 4.Builder 建造者(生成器)(创建型模式) 书籍收藏 3. Abstract Factory 抽象工厂(创建型模式)
14. Template Method模板方法(行为型模式)
Cameo · 2008-07-28 · via 博客园 - Cameo

无处不在的Template Method
     如果你只想掌握一种设计模式,那么它就是Template Method!

     变化——是软件设计的永恒主题,如何管理变化带来的复杂性?设计模式的艺术性和复杂度就在于如何分析,并发现系统中的变化点和稳定点,并使用特定的设计方法来应对这种变化。

动机(Motivation)
     在软件构建过程中,对于某一项任务,它常常有稳定的整体操作结构,但各个子步骤却有很多改变的需求,或者由于固有的原因(比如框架与应用之间的关系)而无法和任务的整体结构同时实现。
     如何在确定稳定操作结构的前提下,来灵活应对各个子步骤的变化或者晚期实现需求?

意图(Intent)
     定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。Template Method使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤
                                                                                                                                                      ——《设计模式》GoF

结构(Structure)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Template_method
{
    
//框架开发者-先开发

    
public abstract class Vehical //表示汽车
    {
        
protected abstract void startup();//业界的共识 虚方法前加protected
        protected abstract void run();
        
protected abstract void turn(int degree);
        
protected abstract void stop();
        
public void Test()  //非虚方法前加public
        {
            
// 测试数据记录
            startup();//晚绑定-留给应用程序开发人员;扩展点

            
// 测试数据记录
            run();//晚绑定-留给应用程序开发人员;扩展点

            
// 测试数据记录
            turn(40);//晚绑定-留给应用程序开发人员;扩展点

            
// 测试数据记录
            stop();//晚绑定-留给应用程序开发人员;扩展点

            
//生成测试报表
        }

    
    }



    
//应用程序开发-晚开发

    
public class HongqiCar : Vehical
    
{
        
protected override void startup()
        
{
            Console.WriteLine(
"startup");
        }


        
protected override void run()
        
{
            Console.WriteLine(
"run");
        }

        
protected override void turn(int degree)
        
{
            Console.WriteLine(
"degree");
        }

        
protected override void stop()
        
{
            Console.WriteLine(
"stop");
        }


    }


    
class VehicalTestFramework
    
{
        
public static void DoTest(Vehical vehical)
        
{
            vehical.Test();
        }

    }

    
class Program
    
{
        
static void Main(string[] args)
        
{
            VehicalTestFramework.DoTest(
new HongqiCar());
            Console.ReadLine();
        }

    }

}

Template Method模式的几个要点
     • Template Method模式是一种非常基础性的设计模式,在面向对象系统中有着大量的应用。它用最简洁的机制(虚函数的多态性)为很多应用程序框架提供了灵活的扩展点,是代码复用方面的基本实现结构
     • 除了可以灵活应对子步骤的变化外,“不要调用我,让我来调用你”的反向控制结构是Template Method的典型应用。
     • 在具体实现方面,被Template Method调用的虚方法可以具有实现,也可以没有任何实现(抽象方法、纯虚方法),但一般推荐将它们设置为protected方法。

     模版方法模式实际上是所有模式中最为常见的几个模式之一,而且很多人可能使用过模版方法模式而没有意识到自己已经使用了这个模式。模版方法模式是基于继承的代码复用的基本技术,模版方法模式的结构和用法也是面向对象设计的核心