1. 程式人生 > >設計模式-建立型模式-工廠模式,抽象工廠模式

設計模式-建立型模式-工廠模式,抽象工廠模式

簡單工廠模式

簡單工廠模式不是 23 種裡的一種,簡而言之,就是有一個專門生產某個產品的類。

它只算工廠模式的一個特殊實現。簡單工廠模式在實際中的應用相對於其他2個工廠模式用的還是相對少得多,因為它只適應很多簡單的情況。

簡單工廠例項

1)建立Shape介面
public interface Shape {
    void draw();
}

(2)建立實現該介面的具體圖形類

圓形
public class Circle implements Shape {
    public Circle() {
        System.out.println("Circle");
    }
    @Override
    public void draw() {
        System.out.println("Draw Circle");
    }
}

長方形
public class Rectangle implements Shape {
    public Rectangle() {
        System.out.println("Rectangle");
    }
    @Override
    public void draw() {
        System.out.println("Draw Rectangle");
    }
}

(3)建立工廠類:


public class ShapeFactory {

    // 使用 getShape 方法獲取形狀型別的物件
    public static 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;
    }
}

(4)測試方法:


public class Test {

    public static void main(String[] args) {

        // 獲取 Circle 的物件,並呼叫它的 draw 方法
        Shape circle = ShapeFactory.getShape("CIRCLE");
        circle.draw();

        // 獲取 Rectangle 的物件,並呼叫它的 draw 方法
        Shape rectangle = ShapeFactory.getShape("RECTANGLE");
        rectangle.draw();

        // 獲取 Square 的物件,並呼叫它的 draw 方法
        Shape square = ShapeFactory.getShape("SQUARE");
        square.draw();
    }
}

輸出結果:

Circle
Draw Circle
Rectangle
Draw Rectangle
Square
Draw Square

如果我們這時要增加一個產品,就需要修改工廠類中的方法,不符合開放-封閉的原則。

工廠模式

工廠模式也就是滑鼠工廠是個父類,有生產滑鼠這個介面。

戴爾滑鼠工廠,惠普滑鼠工廠繼承它,可以分別生產戴爾滑鼠,惠普滑鼠。

生產哪種滑鼠不再由引數決定,而是建立滑鼠工廠時,由戴爾滑鼠工廠建立。

後續直接呼叫滑鼠工廠.生產滑鼠()即可

(1)增加一個工廠介面:

public interface Factory {
    public Shape getShape();
}

(2)增加相關工廠類:

圓形工廠類

public class CircleFactory implements Factory {

    @Override
    public Shape getShape() {
        // TODO Auto-generated method stub
        return new Circle();
    }

}

長方形工廠類

public class RectangleFactory implements Factory{

    @Override
    public Shape getShape() {
        // TODO Auto-generated method stub
        return new Rectangle();
    }

}
圓形工廠類

public class SquareFactory implements Factory{

    @Override
    public Shape getShape() {
        // TODO Auto-generated method stub
        return new Square();
    }

}
(3)測試:

public class Test {

    public static void main(String[] args) {
        Factory circlefactory = new CircleFactory();
        Shape circle = circlefactory.getShape();
        circle.draw();
    }

}


輸出結果:

Circle
Draw Circle
 

抽象工廠模式

抽象工廠模式也就是不僅生產滑鼠,同時生產鍵盤。

也就是 PC 廠商是個父類,有生產滑鼠,生產鍵盤兩個介面。

戴爾工廠,惠普工廠繼承它,可以分別生產戴爾滑鼠+戴爾鍵盤,和惠普滑鼠+惠普鍵盤。

建立工廠時,由戴爾工廠建立。

後續工廠.生產滑鼠()則生產戴爾滑鼠,工廠.生產鍵盤()則生產戴爾鍵盤。

(1)建立相關介面:

public interface Gun {
    public void shooting();
}

子彈

public interface Bullet {
    public void load();
}

(2)建立介面對應實現類:

AK類

public class AK implements Gun{

    @Override
    public void shooting() {
        System.out.println("shooting with AK");

    }

}

M4A1類

public class M4A1 implements Gun {

    @Override
    public void shooting() {
        System.out.println("shooting with M4A1");

    }

}

AK子彈類

public class AK_Bullet implements Bullet {

    @Override
    public void load() {
        System.out.println("Load bullets with AK");
    }

}

M4A1子彈類

public class M4A1
_Bullet implements Bullet {

    @Override
    public void load() {
        System.out.println("Load bullets with M4A1");
    }

}

(3)建立工廠介面

public interface Factory {
    public Gun produceGun();
    public Bullet produceBullet();
}

(4)建立具體工廠

生產AK和AK子彈的工廠

public class AK_Factory implements Factory{

    @Override
    public Gun produceGun() {
        return new AK();
    }

    @Override
    public Bullet produceBullet() {
        return new AK_Bullet();
    }

}

生產M4A1和M4A1子彈的工廠

public class M4A1_Factory implements Factory{

    @Override
    public Gun produceGun() {
        return new M4A1();
    }

    @Override
    public Bullet produceBullet() {
        return new M4A1_Bullet();
    }

}

(5)測試

public class Test {

    public static void main(String[] args) {  

     Factory factory;
     Gun gun;
     Bullet bullet;

     factory =new AK_Factory();
     bullet=factory.produceBullet();
     bullet.load();
     gun=factory.produceGun();
     gun.shooting(); 

    }

}

輸出結果:

Load bullets with AK
shooting with AK