zoukankan      html  css  js  c++  java
  • 4.抽象工厂模式(Abstract Factory)

    using System;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                //这样我湖南工厂换广州工厂改抽象工厂类的实例对象就可以了,对产品鸭架,鸭脖没影响
                //AbstractFactory nanChangFactory = new GuanzhouFactory();
                AbstractFactory nanChangFactory = new HuNanFactory();
                YaBo _yabo = nanChangFactory.CreateYaBo();
                YaJia _yajia = nanChangFactory.CreateYaJia();
                _yabo.Make();
                _yajia.Make();
                Console.ReadLine();  
            }
        }
    
        /// <summary>    
        /// 抽象工厂类,提供创建两个不同地方的鸭架和鸭脖的接口    
        /// </summary>    
        public abstract class AbstractFactory    
        {        
            // 抽象工厂提供创建一系列产品的接口,这里作为例子,只给出了绝味中鸭脖和鸭架的创建接口        
            public abstract YaBo CreateYaBo();        
            public abstract YaJia CreateYaJia();    
        }
    
        /// <summary>
        /// 广州绝味工厂负责制作广州的鸭脖和鸭架
        /// </summary>
        public class GuanzhouFactory : AbstractFactory
        {
            public override YaBo CreateYaBo()
            {
                return new GuanzhouYaBo();
            }
    
            public override YaJia CreateYaJia()
            {
                return new GuanzhouYaJia();
            }
        }
    
        /// <summary>
        /// 湖南绝味工厂负责制作湖南的鸭脖和鸭架
        /// </summary>
        public class HuNanFactory : AbstractFactory
        {
            public override YaBo CreateYaBo()
            {
                return new HuNanYaBo();
            }
    
            public override YaJia CreateYaJia()
            {
                return new HuNanYaJia();
            }
        }
    
        /// <summary>
        /// 鸭架抽象类,供每个地方的鸭架类继承
        /// </summary>
        public abstract class YaBo
        {
            public abstract void Make();
        }
    
        /// <summary>
        /// 鸭架抽象类,供每个地方的鸭架类继承
        /// </summary>
        public abstract class YaJia
        {
            public abstract void Make();
        }
    
        /// <summary>
        /// 广州的鸭脖类,一般辣
        /// </summary>
        public class GuanzhouYaBo : YaBo
        {
            public override void Make()
            {
                Console.WriteLine("广州的鸭脖");
            }
        }
    
        /// <summary>
        /// 湖南的鸭脖类,比较辣
        /// </summary>
        public class HuNanYaBo : YaBo
        {
            public override void Make()
            {
                Console.WriteLine("湖南的鸭脖");
            }
        }
    
        /// <summary>
        /// 广州的鸭架类,一般辣
        /// </summary>
        public class GuanzhouYaJia : YaJia
        {
            public override void Make()
            {
                Console.WriteLine("广州的鸭架");
            }
        }
    
        /// <summary>
        /// 湖南的鸭架类,比较辣
        /// </summary>
        public class HuNanYaJia : YaJia
        {
            public override void Make()
            {
                Console.WriteLine("湖南的鸭架");
            }
        }
    }
  • 相关阅读:
    JS定时执行,循环执行
    Ecshop(二次开发)
    百度歌曲接口
    给大家讲讲在哪些地方发外链最好
    360浏览器默认以兼容模式或急速模式方式打开页面
    子iframe 怎么调用 父级的JS函数
    ASP 发送邮件
    PHP发送邮件
    php表单数据验证类
    js获取url传递参数
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4629025.html
Copyright © 2011-2022 走看看