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

推荐订阅源

博客园_首页
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
J
Java Code Geeks
G
Google Developers Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
罗磊的独立博客
GbyAI
GbyAI
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 老调重弹:插件式框架开发的一个简单应用 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
Core Design Patterns(15) Template Method 模版方法模式
Tristan(GuoZhijian) · 2008-03-23 · via 博客园 - Tristan(GuoZhijian)

VS 2008

父类定义了一个模版方法,这个模版方法规定了一个算法几个步骤的执行顺序,它的子类可以更改这个算法某几个步骤的具体实现。

1. 模式UML图

2. 应用    由于业务需要,应用程序的逻辑层需要同时提供支持Web应用程序用户登录和Win Form程序用户登录。一个完成的用户登录过程为:检验用户名密码、保存用户信息、跳转页面。对于WebForm和WinForm的用户登录来说,检验用户名和密码的逻辑是一样的,而他们各自有自己的保存用户信息,和跳转页面(切换Form)逻辑

SignInLogic.cs

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

namespace DesignPattern.TemplateMethod.BLL {
    
abstract class SignInLogic {

        
protected string m_UserName;
        
protected string m_Password;

        
public void SignIn(string userName, string password) {
            
this.m_UserName = userName;
            
this.m_Password = password;

            
if (this.CheckUser() && this.SaveUserInfo()) {
                
this.Redirect();
            }

        }

        
protected virtual bool CheckUser() {
            Console.WriteLine(
"validate username and password");
            
return true;
        }

        
protected abstract bool SaveUserInfo();
        
protected abstract void Redirect();
    }

}

WebSignIn.cs

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

namespace DesignPattern.TemplateMethod.BLL {
    
class WebSignIn : SignInLogic {

        
protected override bool SaveUserInfo() {
            Console.WriteLine(
"save user information to session");
            
return true;
        }


        
protected override void Redirect() {
            Console.WriteLine(
"redirect to default page");
        }

    }

}

WinSignIn.cs

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

namespace DesignPattern.TemplateMethod.BLL {
    
class WinSignIn : SignInLogic {
        
protected override bool SaveUserInfo() {
            Console.WriteLine(
"save user information to static variables");
            
return true;
        }


        
protected override void Redirect() {
            Console.WriteLine(
"open new form");
        }

    }

}

Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.TemplateMethod.BLL;

namespace DesignPattern.TemplateMethod {
    
class Program {
        
static void Main(string[] args) {

            SignInLogic logic1 
= new WebSignIn();
            logic1.SignIn(
"guozhijian""77707007");

            Console.WriteLine(
string.Empty);

            SignInLogic logic2 
= new WinSignIn();
            logic2.SignIn(
"guozhijian""77707007");

        }

    }

}

Output