共用方式為


GreetingsBuilder

Well.. just read / execute this.. What more do I need to say?!

By the way.. Sorry for the missing comments :-)

 using System;
using System.Globalization;

namespace GreetingsBuilder
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Max says..");
                Console.WriteLine("\t" + 
GreetingsFactory.Create().GetGreeting());
            }
            catch (NoGreetingAvailableException e)
            {
                Console.WriteLine("\t" + e.Message);
            }
        }
    }

    public class GreetingsFactory
    {
        public static GreetingBase Create()
        {
            if (DateTime.Now.Date < DateTime.Parse("28.12.06",
                CultureInfo.CreateSpecificCulture("de-at")))
            {
                return new ChristmasGreeting();
            }
            else if (DateTime.Now.Date < DateTime.Parse("04.01.07",
                CultureInfo.CreateSpecificCulture("de-at")))
            {
                return new NewYearsGreeting();
            }
            else
            {
                throw new NoGreetingAvailableException();
            }
        }
    }

    public abstract class GreetingBase
    {
        public abstract string GetGreeting();
    }

    public class ChristmasGreeting : GreetingBase
    {
        public override string GetGreeting()
        {
            return "\"Merry Christmas !\"";
        }
    }

    public class NewYearsGreeting : GreetingBase
    {
        public override string GetGreeting()
        {
            return "\"A happy new year!\"";
        }
    }

    public class NoGreetingAvailableException : Exception
    {
        public NoGreetingAvailableException()
            : base(
            "\"Sorry! You've got to wait until next year!\"")
        {
        }
    }
}