• 抽象工厂


    简单工厂:抽象产品,工厂没有抽象出来。

    工厂方法:抽象产品,抽象工厂。

    抽象工厂:抽象多种产品,抽象工厂。

    代码1:工厂抽象部分

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Configuration;
    
    namespace AbstractFactory
    {
        /// <summary>
        /// 抽象工厂
        /// </summary>
        public abstract class AbstractClothesFactory
        {
            //抽象方法:创建一个上衣
            abstract public AbstractCoat CreateCoat();
            //抽象方法:创建一个裤子
            abstract public AbstractTrousers CreateTrousers();
        }
    
        /// <summary>
        ///时尚男装 
        /// </summary>
        public class FashionManClothesFactory : AbstractClothesFactory
        {
            public override AbstractCoat CreateCoat()
            {
                return new CoatMan();
            }
    
            public override AbstractTrousers CreateTrousers()
            {
                return new TrousersMan();
            }
        }
    
        /// <summary>
        /// 时尚女装
        /// </summary>
        public class FashionWomanClothesFactory : AbstractClothesFactory
        {
            public override AbstractCoat CreateCoat()
            {
                return new CoatWomen();
                //throw new Exception("The method or operation is not implemented.");
            }
            public override AbstractTrousers CreateTrousers()
            {
                return new TrouserWomen();
                //throw new Exception("The method or operation is not implemented.");
            }
        }
    }
    

    代码2:抽象产品部分

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;
    using System.Configuration;
    
    namespace AbstractFactory
    {
    
        /// <summary>
        /// 抽象产品----上衣抽象类
        /// </summary>
        public abstract class AbstractCoat
        {
            //性别属性
            public abstract bool Sex
            {
                get;
            }
    
            //样式属性
            public abstract string Style
            {
                get;
            }
        }
    
        /// <summary>
        /// 时尚男性上衣
        /// </summary>
        public class CoatMan : AbstractCoat
        {
            private bool sex = true;
            private string style = "时尚";
            /// <summary>
            /// 重写基类的Sex属性
            /// </summary>
            public override bool Sex
            {
                get
                {
                    return sex;
                }
            }
    
            /// <summary>
            /// 重写基类的Style属性
            /// </summary>
            public override string Style
            {
                get
                {
                    return style;
                }
            }
        }
    
        /// <summary>
        /// 时尚男性上衣
        /// </summary>
        public class CoatWomen : AbstractCoat
        {
            private bool sex = false;
            private string style = "时尚";
            /// <summary>
            /// 重写基类的Sex属性
            /// </summary>
            public override bool Sex
            {
                get
                {
                    return sex;
                }
            }
    
            /// <summary>
            /// 重写基类的Style属性
            /// </summary>
            public override string Style
            {
                get
                {
                    return style;
                }
            }
        }
    
        /// <summary>
        /// 抽象产品----裤子抽象类
        /// </summary>
        public abstract class AbstractTrousers
        {
            //性别属性
            public abstract bool Sex
            {
                get;
            }
    
            //样式属性
            public abstract string Style
            {
                get;
            }
        }
    
        /// <summary>
        /// 时尚男性裤子
        /// </summary>
        public class TrousersMan : AbstractTrousers
        {
            private bool sex = true;
            private string style = "时尚";
            public override bool Sex
            {
                get
                {
                    return sex;
                }
            }
            public override string Style
            {
                get
                {
                    return style;
                }
            }
        }
    
        /// <summary>
        /// 时尚男性裤子
        /// </summary>
        public class TrouserWomen : AbstractTrousers
        {
            private bool sex = false;
            private string style = "时尚";
            public override bool Sex
            {
                get
                {
                    return sex;
                }
            }
            public override string Style
            {
                get
                {
                    return style;
                }
            }
        }
    }
    

    代码3:对工厂进行包装

     /// <summary>
        /// 创建衣服类
        /// </summary>
        public class CreateClothes
        {
            private AbstractCoat myCoat;
            private AbstractTrousers myTrousers;
            public CreateClothes(AbstractClothesFactory clothes)
            {
                myCoat = clothes.CreateCoat();
                myTrousers = clothes.CreateTrousers();
            }
    
            public void ShowMyClothes()
            {
                Console.WriteLine("My Clothes:");
                string sex = myCoat.Sex ? "男" : "女";
                //Console.WriteLine("Coat:{0} {1}", myCoat.Sex ? "男" : "女", myCoat.Style);
                Console.WriteLine("Coat:{0} {1}", sex, myCoat.Style);
                sex = myTrousers.Sex ? "男" : "女";
                Console.WriteLine("Trousers:{0} {1}", sex, myTrousers.Style);
            }
        }
    

    代码4:客户端代码

       public class Client
        {
            static void Main1(string[] args)
            {
                //创建一个工厂类的实例   
                string assemblyName = ConfigurationManager.AppSettings["assemblyName"];
                string fullTypeName = string.Concat(ConfigurationManager.AppSettings["nameSpaceName"], ".", ConfigurationManager.AppSettings["typename"]);
    
                AbstractClothesFactory factory = (AbstractClothesFactory)Assembly.Load(assemblyName).CreateInstance(fullTypeName);
                CreateClothes clothes = new CreateClothes(factory);
                clothes.ShowMyClothes();
                Console.Read();
            }
        }
    

      

  • 相关阅读:
    学习使用&运算符
    企业发放的奖金根据利润提成。
    取一个整数a从右端开始的47位。
    jQuery Select操作大集合
    js 获取某年某月的最后一天
    sql 语句区分大小写查询
    js 冒泡排序
    一个初学者的程序自学计划
    JWNL体验
    (转)GIS相关的SCI、EI期刊
  • 原文地址:https://www.cnblogs.com/wuMing-dj/p/3357219.html
Copyright © 2020-2023  润新知