工厂方法使用继承:把对象的创建委托给子类,子类实现工厂方法来创建对象。
抽象工厂使用对象组合:对象的创建被实现在工厂接口所暴露出来的方法中。
工厂方法demo:
interface Shape {
void draw();
}
class Circle implements Shape {
@Override
void draw() {
}
}
class Square implements Shape {
@Override
void draw() {
}
}
class Rectangle implements Shape {
@Override
void draw() {
}
}
class ShapeFactory {
Shape shape = null;
// 工厂根据需求生成子类对象,但是返回的接口是统一的,
// 都是以父类接口返回
public Shape getShape(String type) {
if (type.equals("CIRCLE")) {
shape = new Circle();
}
if (type.equals("RECTANGLE")) {
shape = new Rectangle();
}
if (type.equals("SQUARE")) {
shape = new Square();
}
return shape;
}
}
class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// 直接提需求,扔给工厂去做,而不需要关心内部细节
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = shapeFactory.getShape("RECTANGLE");
shape2.draw();
Shape shape3 = shapeFactory.getShape("SQUARE");
shape3.draw();
}
}
这些类的UML图为:
抽象工厂demo
interface Shape {
void draw();
}
class Rectangle implements Shape {
@Override
public void draw() {
}
}
class Square implements Shape {
@Override
public void draw() {
}
}
public class Circle implements Shape {
@Override
public void draw() {
}
}
interface Color {
void fill();
}
class Red implements Color {
@Override
public void fill() {
}
}
class Green implements Color {
@Override
public void fill() {
}
}
class Blue implements Color {
@Override
public void fill() {
}
}
abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
class ColorFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
if (color == null) {
return null;
}
if (color.equals("RED")) {
return new Red();
}
if (color.equals("GREEN")) {
return new Green();
}
if (color.equals("BLUE")) {
return new Blue();
}
}
@Override
public Shape getShape(String shape) {
return null;
}
}
class ShapeFactory extends AbstractFactory {
@Override
public Color getShape(String shape) {
if (shape == null) {
return null;
}
if (shape.equals("CIRCLE")) {
return new Circle();
}
if (shape.equals("RECTANGLE")) {
return new Rectangle();
}
if (shape.equals("SQUARE")) {
return new Square();
}
}
@Override
public Shape getColor(String color) {
return null;
}
}
class FactoryProducer {
public static AbstractFactory getFactory(String type) {
if (type.equals("SHAPE")) {
return new shapeFactory();
} else if (type.equals("COLOR")) {
return new colorFactory();
}
return null;
}
}
class AbstractFactoryPatternDemo {
public static void main(String[] args) {
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = shapeFactory.getShape("RECTANGLE");
shape2.draw();
Shape shape3 = shapeFactory.getShape("SQUARE");
shape3.draw();
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
Color color1 = colorFactory.getColor("RED");
color1.fill();
Color color2 = colorFactory.getColor("GREEN");
color2.fill();
Color color3 = colorFactory.getColor("BLUE");
color3.fill();
}
}
UML图: