---
title: "抽象工厂模式"
author: "BestUpon"
date: "2009-09-28T11:20:50Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2009-09-28T11:20:50Z"
id: 479174
url: "https://bestupon.iteye.com/blog/479174"
---

```
package org.bestupon.abstratfactory.product;

public class Product2OtherProtory implements OtherProtory{

	private String otherProtoryStr;
	
	public String otherProtoryInfo(String protoryInfo) {
		this.otherProtoryStr = protoryInfo;
		return otherProtoryStr;
	}

	public void printOtherProtoryInfo() {
		System.out.println(this.otherProtoryStr);
	}

}
```

 再看看这个抽象的工厂是干什么的？

 

```
package org.bestupon.abstratfactory.fatory;

import org.bestupon.abstratfactory.product.OtherProtory;
import org.bestupon.abstratfactory.product.ProductColor;


public interface AbstratFactory {
	public OtherProtory createOtherProtory();
	public ProductColor createProductColor();
	
}
```

 

他负责创建了一系列的产品（组装一个产品的抽象属性，这样理解比较好理解，比如汽车和轮胎，和底座等关系参照）。

每一种产品都有一个自己实现类，那就是：

```
package org.bestupon.abstratfactory.fatory;

import org.bestupon.abstratfactory.product.OtherProtory;
import org.bestupon.abstratfactory.product.Product1OtherProtory;
import org.bestupon.abstratfactory.product.Product1ProductColor;
import org.bestupon.abstratfactory.product.ProductColor;

public class Product1Factory  implements AbstratFactory{
	
	Product1OtherProtory product1OtherProtory = null;
	Product1ProductColor product1ProductColor = null;
	
	public OtherProtory createOtherProtory() {
		product1OtherProtory = new Product1OtherProtory();
		product1OtherProtory.otherProtoryInfo("产品一的其他属性");
		return product1OtherProtory;
	}

	public ProductColor createProductColor() {
		product1ProductColor = new Product1ProductColor();
		product1ProductColor.otherProductColor("产品一的颜色");
		return product1ProductColor;
	}

}
```

 

还有：

```
package org.bestupon.abstratfactory.fatory;

import org.bestupon.abstratfactory.product.OtherProtory;
import org.bestupon.abstratfactory.product.Product2OtherProtory;
import org.bestupon.abstratfactory.product.Product2ProductColor;
import org.bestupon.abstratfactory.product.ProductColor;

public class Product2Factory  implements AbstratFactory{
	
	Product2OtherProtory product2OtherProtory = null;
	Product2ProductColor product2ProductColor = null;
	
	public OtherProtory createOtherProtory() {
		product2OtherProtory = new Product2OtherProtory();
		product2OtherProtory.otherProtoryInfo("产品【二】的其他属性");
		return product2OtherProtory;
	}

	public ProductColor createProductColor() {
		product2ProductColor = new Product2ProductColor();
		product2ProductColor.otherProductColor("产品【二】的颜色");
		return product2ProductColor;
	}

}
```

 

 

看看简单的客户端：

```
package org.bestupon.abstratfactory.client;

import org.bestupon.abstratfactory.fatory.AbstratFactory;
import org.bestupon.abstratfactory.fatory.Product1Factory;
import org.bestupon.abstratfactory.fatory.Product2Factory;
import org.bestupon.abstratfactory.product.OtherProtory;
import org.bestupon.abstratfactory.product.ProductColor;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AbstratFactory abstratFactory = new Product1Factory();
		OtherProtory otherProtory = abstratFactory.createOtherProtory();
		otherProtory.printOtherProtoryInfo();
		ProductColor productColor = abstratFactory.createProductColor();
		productColor.printOtherProductColor();
		//以上是系列一的产品，下面是系列二产品，注意更换了接口
		abstratFactory = new Product2Factory();
		otherProtory = abstratFactory.createOtherProtory();
		otherProtory.printOtherProtoryInfo();
		productColor = abstratFactory.createProductColor();
		productColor.printOtherProductColor();
		
		
	}

}
```

 

看看以上的产品的，客户端要创建一列系列的产品，只需要更换一次接口，而其他的方法都是一模一样的。

 

注意：区别以上代码中，中产品一和产品二，其实都是一系列的意思。

 

 

使用抽象工厂模式（Abstract Factory）的意图很简单：那就是：提供一个创建一系列相关或者相互依赖对象的接口，而无需指定她们具体的类。

 

 

 

 

使用抽象工厂模式可以实现的效果：

 =  由工厂封装产品对象的创建，将客户端与产品类的实现相分离

 =  容易更换产品系列。简单只在换一系列产品的工厂制造接口

 =  有利于产品的一致性。同属于一系列的产品

 =  难以支持新产品的添加，因为要添加新产品就要更换产品工厂的interface接口。

抽象工厂一般有以下的实现方式：

   &gt;∞通常将同系列的产品工厂作为Singleton。

   &gt;∞AbstractFactory声明创建产品的接口，由工厂子类负责创建产品，通常为每个产品定义一个工厂方法。

   &gt;∞还一种不常用的就是使用参数方式创建对象，这种设计不太安全，所以很少使用。

现在以第二种实现方式为例说明AbstractFactory。

首先从需要创建的产品说起，组装一个产品，肯定有很多的产品零部件，或者产品的属性。

1.ProductColor是产品的一个抽象属性。

```
package org.bestupon.abstratfactory.product;

public interface ProductColor {
	public String otherProductColor(String productColor);
	public void printOtherProductColor();
}
```

 

2、产品还有其他的属性OtherProtory。

```
package org.bestupon.abstratfactory.product;

public interface OtherProtory {
	public String otherProtoryInfo(String protoryInfo);
	public void printOtherProtoryInfo();
}
```

 

两个抽象属性的实现类如下：

Product1ProductColor代表产品一的ProductColor实现，Product1OtherProtory代表产品一的OtherProtory实现。同理有Product2ProductColor代表产品一的ProductColor实现，Product2OtherProtory代表产品一的OtherProtory实现。注意体会一系列的含义。

见代码：

```
package org.bestupon.abstratfactory.product;

public class Product1ProductColor implements ProductColor {

	private String colorString ;	
	
	public String otherProductColor(String productColor) {
		this.colorString = productColor;
		return colorString;
	}

	public void printOtherProductColor() {
		System.out.println(this.otherProductColor(this.colorString));
	}

}
```

 

 

```
package org.bestupon.abstratfactory.product;

public class Product1OtherProtory implements OtherProtory{

	private String otherProtoryStr;
	
	public String otherProtoryInfo(String protoryInfo) {
		this.otherProtoryStr = protoryInfo;
		return otherProtoryStr;
	}

	public void printOtherProtoryInfo() {
		System.out.println(this.otherProtoryStr);
	}

}
```

 

```
package org.bestupon.abstratfactory.product;

public class Product2ProductColor implements ProductColor {

	private String colorString ;	
	
	public String otherProductColor(String productColor) {
		this.colorString = productColor;
		return colorString;
	}

	public void printOtherProductColor() {
		System.out.println(this.otherProductColor(this.colorString));
	}

}
```

  

 

抽象工厂模式的优缺点分析：

 1、优点：多个产品同时创建。客户端代码简洁。  
  一个具体的工厂创建一系列相互联系的产品，使得客户端的调用非常简单，原因是一系列的产品被  
 设计到一个工厂类中来创建。客户端需要创建另外一些列的产品，只需要更换一个产品创建类就可以了。  
 2、缺点：增加新产品的话，需要修改工厂类（接口），以及其实现类。  
 3、使用时机：在客户端需要同时创建一些列比较固定的对象时，可以考虑使用抽象工厂模式。

 4、适用性：

  ·一个系统要独立他的产品创建、组合和表示时。

  ·一个系统要用多个产品系列中的一个来配置时

  ·要强调一系列相关的产品对象和对象的设计以便进行联合使用时

  ·提供一个产品类库，而只想显示他们的接口而不是实现时

 

 附件：包含了能直接运行的代码示例。