工廠設計模式
工廠模式是用工廠方法代替new操作的一種模式。
在創建對象時不會對客戶端暴露創建邏輯,並且是通過使用一個共同的接口來指向新創建的對象。
3種:簡單工廠模式、工廠方法模式、抽象工廠模式。
適用場景
(1)在編碼時不能預見需要創建哪種類的實例。
(2)系統不應依賴於產品類實例如何被創建、組合和表達的細節
(3)Spring框架的IOC就是使用的工廠模式
優點
(1)一個調用者想創建一個對象,只要知道其名稱就可以了
(2)擴展性高,如果想增加一個產品,只要擴展一個工廠類就可以
(3)屏蔽產品的具體實現,調用者只關心產品的接口。
缺點
每次增加一個產品時,都需要增加一個具體類和對象實現工廠,使得系統中類的個數成倍增加,在一定程度上增加了系統的複雜度,同時也增加了系統具體類的依賴。這並不是什麼好事。
1. 簡單工廠模式:
通過專門定義一個類來負責創建其他類的實例
結構如下:
代碼如下:
package com.revision.DesignMethod;
public class EasyFactory {
public static Operation createOperate(String operate) {
Operation oper = null;
switch (operate) {
case "+":
oper = new OperationAdd();
break;
case "-":
oper = new OperationSub();
break;
case "*":
oper = new OperationMul();
break;
case "/":
oper = new OperationDiv();
break;
default:
return null;
}
return oper;
}
}
class Operation {
private int i, j;
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public int getJ() {
return j;
}
public void setJ(int j) {
this.j = j;
}
public double getRes() {
return 0;
}
}
class OperationAdd extends Operation {
@Override
public double getRes() {
return this.getI() + this.getJ();
}
}
class OperationSub extends Operation {
@Override
public double getRes() {
return this.getI() - this.getJ();
}
}
class OperationMul extends Operation {
@Override
public double getRes() {
return this.getI() * this.getJ();
}
}
class OperationDiv extends Operation {
@Override
public double getRes() {
if (this.getJ() == 0) {
System.out.println("除數不能爲0!");
return 0;
} else {
return this.getI() / this.getJ();
}
}
}
class User{
public static void main(String[] args) {
Operation operation = null;
operation = EasyFactory.createOperate("+");
operation.setI(1);
operation.setJ(2);
System.out.println(operation.getRes());
}
}
2. 工廠方法模式
定義一個用於創建對象的接口,讓子類決定實例化哪一個類。
結構如下:
先定義抽象接口,再創建抽象產品類,創建具體產品類,創建具體工廠類。外界通過調用具體工廠類的方法,從而創建不同具體產品類的實例。
代碼如下:(邏輯重複的部分並沒有寫,具體可以參照上面的代碼)
package com.revision.DesignMethod.factorydesign;
interface IFactory{
Operation createOperation();
}
public class AbstractFactory {
public static void main(String[] args) {
}
}
class AddFactory implements IFactory{
@Override
public Operation createOperation() {
return new OperationAdd();
}
}
class SubFactory implements IFactory{
@Override
public Operation createOperation() {
return new OperationSub();
}
}
class MulFactory implements IFactory{
@Override
public Operation createOperation() {
return new OperationMul();
}
}
class DivFactory implements IFactory{
@Override
public Operation createOperation() {
return new OperationDiv();
}
}
3. 抽象工廠模式
提供一個創建一系列相關或相互依賴對象的接口,而無需指定它們具體的類。
【好處】
①便於交換產品系列
②讓具體的創建實例過程與客戶端分離
結構如圖:
先創建接口,如一個形狀接口一個顏色接口,再創建接口實現類,如各種形狀和各種顏色,覆寫了這兩個接口的塗色方法和定型方法。然後創建一個抽象類,用來定義獲取顏色和形狀的方法,然後定義工廠繼承這個抽象類,覆寫需要的代碼,並提供返回什麼形狀或者什麼顏色的示例。
代碼如下:
package com.revision.DesignMethod.factorydesign;
interface Shape {
void draw();
}
interface Color{
void fill();
}
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Square");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle");
}
}
class Red implements Color{
@Override
public void fill() {
System.out.println("red");
}
}
class Green implements Color{
@Override
public void fill() {
System.out.println("green");
}
}
class Blue implements Color{
@Override
public void fill() {
System.out.println("blue");
}
}
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType == null){
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;
}
@Override
public Color getColor(String color) {
return null;
}
}
class ColorFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
return null;
}
@Override
public Color getColor(String color) {
if(color == null){
return null;
}
if(color.equalsIgnoreCase("RED")){
return new Red();
} else if(color.equalsIgnoreCase("GREEN")){
return new Green();
} else if(color.equalsIgnoreCase("BLUE")){
return new Blue();
}
return null;
}
}
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;
}
}
class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//獲取形狀工廠
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//獲取形狀爲 Circle 的對象
Shape shape1 = shapeFactory.getShape("CIRCLE");
//調用 Circle 的 draw 方法
shape1.draw();
//獲取形狀爲 Rectangle 的對象
Shape shape2 = shapeFactory.getShape("RECTANGLE");
//調用 Rectangle 的 draw 方法
shape2.draw();
//獲取形狀爲 Square 的對象
Shape shape3 = shapeFactory.getShape("SQUARE");
//調用 Square 的 draw 方法
shape3.draw();
//獲取顏色工廠
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//獲取顏色爲 Red 的對象
Color color1 = colorFactory.getColor("RED");
//調用 Red 的 fill 方法
color1.fill();
//獲取顏色爲 Green 的對象
Color color2 = colorFactory.getColor("Green");
//調用 Green 的 fill 方法
color2.fill();
//獲取顏色爲 Blue 的對象
Color color3 = colorFactory.getColor("BLUE");
//調用 Blue 的 fill 方法
color3.fill();
}
}