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

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

  • 相关阅读:
    Linux Hung Task分析
    Linux内存都去哪了:(1)分析memblock在启动过程中对内存的影响
    《Linux/UNIX系统编程手册》第63章 IO多路复用、信号驱动IO以及epoll
    Linux内核和用户空间通信之netlink
    Linux soft lockup分析
    一款DMA性能优化记录:异步传输和指定实时信号做async IO
    Linux下时钟框架实践---一款芯片的时钟树配置
    使用Kernel NetEm和tc模拟复杂网络环境
    使用Flame Graph进行系统性能分析
    sigsuspend()阻塞:异步信号SIGIO为什么会被截胡?
  • 原文地址:https://www.cnblogs.com/xoray007/p/2325860.html
Copyright © 2011-2022 走看看