The template method pattern

The template method pattern basically defines the steps of an algorithm inside a class as a contract while deferring some of the steps (methods) to be implemented by the derived classes; hence, it primarily defines the structure via an abstract base class.

We will explain its implementation using an example in continuation of our sample for a GUI app we created earlier for the abstract factory pattern. In fact, within the same example app, we cover abstract factory, builder, and template method patterns.

In the abstract factory example, we presented the IUIAbsFactory interface and mentioned two of the possible implementations as DarkUIAbsFactory and LightUIAbsFactory concrete factory classes. Since the difference in our supposed implementation is basically only the color factor between the two themes, DarkUIAbsFactory and LightUIAbsFactory, we do not need to fully implement all the methods of IUIAbsFactory. Therefore, what we will do is create a partial implementation as an abstract class and leave the specific parts of the code algorithm which deals with the coloring part to the further derived classes; that is, we apply the template method pattern.

For our example code, this ThemeableUIAbsFactory abstract base class looks like the following:

    public abstract class ThemeableUIAbsFactory : IUIAbsFactory 
{
protected IServiceProvider _provider;
private IMenu menu;

public ThemeableUIAbsFactory(IServiceProvider provider)
{
_provider = provider;
}

#region IUIAbsFactory abstract factory interface
public IStatusBar CreateStatusBar()
{
var statusBar = new StatusBar();
//StatusBar creation Preprocessing..
ApplyThemeOnStatusBar(statusBar);
//StatusBar creation Post-processing..
return statusBar;
}

/// <summary>
/// Internal code demonstrates the use of Builder pattern
/// </summary>
/// <returns></returns>
public IWizard CreateWizard()
{
//Director code for builder pattern
var wizardBuilder = _provider.
GetRequiredService<IWizardBuilder>();

wizardBuilder.CreateWizardSteps(4);
wizardBuilder.AddFrontScreen();
wizardBuilder.AddFinalScreen();

var wizard = wizardBuilder.GetResult();

ApplyThemeOnWizard(wizard);

return wizard;
}

public IMenu GetMenu()
{
//sort of Singleton resource behaviour
if (menu != null) return menu; //only one menu
resource will be created

menu = new Menu();
//Menu creation Preprocessing..
ApplyThemeOnMenu(menu);
//Menu creation Post-processing..
return menu;
}
#endregion

#region Template Methods
public abstract void ApplyThemeOnStatusBar(IStatusBar
statusBar);

public abstract void ApplyThemeOnWizard(IWizard wizard);

public abstract void ApplyThemeOnMenu(IMenu menu);
#endregion
}

ThemeableUIAbsFactory is an abstract base class that implements IUIAbsFactory. It implements almost all of the interface methods and carefully delegates the responsibility of filling the gaps, that is, applying themes of colors to the derived classes. It has provided a template to be followed by the derived classes, in our case, DarkUIAbsFactory and LightUIAbsFactory, so let's look at the code for one of them; the other is similar:

    public class DarkUIAbsFactory : ThemeableUIAbsFactory 
{
public DarkUIAbsFactory(IServiceProvider provider) :
base(provider)
{
}

public override void ApplyThemeOnMenu(IMenu menu)
{
//specific implementation
}

public override void ApplyThemeOnStatusBar(IStatusBar
statusBar)
{
//specific implementation
}

public override void ApplyThemeOnWizard(IWizard wizard)
{
//specific implementation
}
}

From the implementation of DarkUIAbsFactory, you can see that it only has to fill the template of the abstract methods and does not have to worry about implementing the complete IUIAbsFactory interface again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.211.166