博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
抽象工厂模式
阅读量:5040 次
发布时间:2019-06-12

本文共 3876 字,大约阅读时间需要 12 分钟。

 

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();            }        }    }}

运行结果:

 

转载于:https://www.cnblogs.com/CHUR/p/4936775.html

你可能感兴趣的文章
输入名字显示其生日,没有则让输入生日,做记录
查看>>
爬虫综合大作业
查看>>
Kubernetes 运维学习笔记
查看>>
并查集 经典 畅通工程
查看>>
Spark MLlib 之 Naive Bayes
查看>>
php修改SESSION的有效生存时间
查看>>
spring security 11种过滤器介绍
查看>>
Hibernate一对多、多对一关联
查看>>
一、记录Git使用中遇到的问题及解决方法
查看>>
学习网址
查看>>
前端表格插件datatables
查看>>
内部类
查看>>
树链剖分入门
查看>>
图解算法时间复杂度
查看>>
UI_搭建MVC
查看>>
一个样例看清楚JQuery子元素选择器children()和find()的差别
查看>>
代码实现导航栏分割线
查看>>
Windows Phone开发(7):当好总舵主 转:http://blog.csdn.net/tcjiaan/article/details/7281421...
查看>>
ASP.Net页面和控件缓存设置
查看>>
SEO第一步做什么
查看>>