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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Cameo

解决VSTO项目重定向到.NET Framework 4的编译和运行时错误 Excel 应用程序如何创建数据透视表 - Cameo 值类型与引用类型 Visual studio tools for office体系结构 - Cameo C#类型转换 VSTO开发团队成立 15. Command 命令(行为型模式) 13. Proxy代理(结构型模式) 12. Flyweight享元(结构型模式) - Cameo 11. Facade外观[门面模式](结构型模式) - Cameo 10. Decorator 装饰(结构型模式) 9. Composite 组合(结构型模式) 8. Bridge 桥接(结构型模式) - Cameo 7. Adapter 适配器(结构型模式) 6. Prototype 原型(创建型模式) 5. Factory Method 工厂方法(创建型模式的基础) - Cameo 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方法。

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