UML图:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace KFCandM{ class Program { interface IFactory { Burger Makeburger(); Drink Makedrink(); Chicken Makechicken(); } class KFC : IFactory { public Burger Makeburger() { return new Kburger(); } public Drink Makedrink() { return new Kdrink(); } public Chicken Makechicken() { return new Kchicken(); } } class MacDonald : IFactory { public Burger Makeburger() { return new Mburger(); } public Drink Makedrink() { return new Mdrink(); } public Chicken Makechicken() { return new Mchicken(); } } abstract class Burger { public abstract void burger(); } class Kburger : Burger { public override void burger() { Console.WriteLine("KFC's Hamburger is ready..."); } } class Mburger : Burger { public override void burger() { Console.WriteLine("MacDonald's New Orleans Beef Burger is ready..."); } } abstract class Drink { public abstract void drink(); } class Kdrink : Drink { public override void drink() { Console.WriteLine("KFC's coffee is ready..."); } } class Mdrink : Drink { public override void drink() { Console.WriteLine("MacDonald's orange juice is ready..."); } } abstract class Chicken { public abstract void chicken(); } class Kchicken : Chicken { public override void chicken() { Console.WriteLine("KFC's New Orleans chicken wings are ready..."); } } class Mchicken : Chicken { public override void chicken() { Console.WriteLine("MacDonald's spicy chicken wings are ready..."); } } class Food { private Burger _burger; private Drink _drink; private Chicken _chicken; public Burger Burger { get { return _burger; } set { _burger = value; } } public Drink Drink { get { return _drink; } set { _drink = value; } } public Chicken Chicken { get { return _chicken; } set { _chicken = value; } } public void Eatting() { _chicken.chicken(); _burger.burger(); _drink.drink(); Console.WriteLine("The lunch is over"); } private static void Main(string[] args) { IFactory factory1 = new KFC(); IFactory factory2 = new MacDonald(); Food f1 = new Food(); f1.Burger = factory1.Makeburger(); f1.Chicken = factory1.Makechicken(); f1.Drink = factory1.Makedrink(); f1.Eatting(); Food f2 = new Food(); f2.Burger = factory2.Makeburger(); f2.Chicken = factory2.Makechicken(); f2.Drink = factory2.Makedrink(); f2.Eatting(); Console.ReadLine(); } } }}
运行结果: