只需要配置一下客户端自定义配置文件configrature.xml以及生成抽象对象就可以了AbstractFactoryDll.AbstractFactory.GetInstance();
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式可以向客户端提供一个接口,使用客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。当有多个抽象产品角色时,工厂方法模式已经不能满足要求。
抽象工厂:主要功能是生产抽象产品;
抽象产品:主要功能是提供实体产品访问接口;
实体工厂:主要功能是生产实体产品;
实体产品:主要功能是实现自己的功能;
AbstractFactoryDll.AbstractFactory abstractFactory = AbstractFactoryDll.AbstractFactory.GetInstance();
AbstractFactoryDll.A a = abstractFactory.CreateA();
a.ShowA();
AbstractFactoryDll.B b = abstractFactory.CreateB();
b.ShowB();
抽象工厂(Abstract Factory)设计模式的使用思路概括如下:
提供一系列相互依赖对象的创建;
封装对象的常规创建方法(new);
提供统一调用数据访问方法的方式;
避免调用数据访问方法和具体对象创建工作的紧偶合。
抽象工厂(Abstract Factory)设计模式的概念:
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
抽象工厂(Abstract Factory)设计模式的使用场合:
一个系统要独立于它产品的创建、组合和表示时。
一个系统要由多个产品系列中的一个来配置时。
visio中的图解:
类关系图:
A系列产品的代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryDll
{
public abstract class A
{
public abstract void ShowA();
}
}
public class PA1:A
{
public PA1()
{
}
public override void ShowA()
{
// throw new Exception("The method or operation is not implemented.");
Console.WriteLine( " show a1! " );
Console.WriteLine( " ---------- " );
}
}
public class PA2:A
{
public PA2()
{
}
public override void ShowA()
{
// throw new Exception("The method or operation is not implemented.");
Console.WriteLine( " show a2! " );
Console.WriteLine( " ------- " );
}
}
B系列产品的代码如下:
using System.Collections.Generic;
using System.Text;
namespace AbstractFactoryDll
{
public abstract class B
{
public abstract void ShowB();
}
}
public class PB1:B
{
public PB1()
{
}
public override void ShowB()
{
// throw new Exception("The method or operation is not implemented.");
Console.WriteLine( " show b1! " );
Console.WriteLine( " ------- " );
}
}
public class PB2:B
{
public PB2()
{
}
public override void ShowB()
{
// throw new Exception("The method or operation is not implemented.");
Console.WriteLine( " show b2! " );
Console.WriteLine( " ------- " );
}
}
抽象工厂系列产品的代码如下:
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Reflection;
5
6
7 using System.Data;
8 using System.Xml;
9
10 namespace AbstractFactoryDll
11 {
12 public abstract class AbstractFactory
13 {
14
15 public static AbstractFactory GetInstance()
16 {
17 DataSet ds = new DataSet();
18 string currentPath = System.IO.Directory.GetCurrentDirectory();
19 // string path=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
20 string path = currentPath.Substring( 0 , currentPath.LastIndexOf( " bin " ));
21 string configurePath = path + " configure.xml " ;
22 System.Xml.XmlDocument doc = new XmlDocument();
23 AbstractFactory abstractFactoryInstance;
24 try
25 {
26 doc.Load(configurePath);
27 XmlNodeList nodes = doc.SelectNodes( " /configuration/add " );
28 string value = "" ;
29 if (nodes != null && nodes.Count == 1 )
30 {
31 value = nodes[ 0 ].Attributes[ " value " ].Value;
32 }
33 // string factoryName7 = System.Configuration.ConfigurationSettings.AppSettings["Name"].ToString();
34 // System.Configuration.ConfigurationSettings.AppSettings[]
35 string factoryName = @" AbstractFactoryDll. " + value.Trim();
36
37 if (factoryName != "" )
38 {
39 // abstractFactoryInstance = (AbstractFactory)Activator.CreateInstance(Type.GetType("AbstractFactoryDll.EntityFactory2"));
40 Assembly assembly = Assembly.GetExecutingAssembly();
41 // abstractFactoryInstance = (AbstractFactory)(assembly.CreateInstance(factoryName));
42 // abstractFactoryInstance =(AbstractFactory) typeof(factoryName);
43 abstractFactoryInstance = (AbstractFactory)assembly.CreateInstance(factoryName, false );
44 }
45 else
46 {
47 abstractFactoryInstance = null ;
48 }
49 }
50 catch (Exception ex)
51 {
52 throw new Exception(ex.Message, ex);
53 }
54 finally
55 {
56
57 }
58 return abstractFactoryInstance;
59 }
60
61 public abstract A CreateA();
62 public abstract B CreateB();
63 }
64 }
65
66 using System;
67 using System.Collections.Generic;
68 using System.Text;
69
70 namespace AbstractFactoryDll
71 {
72 public class EntityFactory1:AbstractFactory
73 {
74 public EntityFactory1()
75 {
76
77 }
78 public override A CreateA()
79 {
80 return new PA1();
81
82 }
83 public override B CreateB()
84 {
85 return new PB1();
86 }
87 }
88 }
89 using System;
90 using System.Collections.Generic;
91 using System.Text;
92
93 namespace AbstractFactoryDll
94 {
95 public class EntityFactory2:AbstractFactory
96 {
97 public EntityFactory2()
98 {
99
100 }
101 public override A CreateA()
102 {
103 return new PA2();
104 }
105 public override B CreateB()
106 {
107 return new PB2();
108 }
109 }
110 }
111
配置文件configure.xml 放在客户端:
< configuration >
< add value = " EntityFactory2 " ></ add >
</ configuration >
客户端代码:注意引用程序集 以及命名空间;
using System.Collections.Generic;
using System.Text;
using AbstractFactoryDll;
namespace AbstractFactory
{
class Program
{
static void Main( string [] args)
{
/* A a = new EntityFactory1().CreateA();
a.ShowA();
B b = new EntityFactory1().CreateB();
b.ShowB();
Console.ReadLine(); */
AbstractFactoryDll.AbstractFactory abstractFactory = AbstractFactoryDll.AbstractFactory.GetInstance();
AbstractFactoryDll.A a = abstractFactory.CreateA();
a.ShowA();
AbstractFactoryDll.B b = abstractFactory.CreateB();
b.ShowB();
Console.ReadLine();
}
}
}
源代码下载:抽象工厂源代码下载
自己写的,有不对的地方还请见谅并且指出,不胜感激,呵呵。学习ING.......