In book Agile Software Development, Principles, Patterns, and Practices there is below example mentioned which shows how template pattern can be abused
public abstract class Application
{
private bool isDone = false;
protected abstract void Init();
protected abstract void Idle();
protected abstract void Cleanup();
protected void SetDone()
{
isDone = true;
}
protected bool Done()
{
return isDone;
}
public void Run()
{
Init();
while (!Done())
Idle();
Cleanup();
}
}
public class FtoCTemplateMethod : Application
{
private TextReader input;
private TextWriter output;
public static void Main(string[] args)
{
new FtoCTemplateMethod().Run();
}
protected override void Init()
{
input = Console.In;
output = Console.Out;
}
protected override void Idle()
{
string fahrString = input.ReadLine();
if (fahrString == null || fahrString.Length == 0)
SetDone();
else
{
double fahr = Double.Parse(fahrString);
double celcius = 5.0/9.0*(fahr - 32);
output.WriteLine("F={0}, C={1}", fahr, celcius);
}
}
protected override void Cleanup()
{
output.WriteLine("ftoc exit");
}
}
Can someone please explain what is wrong with this example . why does author say that Using TEMPLATE METHOD for this particular application is ridiculous. It complicates the program and makes it bigger
Aucun commentaire:
Enregistrer un commentaire