Java设计模式 - 抽象工厂模式
抽象工厂模式是另一个创建模式。
抽象工厂模式,也称为工厂的工厂,有一个工厂创建其他工厂。
当使用抽象工厂模式时,我们首先使用超级工厂创建工厂,然后使用创建的工厂创建对象。
例子
下面的代码展示了如何使用抽象工厂模式。
我们将要创建形状和打印机。对于形状,我们会有圆形,矩形和正方形。对于打印机,我们将有纸张打印机,网络打印机和屏幕打印机。
对于shape,我们将创建Shape接口,如下所示:
/**
* Created by david.tian on 16/08/2017.
*/
public interface IShape {
void draw();
}
然后我们创建实现Shape接口的具体类。
/**
* Created by david.tian on 16/08/2017.
*/
public class Rectangle implements IShape {
@Override
public void draw(){
System.out.println("Inside Rectangle::draw() method.");
}
}
/**
* Created by david.tian on 16/08/2017.
*/
public class Square implements IShape {
@Override
public void draw(){
System.out.println("Inside Square::draw() method.");
}
}
/**
* Created by david.tian on 16/08/2017.
*/
public class Circle implements IShape {
@Override
public void draw(){
System.out.println("Inside Circle::draw() method.");
}
}
我们为打印机创建一个接口。
/**
* Created by david.tian on 16/08/2017.
*/
public interface IPrinter {
void print();
}
然后我们创建实现Printer接口的具体类。
/**
* Created by david.tian on 16/08/2017.
*/
public class PaperPrinter implements IPrinter {
@Override
public void print(){
System.out.println("paper");
}
}
/**
* Created by david.tian on 16/08/2017.
*/
public class WebPrinter implements IPrinter {
@Override
public void print(){
System.out.println("webprinter");
}
}
/**
* Created by david.tian on 16/08/2017.
*/
public class ScreenPrinter implements IPrinter {
@Override
public void print(){
System.out.println("screen");
}
}
最后,我们创建一个抽象类来获取打印机和形状对象的工厂。
/**
* Created by david.tian on 16/08/2017.
*/
public abstract class AbstractFactory {
abstract IPrinter getPrinter(String type);
abstract IShape getShape(String shape);
}
最后,我们创建Factory类,根据给定的信息扩展AbstractFactory以生成具体类的对象。
/**
* Created by david.tian on 16/08/2017.
*/
public class ShapeFactory extends AbstractFactory {
@Override
public IShape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
@Override
public IPrinter getPrinter(String printerType){
return null;
}
}
/**
* Created by david.tian on 16/08/2017.
*/
public class PrinterFactory extends AbstractFactory {
@Override
IPrinter getPrinter(String type) {
if(type == null){
return null;
}
if(type.equalsIgnoreCase("PAPER")){
return new PaperPrinter();
} else if(type.equalsIgnoreCase("WEB")){
return new WebPrinter();
} else if(type.equalsIgnoreCase("SCREEN")){
return new ScreenPrinter();
}
return null;
}
@Override
IShape getShape(String shape) {
return null;
}
}
下面的代码展示了如何使用抽象工厂模式。
/**
* Created by david.tian on 16/08/2017.
*/
public class AbstractFactoryTest {
public static void main(String[] args){
//get shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//get an object of Shape Circle
IShape circle = shapeFactory.getShape("CIRCLE");
//call draw method of Shape Circle
circle.draw();
//get an object of Shape Rectangle
IShape rectangle = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle
rectangle.draw();
//get an object of Shape square
IShape square = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square
square.draw();
//get Printer factory
AbstractFactory printerFactory = FactoryProducer.getFactory("PRINTER");
//get an object of Printer paper
IPrinter paper = printerFactory.getPrinter("PAPER");
//call print method
paper.print();
//get an object of Printer paper
IPrinter webprinter = printerFactory.getPrinter("WEB");
//call print method
webprinter.print();
//get an object of Printer paper
IPrinter screenprinter = printerFactory.getPrinter("SCREEN");
//call print method
screenprinter.print();
}
}
上面的代码生成以下结果。
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
paper
webprinter
screen