zoukankan      html  css  js  c++  java
  • 简单工厂 学习手记

    概述:    

           简单工厂,通过具体工厂类、抽象产品、具体产品类设计,封装具体产品。

    实现如下 

     

    具体代码实现:

    接口(产品):

        internal interface Clothes
        {
             String Name { getset; }

             String Code { getset; }

             void Display();
            

        }

    具体产品A:

     using System;

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

    namespace ConsoleSimpleFactoryDemo
    {
        internal class FemaleShirtClothes : Clothes
        {
            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 ConsoleSimpleFactoryDemo
    {
        internal class FemaleTshirtsClothes : Clothes
        {
            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 ConsoleSimpleFactoryDemo
    {
         internal class FemaleClothesFactory
        {
             public static Clothes GetInstance(String clothes)
             {
                 switch (clothes.Trim().ToLower())
                 {
                     case "shirt":
                         return new FemaleShirtClothes();
                     case "tshirts":
                         return new FemaleTshirtsClothes();
                     default:
                         throw new Exception("没有你要的衣服");
                 }
             }
        }

    }

     客户端调用:

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

    namespace ConsoleSimpleFactoryDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Clothes clothes;

                clothes=FemaleClothesFactory.GetInstance("Shirt");

                clothes.Display();

                Console.ReadLine();
            }
        }

    }

    简单工厂,分离了具体的类,通过控制具体工厂来实现对产品的控制。不过有个缺点就是简单工厂的switch实现比较麻烦。每次添加一个新的具体产品都需要去修改。


  • 相关阅读:
    推介一款小工具——SwitchHosts
    Postman的使用之进行文件上传
    Postman的使用之普通的提交Post和Get请求
    Postman的安装
    Windows配置基础环境(jdk+tomcat)
    yum clean all大坑解决
    通过代理实现访问内网系统
    批量配置免密登录
    设置JRebel热部署【本地模式】
    使用多线程程序模拟实现单生产者/多消费者问题 (Linux 线程锁实践)
  • 原文地址:https://www.cnblogs.com/xoray007/p/2325342.html
Copyright © 2011-2022 走看看