在现实生活中我们可以看见,乐事在卖薯片,可比克也在卖.
我敢肯定的说它们各自都有自己的工厂来生产薯片,而且生产出来的薯片味道还是不一样的.
这就是我们这里所谓的工厂方法模式. 我们来看看这个模式的UML图:
这个模式中我们可以看到:
产品和工厂2个基类,然后它们下面又有些子孙类.
--------------------------------------------------------------------------------
现在进入我们的代码环节了,首先上场的是PHP.
2 abstract class Product{
3 abstract function Test();
4 }
5
6 class leshi extends Product {
7 public function Test() {
8 echo("Leshi!\n");
9 }
10 }
11 class kebike extends Product {
12 public function Test() {
13 echo("Kebike!\n");
14 }
15 }
16 //抽象工厂
17 abstract class Factory{
18 abstract function GetProduct();
19 }
20
21 class leshiFactory extends Factory{
22 public function GetProduct() {
23 return new leshi();
24 }
25 }
26 class kebikeFactory extends Factory{
27 public function GetProduct() {
28 return new kebike();
29 }
30 }
31 //首先我们先建立两个公司的工厂.
32 $leshiF = new leshiFactory();
33 $kebikeF = new kebikeFactory();
34 //每个工厂给我出10包薯片
35 for($i=0;$i<10;$i++){
36 $leshiF->GetProduct()->Test();
37 $kebikeF->GetProduct()->Test();
38 }
39
40 //抽象产品
41 abstract class Product{
42 abstract function Test();
43 }
44
45 class leshi extends Product {
46 public function Test() {
47 echo("Leshi!\n");
48 }
49 }
50 class kebike extends Product {
51 public function Test() {
52 echo("Kebike!\n");
53 }
54 }
55 //抽象工厂
56 abstract class Factory{
57 abstract function GetProduct();
58 }
59
60 class leshiFactory extends Factory{
61 public function GetProduct() {
62 return new leshi();
63 }
64 }
65 class kebikeFactory extends Factory{
66 public function GetProduct() {
67 return new kebike();
68 }
69 }
70 //首先我们先建立两个公司的工厂.
71 $leshiF = new leshiFactory();
72 $kebikeF = new kebikeFactory();
73 //每个工厂给我出10包薯片
74 for($i=0;$i<10;$i++){
75 $leshiF->GetProduct()->Test();
76 $kebikeF->GetProduct()->Test();
77 }
78
79
和简单工厂模式有点儿像,但是那个只有一个工厂实例,
而我们这里是有多少种产品就有多少个工厂实例
--------------------------------------------------------------------------------
C#粉墨登场:
{
using System;
// These two classes could be part of a framework,
// which we will call DP
// ===============================================
class DPDocument
{
}
abstract class DPApplication
{
protected DPDocument doc;
abstract public void CreateDocument();
public void ConstructObjects()
{
// Create objects as needed
// . . .
// including document
CreateDocument();
}
abstract public void Dump();
}
// These two classes could be part of an application
// =================================================
class MyApplication : DPApplication
{
override public void CreateDocument()
{
doc = new MyDocument();
}
override public void Dump()
{
Console.WriteLine("MyApplication exists");
}
}
class MyDocument : DPDocument
{
}
/// <SUMMARY></SUMMARY>
/// Summary description for Client.
///
public class Client
{
public static int Main(string[] args)
{
MyApplication myApplication = new MyApplication();
myApplication.ConstructObjects();
myApplication.Dump();
return 0;
}
}
}
namespace FactoryMethod_DesignPattern
{
using System;
// These two classes could be part of a framework,
// which we will call DP
// ===============================================
class DPDocument
{
}
abstract class DPApplication
{
protected DPDocument doc;
abstract public void CreateDocument();
public void ConstructObjects()
{
// Create objects as needed
// . . .
// including document
CreateDocument();
}
abstract public void Dump();
}
// These two classes could be part of an application
// =================================================
class MyApplication : DPApplication
{
override public void CreateDocument()
{
doc = new MyDocument();
}
override public void Dump()
{
Console.WriteLine("MyApplication exists");
}
}
class MyDocument : DPDocument
{
}
///
/// Summary description for Client.
///
public class Client
{
public static int Main(string[] args)
{
MyApplication myApplication = new MyApplication();
myApplication.ConstructObjects();
myApplication.Dump();
return 0;
}
}
}
--------------------------------------------------------------------------------
Delphi王者驾临!
procedure Test;
end;
IFactory = interface
function GetProduct:IProduct;
end;
TLeShi = class(TInterfacedObject,IProduct)
public
procedure Test;
end;
TKeBiKe = class(TInterfacedObject,IProduct)
public
procedure Test;
end;
TLeShiFactory = class(TInterfacedObject,IFactory)
public
function GetProduct:IProduct;
end;
TKeBiKeFactory = class(TInterfacedObject,IFactory)
public
function GetProduct:IProduct;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TKeBiKeFactory }
function TKeBiKeFactory.GetProduct: IProduct;
begin
Result:=TKeBiKe.Create;
end;
{ TLeShi }
procedure TLeShi.Test;
begin
ShowMessage('LeShi!');
end;
{ TKeBiKe }
procedure TKeBiKe.Test;
begin
ShowMessage('KeBiKe');
end;
{ TLeShiFactory }
function TLeShiFactory.GetProduct: IProduct;
begin
Result:=TLeShi.Create;
end;
--------------------------------------------------------------------------------
给乐事和可比克打了广告不知道能不能得到点儿广告费哟..哈哈.
我们最后看看这个模式的适用性:
当一个类不知道它所必须创建的对象的类的时候。
当一个类希望由它的子类来指定它所创建的对象的时候。
当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。