工厂模式:我们需要什么就给工厂发出需求,工厂会给我们返回特定的需求(对象)
抽象工厂模式:给工厂建一个指挥部:FactoryProducer,假设又两个工厂,一个负责shape打印, 一个负责Color填充,我们想指挥部(FactoryProducer)提出需求,指挥部根据需求分配任务给各个工厂进行制造
简言之抽象工厂就是工厂的工厂,一个超级工厂
1.工厂模式
假设有需求:需要不同形状的图形
source code:
a.创建形状接口:
package factory;
public interface Type {
void draw();
}
b.根据需求创建相关的实体类:
class Rectangle implements Type{
@Override
public void draw() {
System.out.println("draw Rectangle");
}
}
class Circle implements Type{
@Override
public void draw() {
System.out.println("draw Circle");
}
}
class Triangle implements Type{
@Override
public void draw() {
System.out.println("draw Triangle");
}
}
c.创建工厂类:
class Factory{
// 根据具体需求(types),返回相应的对象
public Type getType(String types) {
// 如若实体类过多,可以用switch替代
if(types.equalsIgnoreCase("rectangle")) {
return new Rectangle();
}
else if(types.equalsIgnoreCase("circle")) {
return new Circle();
}
else if(types.equalsIgnoreCase("triangle")) {
return new Triangle();
}
else {
return null;
}
}
}
d.测试类:
public class FactoryDemo{
public static void main(String[] args) {
// Scanner s = new Scanner(System.in);
Factory f = new Factory();
// 提出需求,并得到相应的需求成果
Type type1 = f.getType("circle");
Type type2 = f.getType("triangle");
Type type3 = f.getType("Rectangle");
type1.draw();
type2.draw();
type3.draw();
}
}
e.运行结果:
draw Circle
draw Triangle
draw Rectangle
2.抽象工厂模式
outline:
先看看工厂的工作流程:
1.FactoryMain发出需求:制作一个Shape;
2.FactoryProducer收到请求,找到了Shape的制造厂:ShapeFactory,并返回给FactoryMain,意思是去找ShapeFactory制造吧;
3.FactoryMain就去找了ShapeFactory,并且告诉ShapeFactory我需要一个circle;
4.ShapeFactory收到请求,并制造出了相关的需求,并返回给了FactoryMain;
5.FactoryMain得到了自己想要的,于是开始操作这些对象;
6.FactoryMain又想填充颜色,同理,再去找FactoryProducer… …重复1~5类似的步骤;
souce code:
接口:Shape/…:
package abstract_factory;
public interface Shape {
void draw();
}
// 接口Color略
实体类Rectangle/…/…:
package abstract_factory;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("draw rectangle");
}
}
// Circle Triangle 略
工厂抽象类:
package abstract_factory;
public abstract class AbstractFactory {
public abstract Shape getShape(String shape_type);
public abstract Color getColor(String color_type);
}
工厂实体类:(分别负责生产 Shape ,Color)
package abstract_factory;
public class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shape_type) {
if(shape_type.equalsIgnoreCase("circle")) {
return new Circle();
}
else if(shape_type.equalsIgnoreCase("rectangle")) {
return new Rectangle();
}
else if(shape_type.equalsIgnoreCase("triangle")) {
return new Triangle();
}
else {
return null;
}
}
@Override
public Color getColor(String color_type) {
// TODO Auto-generated method stub
return null;
}
}
// ColorFactory略
超级工厂 FactoryProducer(指挥部):
package abstract_factory;
public class FactoryProducer {
public static AbstractFactory getFactory(String choice) {
if(choice.equalsIgnoreCase("shape")) {
return new ShapeFactory();
}
else if(choice.equalsIgnoreCase("color")) {
return new ColorFactory();
}
else {
return null;
}
}
}
测试类(FactoryMain):
package abstract_factory;
public class FactoryMain {
public static void main(String[] args) {
AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");
AbstractFactory colorFactory = FactoryProducer.getFactory("color");
Shape circle = shapeFactory.getShape("circle");
Shape rectangle = shapeFactory.getShape("rectangle");
Shape triangle = shapeFactory.getShape("triangle");
Color red = colorFactory.getColor("red");
Color blue = colorFactory.getColor("blue");
Color green = colorFactory.getColor("green");
circle.draw();
rectangle.draw();
triangle.draw();
red.fill();
blue.fill();
green.fill();
}
}
运行结果:
draw circle
draw rectangle
draw Triangle
fill red
fill blue
fill green