看看petshop interface IOperation { void operation(); } class SavingBox:IOperation { public void operation() { Console.WriteLine("saving!"); } } class OutingBox : IOperation { public void operation() { Console.WriteLine("outing!"); } } class SimpleFactory { public static IOperation getOpertion(string operation) { IOperation opera; switch (operation) { case "out": opera = new OutingBox(); break; default: opera = new SavingBox(); break; } return opera; } } IOperation opera = SimpleFactory.getOpertion(""); opera.operation(); Console.ReadKey(); 参考 参考
class A { | class SimpleFactory//简单工厂 { public static A GetA() { A a = new A; return a; } } 可以理解为 1 创建对象的集中地:创建A对象的唯一入口点,以增加可维护性; 2 5楼是基于接口编程的范例,更有弹性:主要是提高模块重用度时,大大的好。
interface IOperation
{
void operation();
}
class SavingBox:IOperation
{
public void operation()
{
Console.WriteLine("saving!");
}
}
class OutingBox : IOperation
{
public void operation()
{
Console.WriteLine("outing!");
}
}
class SimpleFactory
{
public static IOperation getOpertion(string operation)
{
IOperation opera;
switch (operation)
{
case "out":
opera = new OutingBox();
break;
default:
opera = new SavingBox();
break;
}
return opera;
}
}
IOperation opera = SimpleFactory.getOpertion("");
opera.operation();
Console.ReadKey();
参考
参考
{
|
class SimpleFactory//简单工厂
{
public static A GetA()
{
A a = new A;
return a;
}
} 可以理解为
1 创建对象的集中地:创建A对象的唯一入口点,以增加可维护性;
2 5楼是基于接口编程的范例,更有弹性:主要是提高模块重用度时,大大的好。