简要对比
- 简单工厂方法是一种编程习惯,不是设计模式:一个电脑工厂,负责生产不同的电脑。
- 工厂模式:联想工厂负责生产联想电脑,戴尔工厂负责生产戴尔电脑(实际的工厂类),它们都有生产电脑的方法(实现公共接口)。
- 抽象工厂:联想工厂既要生产电脑,又要生产鼠标;戴尔工厂既要生产电脑,又要生产鼠标(实际的工厂类)。他们都有生产电脑和鼠标的方法(实现公共接口)。其中生产鼠标和电脑都是用工厂模式实现(包含多个工厂模式)。
- 抽象工厂模式的优点是可以保证同一个工厂返回的同一个产品族,比如使用罗技的工厂就只会生产罗技的鼠标和键盘。缺点是扩展比较困难,接口标准里增加了一个方法,所有实现接口的子类都需要改变。
简单工厂方法:把生成对象集中到一个类里,严格来说不是设计模式,而是一种编程习惯。(创建方法通常定义为静态)
类图
示例
public interface Computer {
}
public class DellComputer implements Computer {
public DellComputer(){
System.out.println("戴尔");
}
}
public class Lenovo implements Computer {
public Lenovo(){
System.out.println("联想");
}
}
public class SimpleComputerFactory {
static Computer createComputer(String type){
Computer computer =null;
if("dell".equalsIgnoreCase(type)){
computer = new DellComputer();
}else if("lenovo".equalsIgnoreCase(type)){
computer = new Lenovo();
}
return computer;
}
}
运行
public class SimpleFactoryTestDemo {
public static void main(String[] args){
SimpleComputerFactory.createComputer("dell");
SimpleComputerFactory.createComputer("lenovo");
}
}
结果
工厂模式:定义了一个创建对象的接口,由子类决定要实例化的类,把实例化延迟到子类。
类图
示例
public interface Computer {
}
public class Lenovo implements Computer {
public Lenovo(){
System.out.println("联想");
}
}
public class ThinkPad implements Computer {
public ThinkPad(){
System.out.println("ThinkPad");
}
}
public interface ComputerFactory {
Computer createCompute(String type) ;
}
public class LenovoComputerFactory implements ComputerFactory{
@Override
public Computer createCompute(String type) {
Computer computer = null;
if("lenovo".equalsIgnoreCase(type)){
computer = new Lenovo();
}else if("thinkpad".equalsIgnoreCase(type)){
computer = new ThinkPad();
}
return computer;
}
}
运行
public class ComputerFactoryTestDemo {
public static void main(String[] args){
ComputerFactory lenovoComputerFactory = new LenovoComputerFactory();
lenovoComputerFactory.createCompute("lenovo");
lenovoComputerFactory.createCompute("thinkpad");
}
}
结果
抽象工厂模式:提供了一个接口,用来创建相关或依赖或依赖对象的家族,而不需要明确指定具体类。(常包含多个工厂模式,可以看做创建工厂的工厂)
类图
示例
//键盘接口
public interface KeyBoard {
}
public class LogitechKeyBoardA implements KeyBoard {
public LogitechKeyBoardA(){
System.out.println("罗技键盘A");
}
}
public class LogitechKeyBoardB implements KeyBoard {
public LogitechKeyBoardB(){
System.out.println("罗技键盘B");
}
}
public class RazerKeyBoardA implements KeyBoard {
public RazerKeyBoardA(){
System.out.println("雷蛇键盘A");
}
}
public class RazerKeyBoardB implements KeyBoard {
public RazerKeyBoardB(){
System.out.println("雷蛇键盘B");
}
}
//鼠标接口
public interface Mouse {
}
public class LogitechMouseA implements Mouse{
public LogitechMouseA(){
System.out.println("罗技鼠标A");
}
}
public class LogitechMouseB implements Mouse{
public LogitechMouseB(){
System.out.println("罗技鼠标B");
}
}
public class RazerMouseA implements Mouse{
public RazerMouseA(){
System.out.println("雷蛇鼠标A");
}
}
public class RazerMouseB implements Mouse{
public RazerMouseB(){
System.out.println("雷蛇鼠标B");
}
}
//外设工厂接口,负责生产键盘和鼠标
public interface PeripheralsFactory {
Mouse createMouse(String type);
KeyBoard createKeyBord(String type);
}
public class LogitechPeripheralsFactory implements PeripheralsFactory {
@Override
public Mouse createMouse(String type) {
Mouse mouse = null;
if ("A".equalsIgnoreCase(type)) {
mouse = new LogitechMouseA();
} else if ("B".equalsIgnoreCase(type)) {
mouse = new LogitechMouseB();
}
return mouse;
}
@Override
public KeyBoard createKeyBord(String type) {
KeyBoard keyBoard = null;
if ("A".equalsIgnoreCase(type)) {
keyBoard = new LogitechKeyBoardA();
} else if ("B".equalsIgnoreCase(type)) {
keyBoard = new LogitechKeyBoardB();
}
return keyBoard;
}
}
public class RazerPeripheralsFactory implements PeripheralsFactory {
@Override
public Mouse createMouse(String type) {
Mouse mouse = null;
if ("A".equalsIgnoreCase(type)) {
mouse = new RazerMouseA();
} else if ("B".equalsIgnoreCase(type)) {
mouse = new RazerMouseB();
}
return mouse;
}
@Override
public KeyBoard createKeyBord(String type) {
KeyBoard keyBoard = null;
if ("A".equalsIgnoreCase(type)) {
keyBoard = new RazerKeyBoardA();
} else if ("B".equalsIgnoreCase(type)) {
keyBoard = new RazerKeyBoardB();
}
return keyBoard;
}
}
运行
public class PeripheralsAbstractFactoryTestDemo {
public static void main(String[] args){
PeripheralsFactory razerFactory = new RazerPeripheralsFactory();
razerFactory.createKeyBord("A");
razerFactory.createKeyBord("B");
razerFactory.createMouse("A");
razerFactory.createMouse("B");
PeripheralsFactory logitechFactory = new LogitechPeripheralsFactory();
logitechFactory.createKeyBord("A");
logitechFactory.createKeyBord("B");
logitechFactory.createMouse("A");
logitechFactory.createMouse("B");
}
}
结果