=============================================================================
java代码实现:
步骤一:创建Shape接口
Shape.java
package AbstractFactoryMode;
public interface Shape {
void draw();
}
步骤二:创建Shape接口的实现类
Circle.java
package AbstractFactoryMode;
public class Circle implements Shape {
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("这是circle的draw方法");
}
}
Square.java
package AbstractFactoryMode;
public class Square implements Shape {
@Override
public void draw() {
// TODO Auto-generated method stub
System.out.println("这是square的draw方法");
}
}
步骤三:创建Color接口
package AbstractFactoryMode;
public interface Color {
void getColor();
}
步骤四:创建color接口的实现类
Red.java
package AbstractFactoryMode;
public class Red implements Color {
@Override
public void getColor() {
// TODO Auto-generated method stub
System.out.println("这是red的getColor方法");
}
}
Green.java
package AbstractFactoryMode;
public class Green implements Color {
@Override
public void getColor() {
// TODO Auto-generated method stub
System.out.println("这是green的getColor方法");
}
}
步骤五:创建一个接口,用于定义超级工厂有哪些功能,如形状,颜色
AbstractFactory.java
package AbstractFactoryMode;
public interface AbstractFactory {
public abstract Color getColor2(String color);
public abstract Shape getShape(String shapeType) ;
}
步骤六:定义形状工厂,生产形状对象
ShapeFactory.java
package AbstractFactoryMode;
public class ShapeFactory implements AbstractFactory {
@Override
public Color getColor2(String color) {
// TODO Auto-generated method stub
return null;
}
@Override
public Shape getShape(String shapeType) {
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("circle")){
return new Circle();
}
else if(shapeType.equalsIgnoreCase("square")){
return new Square();
}
return null;
}
}
定义颜色工厂,用于生产颜色对象
ColorFactory.java
package AbstractFactoryMode;
public class ShapeFactory implements AbstractFactory {
@Override
public Color getColor2(String color) {
// TODO Auto-generated method stub
return null;
}
@Override
public Shape getShape(String shapeType) {
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("circle")){
return new Circle();
}
else if(shapeType.equalsIgnoreCase("square")){
return new Square();
}
return null;
}
}
步骤七:定义超级工厂,用于确定是生产形状还是颜色
SuperFactory.java
package AbstractFactoryMode;
public class FactoryProducer {
public static AbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("shape")){
return new ShapeFactory();
} else if(choice.equalsIgnoreCase("color")){
return new ColorFactory();
}
return null;
}
}
步骤八:测试
Test.java
package AbstractFactoryMode;
public class Test {
public static void main(String[] args) {
//获取形状工厂
AbstractFactory shapeFactory = SuperFactory.getFactory("SHAPE");
//获取形状为 Circle 的对象
Shape shape1 = shapeFactory.getShape("CIRCLE");
//调用 Circle 的 draw 方法
shape1.draw();
//获取形状为 Square 的对象
Shape shape2 = shapeFactory.getShape("SQUARE");
//调用 Square 的 draw 方法
shape2.draw();
//获取颜色工厂
AbstractFactory colorFactory = SuperFactory.getFactory("COLOR");
//获取颜色为 Red 的对象
Color color1 = colorFactory.getColor2("RED");
//调用 Red 的 getColor 方法
color1.getColor();
//获取颜色为 Green 的对象
Color color2 = colorFactory.getColor2("Green");
//调用 Green 的 getColor 方法
color2.getColor();
}
}
运行结果