1. 创建型模式 (4) 工厂模式

阅读更多
1. 创建型模式 (4) 工厂模式

它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。[/b]

有一点需要注意的地方就是复杂对象适合使用工厂模式,而简单对象,特别是只需要通过new就可以完成创建的对象,无需使用工厂模式。

工厂模式一般分为三类:
  1. 简单工厂模式(Simple Factory)
  2. 工厂方法模式(Factory Method)
  3. 抽象工厂模式(Abstract Factory)
《设计模式》一书中将工厂模式分为两类:工厂方法模式(Factory Method)与抽象工厂模式(Abstract Factory)。
将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类。

工厂模式组成:
  1. 抽象产品角色:它一般是具体产品继承的父类或者实现的接口。         
  2. 具体产品角色:工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。
  3. 工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑,用来创建产品。


1. 简单工厂模式

package com.andrew.pattern0104.factory.modal01;
public class Circle {
    public void draw() {
        System.out.println("Inside Circle draw method.");
    }
}

package com.andrew.pattern0104.factory.modal01;
public class Rectangle {
    public void draw() {
        System.out.println("Inside Rectangle draw method.");
    }
}

package com.andrew.pattern0104.factory.modal01;
public class Square {
    public void draw() {
        System.out.println("Inside Square draw method.");
    }
}


package com.andrew.pattern0104.factory.modal01;
/**
 * 1. 简单工厂模式
 * 
 * @author andrew
 * @date 2018/06/12
 */
public class FactoryClient {
    public static void main(String[] args) {
        Circle shape1 = new Circle();
        shape1.draw();
        Rectangle shape2 = new Rectangle();
        shape2.draw();
        Square shape3 = new Square();
        shape3.draw();
    }
}


2. 标准工厂方法模式

package com.andrew.pattern0104.factory.modal02;
public interface Shape {
    void draw();
}


package com.andrew.pattern0104.factory.modal02;
public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Rectangle draw method.");
    }
}

package com.andrew.pattern0104.factory.modal02;
public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square draw method.");
    }
}

package com.andrew.pattern0104.factory.modal02;
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle draw method.");
    }
}


package com.andrew.pattern0104.factory.modal02;
public class ShapeFactory {
    public Shape getShape(String shapeType) {
        if (null == shapeType) {
            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;
    }
}


package com.andrew.pattern0104.factory.modal02;
/**
 * 2. 标准工厂模式
 * 
 * @author andrew
 * @date 2018/06/12
 */
public class FactoryClient {
    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();
    }
}
运行结果:
Inside Circle draw method.
Inside Rectangle draw method.
Inside Square draw method.


3. 标准工厂方法模式

package com.andrew.pattern0104.factory.modal03;
public interface Shape {
    void draw();
}


package com.andrew.pattern0104.factory.modal03;
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Circle draw method.");
    }
}

package com.andrew.pattern0104.factory.modal03;
public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Rectangle draw method.");
    }
}


package com.andrew.pattern0104.factory.modal03;
public class Square implements Shape {
    @Override
    public void draw() {
        System.out.println("Inside Square draw method.");
    }
}


package com.andrew.pattern0104.factory.modal03;
public interface ShapeFactory {
    Shape getShape();
}

package com.andrew.pattern0104.factory.modal03;
public class CircleFactory implements ShapeFactory {
    @Override
    public Circle getShape() {
        return new Circle();
    }
}

package com.andrew.pattern0104.factory.modal03;
public class RectangleFactory implements ShapeFactory {
    @Override
    public Rectangle getShape() {
        return new Rectangle();
    }
}

package com.andrew.pattern0104.factory.modal03;
public interface ShapeFactory {
    Shape getShape();
}


package com.andrew.pattern0104.factory.modal03;
/**
 * 3. 严格工厂模式
 * 
 * @author andrew
 * @date 2018/06/12
 */
public class FactoryClient {
    public static void main(String[] args) {
        CircleFactory circleFactory = new CircleFactory();
        Circle circle = circleFactory.getShape();
        circle.draw();
        RectangleFactory rectangleFactory = new RectangleFactory();
        Rectangle rectangle = rectangleFactory.getShape();
        rectangle.draw();
        SquareFactory squareFactory = new SquareFactory();
        Square square = squareFactory.getShape();
        square.draw();
    }
}
运行结果:
Inside Circle draw method.
Inside Rectangle draw method.
Inside Square draw method.

你可能感兴趣的:(java,设计模式)