1. 程式人生 > >C#設計模式-模板模式

C#設計模式-模板模式

using System;
using System.Collections.Generic;

namespace TestCS
{
    class Program
    {
        // template pattern
        static void Main(string[] args)
        {
            Vehicle vehicle0 = new Maserati();
            vehicle0.Launch();
            vehicle0.Brake();

            Vehicle vehicle1 = new BMW();
            vehicle1.Launch(); 
            vehicle1.Brake(); 

            Console.ReadKey();
        }

        // template
        abstract class Vehicle
        {
            public abstract void Launch();
            public abstract void Brake();
        }

        class Maserati : Vehicle
        {
            public override void Brake()
            {
                Console.WriteLine("Maserati Brake");
            }

            public override void Launch()
            {
                Console.WriteLine("Maserati Launch");
            }
        }

        class BMW : Vehicle
        {
            public override void Brake()
            {
                Console.WriteLine("BMW Brake");
            }

            public override void Launch()
            {
                Console.WriteLine("BMW Launch");
            }
        }
    }
}