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

推荐订阅源

N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
B
Blog
J
Java Code Geeks
T
Tailwind CSS Blog
腾讯CDC
A
About on SuperTechFans
GbyAI
GbyAI
H
Help Net Security
IT之家
IT之家
L
LangChain Blog
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
博客园 - 叶小钗
小众软件
小众软件
I
InfoQ
爱范儿
爱范儿
有赞技术团队
有赞技术团队
博客园 - 司徒正美
博客园 - 【当耐特】
Jina AI
Jina AI
D
Docker

博客园 - 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