• Factory Pattern简单随笔


           工厂方法模式解决了许多简单工厂模式的问题。首先完全实现‘开-闭 原则’,实现了可扩展。其次更复杂的层次结构,可以应用于产品结果复杂的场合。

         工厂方法模式的对简单工厂模式进行了抽象。有一个抽象的Factory类(可以是抽象类和接口),这个类将不在负责具体的产品生产,而是只制定一些规范,具体的生产工作由其子类去完成。在这个模式中,工厂类和产品类往往可以依次对应。即一个抽象工厂对应一个抽象产品,一个具体工厂对应一个具体产品,这个具体的工厂就负责生产对应的产品。

           在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口。

           提供一种封装机制来隔离出“这个易变对象”的变化,从而保持系统中“其它依赖该对象的对象”不随着需求的改变而改变

    visio图解为:

     

    类关系图为:

     

    首先构建工厂类:

     

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace FactoryPatternSam
     7 {
     8     public abstract class Factory
     9     {
    10         public abstract Product  CreateInstance(); 
    11     }
    12 }
    13 
    14 
    15 
    16 using System;
    17 using System.Collections.Generic;
    18 using System.Linq;
    19 using System.Text;
    20 
    21 namespace FactoryPatternSam
    22 {
    23     public class FactoryEntityOne:Factory 
    24     {
    25         public override Product  CreateInstance()
    26         {
    27             //throw new NotImplementedException();
    28             return new ProductEntityOne();
    29         }
    30     }
    31 }
    32 
    33 using System;
    34 using System.Collections.Generic;
    35 using System.Linq;
    36 using System.Text;
    37 
    38 namespace FactoryPatternSam
    39 {
    40     public class FactoryEntityTwo:Factory 
    41     {
    42         public override Product CreateInstance()
    43         {
    44             //throw new NotImplementedException();
    45             return new ProductEntityTwo();
    46         }
    47     }
    48 }
    49 
    50 using System;
    51 using System.Collections.Generic;
    52 using System.Linq;
    53 using System.Text;
    54 
    55 namespace FactoryPatternSam
    56 {
    57     public class FactoryEntityThree : Factory
    58     {
    59         public override Product CreateInstance()
    60         {
    61             //throw new NotImplementedException();
    62             return new ProductEntityThree();
    63         }
    64     }
    65 }

    建造产品类:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace FactoryPatternSam
     7 {
     8     public abstract class Product
     9     {
    10         public abstract void CreateProduct();
    11     }
    12 }
    13 
    14 using System;
    15 using System.Collections.Generic;
    16 using System.Linq;
    17 using System.Text;
    18 
    19 namespace FactoryPatternSam
    20 {
    21     public class ProductEntityOne:Product 
    22     {
    23         public override void CreateProduct()
    24         {
    25            // throw new NotImplementedException();
    26             Console.WriteLine("*********************");
    27             Console.WriteLine("Create Product one!!!");
    28             Console.WriteLine("---------------------");
    29         }
    30     }
    31 }
    32 
    33 using System;
    34 using System.Collections.Generic;
    35 using System.Linq;
    36 using System.Text;
    37 
    38 namespace FactoryPatternSam
    39 {
    40     public class ProductEntityThree : Product
    41     {
    42         public override void CreateProduct()
    43         {
    44             //throw new NotImplementedException();
    45             Console.WriteLine("*********************");
    46             Console.WriteLine("Create Product THREE!!!");
    47             Console.WriteLine("---------------------");
    48         }
    49     }
    50 }
    51 
    52 using System;
    53 using System.Collections.Generic;
    54 using System.Linq;
    55 using System.Text;
    56 
    57 namespace FactoryPatternSam
    58 {
    59     public class ProductEntityTwo:Product 
    60     {
    61         public override void CreateProduct()
    62         {
    63             //throw new NotImplementedException();
    64             Console.WriteLine("*********************");
    65             Console.WriteLine("Create Product Two!!!");
    66             Console.WriteLine("---------------------");
    67         }
    68     }
    69 }
    70 

     构建常量和其他操作类: 获取相应的产品类--->

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace FactoryPatternSam
     7 {
     8     public static class ConfigureInstance
     9     { 
    10         private static string configStr = ConfigureSet.ConfigureSetString;
    11         private static Factory _Factory=(Factory)System.Reflection.Assembly.Load("FactoryPatternSam").CreateInstance("FactoryPatternSam." + configStr);
    12  
    13         public static Factory Instance
    14         {
    15             get
    16             {
    17                 return _Factory;
    18             }
    19         }
    20     }
    21 }
    22 
    23 
    24 using System;
    25 using System.Collections.Generic;
    26 using System.Linq;
    27 using System.Text;
    28 using System.Configuration;
    29 
    30 namespace FactoryPatternSam
    31 {
    32     public static class ConfigureSet
    33     {
    34         private static  string _ConfigureSetString = System.Configuration.ConfigurationManager.ConnectionStrings["ProductType"].ToString();
    35         
    36         public static string ConfigureSetString
    37         {
    38             get {
    39                 return _ConfigureSetString;
    40             }
    41         }
    42     }
    43 }
    44 

     配置文件类 :设置相应的类型:

    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <configuration>
    3   <connectionStrings>
    4     <add name="ProductType" connectionString="FactoryEntityOne"/>
    5   </connectionStrings>
    6 </configuration>

    客户端代码如下:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 
     6 namespace FactoryPatternSam
     7 {
     8     class Program
     9     {
    10         static void Main(string[] args)
    11         {
    12             Factory factory = ConfigureInstance.Instance;
    13 
    14             Product pro = factory.CreateInstance();
    15             pro.CreateProduct();
    16             Console.ReadLine();
    17         }
    18     }
    19 }
    20 

    ******************************************************************************************************

    工厂模式源代码下载:/Files/jasenkin/FactoryPatternSam.rar

  • 相关阅读:
    怎么获取数组中的对象的某一项之和
    原型链
    js的事件循环(Eventloop) 机制/js的宏任务微任务执行顺序
    怎么替换数组中对象的属性
    求对象所有值的和
    sequelize中duplicating:false的使用
    WebSocket
    轮播

    ssl tsl
  • 原文地址:https://www.cnblogs.com/jasenkin/p/1684472.html
Copyright © 2020-2023  润新知