嘿嘿,新手参考学习~~~大鸟表笑话俺,哈哈~~using System;namespace DesignPatterns
{
public interface Fruit //具体产品的抽象
{
string Color { get;set;}
} public class Orange : Fruit
{
private string color; public Orange(){ } public string Color
{
get { return color; }
set { color = value; }
}
}
public class Apple : Fruit
{
private string color; public Apple() { } public string Color
{
get { return color; }
set { color = value; }
}
}
public class FruitFactory //工厂类
{
/*==========================================================================
* 注意:这里的name格式为:namespace.class!否则type的返回值一定是null
*==========================================================================*/
public static Fruit CreateFruit(string name)
{
Fruit fruit = null;
/*=============================================================================
* System.Type类: 它对于反射起着核心的作用。我们可以使用 Type 对象的
* 方法、 字段、属性和嵌套类来查找有关该类型的所有信息。
*
* System.Activator类:它包含特定的方法,用以在本地或从远程创建对象类型,
* 或获取对现有远程对象的引用。
*=============================================================================*/
Type type = Type.GetType(name, false);
if (type != null)
fruit = (Fruit)Activator.CreateInstance(type);
return fruit;
}
} class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please write the class name that you want to create:");
string name = Console.ReadLine();
Console.WriteLine("OK,Try to create the class that named {0}",name);
Fruit fruit = FruitFactory.CreateFruit(name);
if (fruit != null)
Console.WriteLine("Success!the class has been created!");
else
Console.WriteLine("Failed!there must be some error!");
}
}
}测试时可以输入:DesignPatterns.Apple or DesignPatterns.Orange然后就可以看见成功的输出了~~!!!
{
public interface Fruit //具体产品的抽象
{
string Color { get;set;}
} public class Orange : Fruit
{
private string color; public Orange(){ } public string Color
{
get { return color; }
set { color = value; }
}
}
public class Apple : Fruit
{
private string color; public Apple() { } public string Color
{
get { return color; }
set { color = value; }
}
}
public class FruitFactory //工厂类
{
/*==========================================================================
* 注意:这里的name格式为:namespace.class!否则type的返回值一定是null
*==========================================================================*/
public static Fruit CreateFruit(string name)
{
Fruit fruit = null;
/*=============================================================================
* System.Type类: 它对于反射起着核心的作用。我们可以使用 Type 对象的
* 方法、 字段、属性和嵌套类来查找有关该类型的所有信息。
*
* System.Activator类:它包含特定的方法,用以在本地或从远程创建对象类型,
* 或获取对现有远程对象的引用。
*=============================================================================*/
Type type = Type.GetType(name, false);
if (type != null)
fruit = (Fruit)Activator.CreateInstance(type);
return fruit;
}
} class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please write the class name that you want to create:");
string name = Console.ReadLine();
Console.WriteLine("OK,Try to create the class that named {0}",name);
Fruit fruit = FruitFactory.CreateFruit(name);
if (fruit != null)
Console.WriteLine("Success!the class has been created!");
else
Console.WriteLine("Failed!there must be some error!");
}
}
}测试时可以输入:DesignPatterns.Apple or DesignPatterns.Orange然后就可以看见成功的输出了~~!!!
public class Pizza
{
public Pizza orderPizza(){
Pizza pizza = createPizza("chineese");
.............................
return pizza;
} public abstract Pizza createPizza(string pizzatype); //工厂,更高度的抽象,由子类实现更利于扩展。适用于某一系列的对象,对于新的对象则开发新的子类
}
好像在51aspx上见过完整源码,记不大清楚了
一般+ singleton第一次加载时略慢(比不反射)
以后都很方便反射工厂的优点:可以动态配置,低耦合
------------------------------------------可以看看Spring.net