Core Design Patterns(9) Factory Method 工厂方法模式
VS 2008
工厂方法模式提供创建一系列属于同一产品体系的类的创建方法。并且,有别于一般的简单工厂,工厂方法模式创建产品由子类来完成。这样增加新的产品不需要修改原来的代码,只需要相应增加一个新的工厂子类。
1. 模式UML图
![](/images/cnblogs_com/guozhijian/Design Pattern/factoryMethod-1.gif)
2. 应用
程序中需要使用到不同的交通工具,将交通工具的行为抽象为一个接口,那么这些交通工具就形成了一个有共同行为的产品体系。根据工厂方法模式的思想,为产品的创建相应的建立一套工厂体系。
![](/images/cnblogs_com/guozhijian/Design Pattern/factoryMethod-2.gif)
ITransportation.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod.BLL {
![](/Images/OutliningIndicators/InBlock.gif)
public interface ITransportation {
![](/Images/OutliningIndicators/InBlock.gif)
void Run();
}
}
Car.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod.BLL {
![](/Images/OutliningIndicators/InBlock.gif)
public class Car : ITransportation {
ITransportation Members
}
}
Plane.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod.BLL {
![](/Images/OutliningIndicators/InBlock.gif)
public class Plane : ITransportation {
ITransportation Members
}
}
ITransportationFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod.BLL {
public interface ITransportationFactory {
![](/Images/OutliningIndicators/InBlock.gif)
ITransportation Create();
}
}
CarFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod.BLL {
![](/Images/OutliningIndicators/InBlock.gif)
public class CarFactory : ITransportationFactory {
ITransportationFactory Members
}
}
PlaneFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod.BLL {
![](/Images/OutliningIndicators/InBlock.gif)
public class PlaneFactory : ITransportationFactory {
ITransportationFactory Members
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DesignPattern.FactoryMethod.BLL;
![](/Images/OutliningIndicators/None.gif)
namespace DesignPattern.FactoryMethod {
class Program {
static void Main(string[] args) {
![](/Images/OutliningIndicators/InBlock.gif)
ITransportationFactory carFactory = new CarFactory();
ITransportation car = carFactory.Create();
car.Run();
![](/Images/OutliningIndicators/InBlock.gif)
ITransportationFactory planeFactory = new PlaneFactory();
ITransportation plane = planeFactory.Create();
plane.Run();
}
}
}
Output
工厂方法模式提供创建一系列属于同一产品体系的类的创建方法。并且,有别于一般的简单工厂,工厂方法模式创建产品由子类来完成。这样增加新的产品不需要修改原来的代码,只需要相应增加一个新的工厂子类。
1. 模式UML图
![](/images/cnblogs_com/guozhijian/Design Pattern/factoryMethod-1.gif)
2. 应用
程序中需要使用到不同的交通工具,将交通工具的行为抽象为一个接口,那么这些交通工具就形成了一个有共同行为的产品体系。根据工厂方法模式的思想,为产品的创建相应的建立一套工厂体系。
![](/images/cnblogs_com/guozhijian/Design Pattern/factoryMethod-2.gif)
ITransportation.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
Car.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
Plane.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
ITransportationFactory.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
CarFactory.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
PlaneFactory.cs
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
Client
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
Output
![](/images/cnblogs_com/guozhijian/Design Pattern/factoryMethod-3.gif)