抽象工厂模式,也称为工厂的工厂,由一个工厂创建其他工厂。
当使用抽象工厂模式时,我们首先使用超级工厂创建工厂,然后使用创建的工厂创建对象
-------------------------------------------------------------------------------------------------------------------------------
使用抽象工厂的步骤:
1.建2个接口
package com.test;
public interface IPrinter {
void print();
}
package com.test;
public interface IShape {
void draw();
}
2.建一个抽象类来封装这2个接口
package com.test;
public abstract class AbstractFactory {
abstract IPrinter getPrinter(String type);
abstract IShape getShape(String shape);
}
3.分别写这2个接口的实现类
package com.test;
public class Circle implements IShape{
public void draw() {
// TODO Auto-generated method stub
System.out.println("Inside Circle::draw() method.");
}
}
package com.test;
public class Rectangle implements IShape {
public void draw() {
System.out.println("Inside Rectangle::draw() method");
}
}
package com.test;
public class Square implements IShape{
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
package com.test;
public class PaperPrinter implements IPrinter {
public void print() {
// TODO Auto-generated method stub
System.out.println("paper");
}
}
package com.test;
public class ScreenPrinter implements IPrinter {
public void print() {
// TODO Auto-generated method stub
System.out.println("screen");
}
}
package com.test;
public class WebPrinter implements IPrinter {
public void print() {
// TODO Auto-generated method stub
System.out.println("Web");
}
}
4.创建2个接口的工厂类
package com.test;
public class ShapeFactory extends AbstractFactory{
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;
}
IPrinter getPrinter(String type) {
return null;
}
}
package com.test;
public class PrinterFactory extends AbstractFactory{
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;
}
IShape getShape(String shape) {
// TODO Auto-generated method stub
return null;
}
}
5.创建超级工厂类,来获取2个子工厂
package com.test;
public class FactoryProducer {
public static AbstractFactory getFactory(String choice) {
if(choice.equalsIgnoreCase("shape")){
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("printer")){
return new PrinterFactory();
}
return null;
}
}
6.创建测试类,测试程序
package com.test;
public class Main {
public static void main(String args[]) {
//用FactoryProducer获取shapeFactory
AbstractFactory shapeFactory =FactoryProducer.getFactory("shape");
//用shapeFactory获取circle rectangle square
IShape circle = shapeFactory.getShape("circle");
circle.draw();
IShape rectangle = shapeFactory.getShape("rectangle");
rectangle.draw();
IShape square = shapeFactory.getShape("square");
square.draw();
AbstractFactory printerFactory =FactoryProducer.getFactory("printer");
IPrinter paper = printerFactory.getPrinter("paper");
paper.print();
IPrinter Web = printerFactory.getPrinter("Web");
Web.print();
IPrinter Screen = printerFactory.getPrinter("Screen");
Screen.print();
}
}
代码下载地址如下