zoukankan      html  css  js  c++  java
  • 抽象工厂 学习手记

    概述:

         提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。 

     模型图:

         

    代码二个抽象接口:


        internal interface FemaleShoes
        {
             String Name { getset; }

             String Style { getset; }

             void Display();
        }



        internal interface FemaleClothes
        {
             String Name { getset; }

             String Code { getset; }

             void Display();
            
        }

     具体实现类:

        internal class FemaleClothShoes:FemaleShoes
        {
            private String name="布鞋";

            public String Name
            {
                get { return name="布鞋"; }
                set { name="布鞋" ; }
            }

            private String style="均码";

            public String Style
            {
                get { return style="均码"; }
                set { style="均码"; }
            }

            public void Display()
            {
                Console.WriteLine("鞋子名称:"+Name+" 款式:"+Style);
            }
            
            
        }


        internal class FemaleBoardShoes : FemaleShoes
        {
            private String name = "女装板鞋";

            public String Name
            {
                get { return name = "女装板鞋"; }
                set { name = "女装板鞋"; }
            }

            private String style = "均码";

            public String Style
            {
                get { return style = "均码"; }
                set { style = "均码"; }
            }

            public void Display()
            {
                Console.WriteLine("鞋子名称:" + Name + " 款式:" + Style);
            }
            
        }

    抽象工厂接口:

        internal interface FemaleStoreFactory
        {
            FemaleClothes CreateClothes();

            FemaleShoes CreateShoes();
        }

     具体工厂类A:

        internal class FemaleStoreOneFactory : FemaleStoreFactory
        {
            public FemaleClothes CreateClothes()
            {
                return new FemaleShirtClothes();
            }

            public FemaleShoes CreateShoes()
            {
                return new FemaleClothShoes();
            }
        }

     具体工厂类B:

        internal class FemaleStoreTwoFactory:FemaleStoreFactory
        {
            public FemaleClothes CreateClothes()
            {
                return new FemaleTshirtsClothes();
            }

            public FemaleShoes CreateShoes()
            {
                return new FemaleBoardShoes();
            }
        }

     实现实例化的类

        internal class CreateStore
        {
            private FemaleShoes shoes;

            private FemaleClothes clothes;
     
            public CreateStore(FemaleStoreFactory store){
                shoes = store.CreateShoes();
                clothes = store.CreateClothes();
            }

            public void ShowDisplay()
            {
                shoes.Display();
                clothes.Display();
            }
        }

     客户端:

        class Program
        {
            static void Main(string[] args)
            {
                CreateStore store = new CreateStore(new FemaleStoreOneFactory());

                store.ShowDisplay();

                Console.ReadLine();
            }
        }

       抽象工厂模式分离了具体的类,同时克服“开放-关闭”原则。抽象工厂模式通过接口实现了产品的分离,由工厂封装创建产品,使得他们不会出现在客户代码中。

  • 相关阅读:
    ORACLE 查看进程数,已执行任务数, 剩余任务数,删除指定任务
    ORACLE 收集统计整个用户数据
    解决Hystrix dashboard Turbine 一直 Loading…… 及其他坑
    利用 Maven 构造 Spring Cloud 微服务架构 模块使用 spring Boot构建
    AES加解密
    JAVA POI XSSFWorkbook导出扩展名为xlsx的Excel,附带weblogic 项目导出Excel文件错误的解决方案
    JAVA 文件的上传下载
    shell启停服务脚本模板
    JAVA 设计模式之 原型模式详解
    JAVA 设计模式之 工厂模式详解
  • 原文地址:https://www.cnblogs.com/xoray007/p/2325860.html
Copyright © 2011-2022 走看看