类图:
代码如下:
Code
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace TemplatePattern
{
public abstract class Student
{
public string studentType;
public virtual void Study()
{
Console.WriteLine(string.Format("{0} is studying,especially ENGLISH.", studentType));
}
public virtual void Eat()
{
Console.WriteLine("Dinner time.");
}
public abstract void Sleep();
/*模板方法 在基类里定义,而且不能够被派生类更改。有时候它是私有方法(private method),但实际上它经常被声明为protected。
* 它通过调用其它的基类方法(覆写过的)来工作,但它经常是作为初始化过程的一部分被调用的,这样就没必要让客户端程序员能够直接调用它了。
*/
/// <summary>
/// 模板方法,学生学习,吃饭,休息 (Template Method模式的一个特征就是剥离共同点)
/// </summary>
public void BeginStudentLife()
{
Study();
Eat();
Sleep();
}
}
public class HighSchoolStudent : Student
{
public HighSchoolStudent(string studentType)
{
base.studentType = studentType;
}
public override void Study()
{
base.Study();
Console.WriteLine(base.studentType + " are studying for colledge.");
}
public override void Sleep()
{
Console.WriteLine(base.studentType + " want to play game,but they have to go to bed.");
}
}
public class UniversityStudent : Student
{
public UniversityStudent(string studentType)
{
base.studentType = studentType;
}
public override void Study()
{
base.Study();
Console.WriteLine(base.studentType + " are studying for job.");
}
public override void Sleep()
{
Console.WriteLine(base.studentType + " want to play game,and they are playing the game day and night.");
}
}
public class Program
{
static void Main(string[] args)
{
Student student;
student = new HighSchoolStudent("High school student ");
student.BeginStudentLife();
student = new UniversityStudent("University student ");
student.BeginStudentLife();
Console.Read();
}
}
}
总结:using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace TemplatePattern
{
public abstract class Student
{
public string studentType;
public virtual void Study()
{
Console.WriteLine(string.Format("{0} is studying,especially ENGLISH.", studentType));
}
public virtual void Eat()
{
Console.WriteLine("Dinner time.");
}
public abstract void Sleep();
/*模板方法 在基类里定义,而且不能够被派生类更改。有时候它是私有方法(private method),但实际上它经常被声明为protected。
* 它通过调用其它的基类方法(覆写过的)来工作,但它经常是作为初始化过程的一部分被调用的,这样就没必要让客户端程序员能够直接调用它了。
*/
/// <summary>
/// 模板方法,学生学习,吃饭,休息 (Template Method模式的一个特征就是剥离共同点)
/// </summary>
public void BeginStudentLife()
{
Study();
Eat();
Sleep();
}
}
public class HighSchoolStudent : Student
{
public HighSchoolStudent(string studentType)
{
base.studentType = studentType;
}
public override void Study()
{
base.Study();
Console.WriteLine(base.studentType + " are studying for colledge.");
}
public override void Sleep()
{
Console.WriteLine(base.studentType + " want to play game,but they have to go to bed.");
}
}
public class UniversityStudent : Student
{
public UniversityStudent(string studentType)
{
base.studentType = studentType;
}
public override void Study()
{
base.Study();
Console.WriteLine(base.studentType + " are studying for job.");
}
public override void Sleep()
{
Console.WriteLine(base.studentType + " want to play game,and they are playing the game day and night.");
}
}
public class Program
{
static void Main(string[] args)
{
Student student;
student = new HighSchoolStudent("High school student ");
student.BeginStudentLife();
student = new UniversityStudent("University student ");
student.BeginStudentLife();
Console.Read();
}
}
}
1.模板方法用最简洁的机制(虚函数的多态性)为很多应用程序框架提供了灵活的扩展点,是代码复用方面的基本实现结构。
2.除了可以灵活应对子步骤的变化外,“不用调用我,让我来调用你”的反向控制结构是模板方法的典型应用。