1.简单工厂模式
根据具体的需求由工厂动态创建相对应的实例,简单工厂模式一般包含单一抽象工厂,单一实例和多个具体实现类。
public interface Shape {
public void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("圆形");
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("矩形");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("方形");
}
}
public class ShapeFactory {
public Shape createShape(String type){
Shape shape = null;
if("circle".equals(type)){
return new Circle();
}else if("rectangle".equals(type)){
return new Rectangle();
}else if("square".equals(type)){
return new Square();
}else{
return shape;
}
}
}
public class Demo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
shapeFactory.createShape("circle").draw();
shapeFactory.createShape("rectangle").draw();
shapeFactory.createShape("square").draw();
}
}
2.工厂方法模式
创建多个子类工厂,在客户端由子类决定由哪一类工厂来创建实例。工厂方法模式一般包含一个抽象工厂,多个具体工厂实现.一个抽象实例,多个具体实例实现,由不同的工厂去创建不同的实例。
public interface Shape {
public void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("圆形");
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("矩形");
}
}
public class Square implements Shape {
@Override
public void draw() {
System.out.println("方形");
}
}
public interface ShapeFactory {
public Shape createShape();
}
public class CircleFactory implements ShapeFactory {
@Override
public Shape createShape() {
return new Circle();
}
}
public class RectangleFactory implements ShapeFactory {
@Override
public Shape createShape() {
return new Rectangle();
}
}
public class SquareFactory implements ShapeFactory {
@Override
public Shape createShape() {
return new Square();
}
}
public class Demo {
public static void main(String[] args) {
ShapeFactory circleFactory = new CircleFactory();
circleFactory.createShape().draw();
ShapeFactory rectangleFactory = new RectangleFactory();
rectangleFactory.createShape().draw();
ShapeFactory squareFactory = new SquareFactory();
squareFactory.createShape().draw();
}
}
3.抽象工厂模式
创建多类子工厂,由子工厂来创建多类实例。抽象工厂模式一般包含一个抽象工厂,多个具体工厂实现,多个抽象实例,多个具体实例实现,形成产品族。如果去掉部分产品,只留下单一抽象产品,那么抽象工厂模式就变成了工厂方法模式。
public interface Button {
public void onClick();
}
public class LinuxBtton implements Button {
@Override
public void onClick() {
System.out.println("Linux事件触发!");
}
}
public class WindowsButton implements Button {
@Override
public void onClick() {
System.out.println("Windows事件触发!");
}
}
public interface Text {
public void getText();
}
public class LinuxText implements Text {
@Override
public void getText() {
System.out.println("Linux文本!");
}
}
public class WindowsText implements Text {
@Override
public void getText() {
System.out.println("Windows文本!");
}
}
public interface AbstractFactory {
public Text createText();
public Button createButton();
}
public class LinuxFactory implements AbstractFactory {
@Override
public Text createText() {
return new LinuxText();
}
@Override
public Button createButton() {
return new LinuxBtton();
}
}
public class WindowsFactory implements AbstractFactory {
@Override
public Text createText() {
return new WindowsText();
}
@Override
public Button createButton() {
return new WindowsButton();
}
}
public class Demo {
public static void main(String[] args) {
AbstractFactory linuxFactory = new LinuxFactory();
linuxFactory.createButton().onClick();
linuxFactory.createText().getText();
AbstractFactory windowsFactory = new WindowsFactory();
windowsFactory.createButton().onClick();
windowsFactory.createText().getText();
}
}