工厂方法模式是一种创建型模式,其在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。
假设你正在开发一款物流管理应用。最初版本只能处理卡车运输,因此大部分代码都在位于名为卡车
的类中。
一段时间后,这款应用变得极受欢迎。你每天都能收到十几次来自海运公司的请求,希望应用能够支持海上物流功能。
这可是个好消息。但是代码问题该如何处理呢? 目前,大部分代码都与卡车
类相关。在程序中添加轮船
类需要修改全部代码。更糟糕的是,如果你以后需要在程序中支持另外一种运输方式,很可能需要再次对这些代码进行大幅修改。
最后,你将不得不编写繁复的代码,根据不同的运输对象类,在应用中进行不同的处理。
工厂方法模式建议使用特殊的工厂方法代替对于对象构造函数的直接调用(即使用new
运算符)。不用担心,对象仍将通过new
运算符创建,只是该运算符改在工厂方法中调用罢了。工厂方法返回的对象通常被称作 “产品”。
乍看之下,这种更改可能毫无意义:我们只是改变了程序中调用构造函数的位置而已。但是,仔细想一下,现在你可以在子类中重写工厂方法,从而改变其创建产品的类型。
但有一点需要注意:仅当这些产品具有共同的基类或者接口时,子类才能返回不同类型的产品,同时基类中的工厂方法还应将其返回类型声明为这一共有接口。
举例来说,卡车Truck
和轮船Ship
类都必须实现运输Transport
接口,该接口声明了一个名为deliver交付
的方法。每个类都将以不同的方式实现该方法:卡车走陆路交付货物,轮船走海路交付货物。陆路运输RoadLogistics
类中的工厂方法返回卡车对象,而海路运输SeaLogistics
类则返回轮船对象。
调用工厂方法的代码(通常被称为客户端代码)无需了解不同子类返回实际对象之间的差别。客户端将所有产品视为抽象的运输
。客户端知道所有运输对象都提供交付
方法,但是并不关心其具体实现方式。
邮件
及其子类航空邮件
和陆路邮件
;运输
及其子类飞机
,卡车
和火车
。航空邮件
仅使用飞机
对象,而陆路邮件
则会同时使用卡车
和火车
对象。你可以编写一个新的子类(例如火车邮件
)来处理这两种情况,但是还有其他可选的方案。客户端代码可以给陆路邮件
类传递一个参数,用于控制其希望获得的产品。using System;
namespace RefactoringGuru.DesignPatterns.FactoryMethod.Conceptual
{
// The Creator class declares the factory method that is supposed to return
// an object of a Product class. The Creator's subclasses usually provide
// the implementation of this method.
abstract class Creator
{
// Note that the Creator may also provide some default implementation of
// the factory method.
public abstract IProduct FactoryMethod();
// Also note that, despite its name, the Creator's primary
// responsibility is not creating products. Usually, it contains some
// core business logic that relies on Product objects, returned by the
// factory method. Subclasses can indirectly change that business logic
// by overriding the factory method and returning a different type of
// product from it.
public string SomeOperation()
{
// Call the factory method to create a Product object.
var product = FactoryMethod();
// Now, use the product.
var result = "Creator: The same creator's code has just worked with "
+ product.Operation();
return result;
}
}
// Concrete Creators override the factory method in order to change the
// resulting product's type.
class ConcreteCreator1 : Creator
{
// Note that the signature of the method still uses the abstract product
// type, even though the concrete product is actually returned from the
// method. This way the Creator can stay independent of concrete product
// classes.
public override IProduct FactoryMethod()
{
return new ConcreteProduct1();
}
}
class ConcreteCreator2 : Creator
{
public override IProduct FactoryMethod()
{
return new ConcreteProduct2();
}
}
// The Product interface declares the operations that all concrete products
// must implement.
public interface IProduct
{
string Operation();
}
// Concrete Products provide various implementations of the Product
// interface.
class ConcreteProduct1 : IProduct
{
public string Operation()
{
return "{Result of ConcreteProduct1}";
}
}
class ConcreteProduct2 : IProduct
{
public string Operation()
{
return "{Result of ConcreteProduct2}";
}
}
class Client
{
public void Main()
{
Console.WriteLine("App: Launched with the ConcreteCreator1.");
ClientCode(new ConcreteCreator1());
Console.WriteLine("");
Console.WriteLine("App: Launched with the ConcreteCreator2.");
ClientCode(new ConcreteCreator2());
}
// The client code works with an instance of a concrete creator, albeit
// through its base interface. As long as the client keeps working with
// the creator via the base interface, you can pass it any creator's
// subclass.
public void ClientCode(Creator creator)
{
// ...
Console.WriteLine("Client: I'm not aware of the creator's class," +
"but it still works.\n" + creator.SomeOperation());
// ...
}
}
class Program
{
static void Main(string[] args)
{
new Client().Main();
}
}
}
执行结果:
App: Launched with the ConcreteCreator1.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of ConcreteProduct1}
App: Launched with the ConcreteCreator2.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of ConcreteProduct2}
参考原文:工厂方法设计模式