Observer-Pattern :👈 👉:Curiously-Recurring-Template-Pattern

Template Method Pattern in C#

🧩 Template Method Pattern (Base ViewModel Lifecycle in C#)

The Template Method Pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class but lets subclasses override specific steps without changing the overall structure.

In the context of MVVM (Model-View-ViewModel), this pattern is often used to define a lifecycle for ViewModels: initialization, loading data, binding state, and cleanup. The base ViewModel provides the template, while derived ViewModels customize the steps.

🔑 Key Concepts

  • Abstract Class (BaseViewModel): Defines the template method (e.g., InitializeLifecycle) and provides default implementations.
  • Concrete Class (ChildViewModel): Overrides specific steps (e.g., LoadData, BindState) to provide custom behavior.
  • Template Method: Calls the steps in a fixed order, ensuring consistency across all ViewModels.

📖 Example in C#

using System;

public abstract class BaseViewModel
{
    // Template Method - defines lifecycle
    public void InitializeLifecycle()
    {
        OnCreate();
        LoadData();
        BindState();
        OnDestroy();
    }

    // Steps with default or abstract behavior
    protected virtual void OnCreate()
    {
        Console.WriteLine("BaseViewModel: OnCreate");
    }

    protected abstract void LoadData();

    protected virtual void BindState()
    {
        Console.WriteLine("BaseViewModel: BindState");
    }

    protected virtual void OnDestroy()
    {
        Console.WriteLine("BaseViewModel: OnDestroy");
    }
}

// Concrete ViewModel
public class UserViewModel : BaseViewModel
{
    protected override void LoadData()
    {
        Console.WriteLine("UserViewModel: Loading user data...");
    }

    protected override void BindState()
    {
        Console.WriteLine("UserViewModel: Binding user state to UI...");
    }
}

public class ProductViewModel : BaseViewModel
{
    protected override void LoadData()
    {
        Console.WriteLine("ProductViewModel: Loading product catalog...");
    }

    protected override void BindState()
    {
        Console.WriteLine("ProductViewModel: Binding product state to UI...");
    }
}

// Client
class Program
{
    static void Main(string[] args)
    {
        BaseViewModel userVM = new UserViewModel();
        userVM.InitializeLifecycle();

        Console.WriteLine();

        BaseViewModel productVM = new ProductViewModel();
        productVM.InitializeLifecycle();
    }
}

📝 Output

BaseViewModel: OnCreate
UserViewModel: Loading user data...
UserViewModel: Binding user state to UI...
BaseViewModel: OnDestroy

BaseViewModel: OnCreate
ProductViewModel: Loading product catalog...
ProductViewModel: Binding product state to UI...
BaseViewModel: OnDestroy

⚡ How This Relates to .NET MAUI

for .NET MAUI , this pattern is highly relevant:

  • In MAUI MVVM, BaseViewModel often defines lifecycle hooks (OnAppearing, OnDisappearing, InitializeAsync).
  • Each Page or ShellContent binds to a ViewModel that follows this lifecycle.
  • The Template Method Pattern ensures all ViewModels follow a consistent initialization process, while allowing customization for each screen.

🚀 Real-World Benefits

  • Consistency: All ViewModels follow the same lifecycle steps.
  • Extensibility: New ViewModels can override only what they need.
  • Maintainability: Centralized lifecycle logic in the base class.
  • Reactive Binding: Works seamlessly with INotifyPropertyChanged for UI updates.
Back to Index
Observer-Pattern :👈 👉:Curiously-Recurring-Template-Pattern
*