zoukankan      html  css  js  c++  java
  • 工厂方法 学习手记

    概述

          简单工厂存在着不少的缺点,工厂类就违反了“对扩展开放、对修改关闭”的原则。而工厂方法可以克服简单工厂的缺点。

    工厂方法通过定义一个用户创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。 

     

     实现例子:

     产品接口:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        internal interface FemaleClothes
        {
             String Name { getset; }

             String Code { getset; }

             void Display();
            
        }

     }

    具体产品类A:

     using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        internal class FemaleShirtClothes : FemaleClothes
        {
            private String name="衬衫";

            public String Name
            {
                get { return name="衬衫"; }
                set { name="衬衫"; }
            }

            private String code="ChenShan";

            public String Code
            {
                get { return code="ChenShan"; }
                set { code="ChenShan"; }
            }
            

            public void Display()
            {
                Console.WriteLine("商品名称是:"+Name+" 拼音是:"+Code);
            }
        }
    }

     具体产品类B:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        internal class FemaleTshirtsClothes : FemaleClothes
        {
            private String name="T恤";

            public String Name
            {
                get { return name="T恤"; }
                set { name="T恤"; }
            }

            private String  code="T-Shirts";

            public String Code     
            {
                get { return code ="T-Shirts"; }
                set { code = "T-Shirts"; }
            }

            public void Display()
            {
                Console.WriteLine("商品名称是:" + Name + " 拼音是:" + Code);
            }
            
        }
     }

    抽象工厂:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        internal interface FemaleClothesFactory
        {
            FemaleClothes GetInstance();
        }

    }

     具体工厂A:

     using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        internal class FemaleShirtClothesFactory : FemaleClothesFactory
        {
            FemaleShirtClothes clothes;

            public FemaleClothes GetInstance()
            {
                clothes= new FemaleShirtClothes();
                return clothes;
            }
        }
    }

    具体工厂B:

     using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        internal class FemaleTshirtsClothesFactory:FemaleClothesFactory
        {
            FemaleTshirtsClothes clothes;

            public FemaleClothes GetInstance()
            {
                clothes = new FemaleTshirtsClothes();
                return clothes;
            }
        }
    }

     客户端调用:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleFactoryMethodDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                FemaleClothesFactory factory = new FemaleShirtClothesFactory();

                FemaleClothes clothes = factory.GetInstance();

                clothes.Display();

                Console.ReadLine();

            }
        }

     }


     工厂方法,克服了简单工厂的“开放-封闭”原则,同时将具体对象的创建工作延迟到子类,更具体扩展能力,较好的解决了这种紧耦合的关系。

  • 相关阅读:
    xfs(dm-3):please umount the filesystem and rectify the problem(s)
    解决 pcre-8.35 make[2]: *** [aclocal.m4] Error 127
    redis-4.0.6 编译安装
    nginx出错:rewrite or internal redirection cycle
    17个技巧
    go-fastdfs/go-fastdfs-web
    Win X86 时间同步
    关闭或启动linux防火墙后,docker启动容器报错问题解决方式
    Nginx 重定向 443
    Docker 容器和宿主机相互拷贝文件
  • 原文地址:https://www.cnblogs.com/xoray007/p/2325678.html
Copyright © 2011-2022 走看看