factory method随想1
public abstract class Fruit
{
public abstract void Wash();
public abstract void Hush();
}
class Apple:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
class Orange:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
public abstract class FruitFactory
{
public abstract Fruit Create();
}
public class AppleFactory
{
public override Fruit Create()
{
return new Apple();
}
}
public class OrangeFactory
{
public override Fruit Create()
{
return new Orange();
}
}
class App
{
public static void Main()
{
FruitFactory fruitFactory=new AppleFactory();
//FruitFactory fruitFactory=Assembly.Load("AssemblyName").CreateInstrance("AppleFactory");
CleanFruit(fruitFactory);
}
public static void CleanFruit(FruitFactory fruitFactory)
{
Fruit fruit=fruitFactory.Create();
fruit.Wash();
fruit.Hush();
}
}
//很典型的Factory Method。可是我觉得烦了,看下面的写法。
public abstract class Fruit
{
public abstract void Wash();
public abstract void Hush();
}
class Apple:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
class Orange:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
public class FruitFactory
{
public static Fruit Create()
{
Fruit fruit=Assembly.Load("AssemblyName").CreateInstrance("Apple");
}
}
class App
{
public static void Main()
{
CleanFruit();
}
public static void CleanFruit(FruitFactory fruitFactory)
{
Fruit fruit=fruitFactory.Create();
fruit.Wash();
fruit.Hush();
}
}
{
public abstract void Wash();
public abstract void Hush();
}
class Apple:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
class Orange:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
public abstract class FruitFactory
{
public abstract Fruit Create();
}
public class AppleFactory
{
public override Fruit Create()
{
return new Apple();
}
}
public class OrangeFactory
{
public override Fruit Create()
{
return new Orange();
}
}
class App
{
public static void Main()
{
FruitFactory fruitFactory=new AppleFactory();
//FruitFactory fruitFactory=Assembly.Load("AssemblyName").CreateInstrance("AppleFactory");
CleanFruit(fruitFactory);
}
public static void CleanFruit(FruitFactory fruitFactory)
{
Fruit fruit=fruitFactory.Create();
fruit.Wash();
fruit.Hush();
}
}
//很典型的Factory Method。可是我觉得烦了,看下面的写法。
public abstract class Fruit
{
public abstract void Wash();
public abstract void Hush();
}
class Apple:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
class Orange:Fruit
{
public override void Wash()
{}
public override void Hush()
{}
}
public class FruitFactory
{
public static Fruit Create()
{
Fruit fruit=Assembly.Load("AssemblyName").CreateInstrance("Apple");
}
}
class App
{
public static void Main()
{
CleanFruit();
}
public static void CleanFruit(FruitFactory fruitFactory)
{
Fruit fruit=fruitFactory.Create();
fruit.Wash();
fruit.Hush();
}
}
//在这个实例中,同样实现了动态灵活。可为什么会写出这样的代码,因为我不是按照模式来写的,而是根据我想达到的目的来写的。
//这样的代码没错,它用反射的特性很好地解决了我目前的问题,那是不是就不要factory method了?
//当然不是的。这是factory method很重要的。这里引用terrylee的原话:工厂的作用并不仅仅只是创建一个对象,它还可以做对象的初始化,参数的设置等。
//这里说下我的观点。做对象的初始化,我想这是不必要的,可在对象的构造器中去做,真正重要的是对参数的设置。