Head First Design study notes-Template Method Pattern
|
Complete a short survey and get free Brand Name samples from QualityHealth.com! |
One to ‘style’ of ‘style’
Template method (Template Method), the name contains a double meaning:
1. What do the model (template): It defines an algorithm skeleton
2. How to do: Method, in a method
In short, the Template Method Pattern defined algorithm framework is key to the method. The general structure is as follows:
Of course, not all steps are abstract, in the superclass where some of the steps can also provide a default implementation.
There is also a template method in the concept hook: some steps are not Required, subclasses can control whether or not the steps.
protected bool NeedStep1 = true;
…
public void TemplateMethod ()
{
if (NeedStep1)
{
Step1 ();
}
Step2 ();
}
…
Second, the ‘style’ that use:
Workflow process involving, in the submission of the various processes from one step to the next step may occur when the following situations:
1. Remove the draft data
2. Save the business data
3. To promote the process
4. Prompts Results
This is the template pattern of a typical application scenarios. First, the definition of operational procedures: public abstract class ProcessBase
{
protected bool NeedPrompt = false;
public void Submit ()
{
/ / 1. Remove the draft data
DeleteDraft ();
/ / 2. Save the business data
SaveBizData ();
/ / 3. To promote the process
SubmitProcess ();
/ / 4. Prompts Results
if (NeedPrompt)
{
Prompt ();
}
}
public void DeleteDraft ()
{
Console.WriteLine (“Deleting Draft …”);
}
public abstract void SaveBizData ();
public abstract void SubmitProcess ();
public void Prompt ()
{
Console.WriteLine (“Success!”);
}
}
With such a base class that contains the template method, the implementation process of the submission of each action is simple:
public class Process1: ProcessBase
{
public Process1 (): base ()
{
NeedPrompt = true;
}
public override void SaveBizData ()
{
Console.WriteLine (“Saving Biz data …”);
}
public override void SubmitProcess ()
{
Console.WriteLine (“Submitting process …”);
}
}
Third, watching the film
More than a little learning experience for individuals and records, please advise.
Sorry, the comment form is closed at this time.


Comments
No comments yet.