CSharp · Console
using System;
using System.Collections.Generic;
public interface IViewEngine
{
string Render(string viewName, Dictionary<string, object> context);
}
public class MatchaViewEngine: IViewEngine
{
public string Render(string viewName, Dictionary<string, object> context)
{
return "View Rendered by Matcha";
}
}
public class SharpViewEngine: IViewEngine
{
public string Render(string viewName, Dictionary<string, object> context)
{
return "View Rendered by Sharp";
}
}
public class Controller
{
public void Render(string viewName, Dictionary<string, object> context)
{
var viewEngine = CreateViewEngine();
var html = viewEngine.Render(viewName, context);
Console.WriteLine(html);
}
protected virtual IViewEngine CreateViewEngine()
{
return new MatchaViewEngine();
}
}
public class SharpController: Controller
{
protected override IViewEngine CreateViewEngine()
{
return new SharpViewEngine();
}
}
public class ProductsController: SharpController
{
public void ListProducts()
{
Dictionary<string, object> context = new Dictionary<string, object>();
Render("products.html", context);
}
}
public class Program
{
public static void Main()
{
ProductsController controller = new ProductsController();
controller.ListProducts();
}
}