設計模式之工廠模式

1、什麼是工廠模式

實現了建立者和調用者分離,工廠模式分爲簡單工廠、工廠方法、抽象工廠模式優化

2、工廠模式好處

工廠模式是咱們最經常使用的實例化對象模式了,是用工廠方法代替new操做的一種模式。利用工廠模式能夠下降程序的耦合性,爲後期的維護修改提供了很大的便利。將選擇實現類、建立對象統一管理和控制。從而將調用者跟咱們的實現類解耦。spa

3、工廠模式分類

一、簡單工廠模式

簡單工廠模式至關因而一個工廠中有各類產品,建立在一個類中,客戶無需知道具體產品的名稱,只須要知道產品類所對應的參數便可。可是工廠的職責太重,並且當類型過多時不利於系統的擴展維護。code

public interface Car {
    public void run();
}複製代碼

public class AoDi implements Car {
    public void run() {
        System.out.println("我是奧迪汽車..");
    }
}複製代碼

public class JiLi implements Car {

    public void run() {
        System.out.println("我是吉利汽車...");
    }
}複製代碼

public class CarFactory {

     public static Car createCar(String name) {
        if (StringUtils.isEmpty(name)) {
             return null;
        }
        if(name.equals("奧迪")){
            return new AoDi();
        }
        if(name.equals("吉利")){
            return new JiLi();
        }
        return null;
    }
}複製代碼

public class Client01 {

    public static void main(String[] args) {
        Car aodi  =CarFactory.createCar("奧迪");
        Car jili  =CarFactory.createCar("吉利");
        aodi.run();
        jili.run();
    }

}複製代碼

簡單工廠的優勢/缺點

  • 優勢:簡單工廠模式可以根據外界給定的信息,決定究竟應該建立哪一個具體類的對象。明確區分了各自的職責和權力,有利於整個軟件體系結構的優化。
  • 缺點:很明顯工廠類集中了全部實例的建立邏輯,容易違反GRASPR的高內聚的責任分配原則

二、工廠方法模式

file工廠方法模式Factory Method,又稱多態性工廠模式。在工廠方法模式中,核心的工廠類再也不負責全部的產品的建立,而是將具體建立的工做交給子類去作。該核心類成爲一個抽象工廠角色,僅負責給出具體工廠子類必須實現的接口,而不接觸哪個產品類應當被實例化這種細節。cdn

public interface Car {
    public void run();
}複製代碼
public interface CarFactory {
    public Car createCar();
}複製代碼

public class AoDi implements Car {
    public void run() {
        System.out.println("我是奧迪汽車..");
    }
}複製代碼

public class JiLi implements Car {

    public void run() {
        System.out.println("我是吉利汽車...");
    }
}複製代碼

public class JiLiFactory implements CarFactory {

    public Car createCar() {

        return new JiLi();
    }

}複製代碼

public class AoDiFactory implements CarFactory {

    public Car createCar() {
    
        return new AoDi();
    }
}複製代碼

public class Client {

    public static void main(String[] args) {
        Car aodi = new AoDiFactory().createCar();
        Car jili = new JiLiFactory().createCar();
        aodi.run();
        jili.run();
    }

}複製代碼

三、抽象工廠模式

抽象工廠簡單地說是工廠的工廠,抽象工廠能夠建立具體工廠,由具體工廠來產生具體產品。至關於零件工廠和組裝工廠。對象

filefile

//發動機
public interface Engine {

    void run();

    void start();
}

class EngineA implements Engine {

    public void run() {
      System.out.println("轉的快!");
    }

    public void start() {
         System.out.println("啓動快,自動檔");
    }

}

class EngineB implements Engine {

    public void run() {
      System.out.println("轉的慢!");
    }

    public void start() {
         System.out.println("啓動快,手動檔");
    }

}複製代碼

//座椅
public interface Chair {
       void run();
}

 class ChairA implements Chair{

    public void run() {
        System.out.println("能夠自動加熱!");
    }
    
}
 class ChairB implements Chair{

    public void run() {
        System.out.println("不能加熱!");
    }
    
}複製代碼

public interface CarFactory {
    // 建立發動機
    Engine createEngine();
    // 建立座椅
    Chair createChair();
}複製代碼

public class JiLiFactory implements CarFactory  {

    public Engine createEngine() {
    
        return new EngineA();
    }

    public Chair createChair() {
        
        return new ChairA();
    }

}複製代碼

public class Client002 {

     public static void main(String[] args) {
        CarFactory carFactory=new JiLiFactory();
        // 現組裝一個發動機
        Engine engine=carFactory.createEngine();
        engine.run();
        engine.start();
        // 再組裝一個座椅
        ChairA chairA=carFactory.createChair();
        chairA.run();

    }
    
}複製代碼

我的博客 蝸牛blog

相關文章
相關標籤/搜索