Core Design Patterns(15) Template Method 模版方法模式
VS 2008
父类定义了一个模版方法,这个模版方法规定了一个算法几个步骤的执行顺序,它的子类可以更改这个算法某几个步骤的具体实现。
1. 模式UML图
2. 应用
由于业务需要,应用程序的逻辑层需要同时提供支持Web应用程序用户登录和Win Form程序用户登录。一个完成的用户登录过程为:检验用户名密码、保存用户信息、跳转页面。对于WebForm和WinForm的用户登录来说,检验用户名和密码的逻辑是一样的,而他们各自有自己的保存用户信息,和跳转页面(切换Form)逻辑
SignInLogic.cs
WebSignIn.cs
WinSignIn.cs
Client
Output
父类定义了一个模版方法,这个模版方法规定了一个算法几个步骤的执行顺序,它的子类可以更改这个算法某几个步骤的具体实现。
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();
}
}
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");
}
}
}
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");
}
}
}
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");
}
}
}
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