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实现比较麻烦。每次添加一个新的具体产品都需要去修改。


  • 相关阅读:
    接口测试再思考
    Python开发简单爬虫
    正则表达式(Python)
    Git常用方法
    CNN--卷积神经网络从R-CNN到Faster R-CNN的理解(CIFAR10分类代码)
    一看就懂的K近邻算法(KNN),K-D树,并实现手写数字识别!
    我是这样一步步理解--主题模型(Topic Model)、LDA(案例代码)
    你想知道的特征工程,机器学习优化方法都在这了!收藏!
    从似然函数到EM算法(附代码实现)
    一次性弄懂马尔可夫模型、隐马尔可夫模型、马尔可夫网络和条件随机场!(词性标注代码实现)
  • 原文地址:https://www.cnblogs.com/xoray007/p/2325342.html
Copyright © 2011-2022 走看看