概要
抽象產品類java
/** * @author cuishifeng * @create 2018-08-02 **/
public interface Computer {
/** * 生產電腦 */
void createComputer();
}
複製代碼
具體產品git
/** * @author cuishifeng * @create 2018-08-02 **/
public class Cpu implements Computer {
@Override
public void createComputer() {
System.out.println("生產cpu");
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class Disk implements Computer {
@Override
public void createComputer() {
System.out.println("生產磁盤disk");
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class Displayer implements Computer {
@Override
public void createComputer() {
System.out.println("生產顯卡displayer");
}
}
複製代碼
抽象工廠類github
/** * @author cuishifeng * @create 2018-08-02 **/
public interface ComputerFactory {
/** * 工廠方法模式的抽象工廠 * @return */
Computer getProduct();
}
複製代碼
具體工廠ide
/** * @author cuishifeng * @create 2018-08-02 **/
public class CpuFactory implements ComputerFactory {
@Override
public Computer getProduct() {
return new Cpu();
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class DiskFactory implements ComputerFactory{
@Override
public Computer getProduct() {
return new Disk();
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class DisplayerFactory implements ComputerFactory {
@Override
public Computer getProduct() {
return new Displayer();
}
}
複製代碼
/** * 客戶端 * @author cuishifeng * @create 2018-08-02 **/
public class AppClient {
public static void main(String[] args) {
ComputerFactory computerFactory = new CpuFactory();
computerFactory.getProduct().createComputer();
System.out.println("---------------------------- ");
computerFactory = new DiskFactory();
computerFactory.getProduct().createComputer();
System.out.println("---------------------------- ");
computerFactory = new DisplayerFactory();
computerFactory.getProduct().createComputer();
}
}
複製代碼
打印結果ui
生產cpu
----------------------------
生產磁盤disk
----------------------------
生產顯卡displayer
Process finished with exit code 0
複製代碼
概要
1.擴展產品族至關麻煩 並且擴展產品族會違反OCP,由於要修改全部的工廠,例如咱們有電腦和鼠標組成的一個產品族,咱們寫完代碼再去添加一個鍵盤就會很麻煩,看完下面代碼就會理解了spa
2.因爲抽象工廠模式是工廠方法模式的擴展 整體的來講 很笨重.net
抽象產品code
/** * 計算機-抽象產品類 * @author cuishifeng * @create 2018-08-02 **/
public abstract class Computer {
/** * 生產計算機 */
public abstract void productComputer();
}
複製代碼
/** * 鼠標 - 抽象產品類 * @author cuishifeng * @create 2018-08-02 **/
public abstract class Mouse {
/** * 生產鼠標 */
public abstract void productMouse();
}
複製代碼
具體產品orm
電腦的具體產品cdn
/** * PC計算機具體產品類 * @author cuishifeng * @create 2018-08-02 **/
public class PcComputer extends Computer {
@Override
public void productComputer() {
System.out.println("PC端計算機");
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class ServerComputer extends Computer {
@Override
public void productComputer() {
System.out.println("Server端計算機");
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class GameMouse extends Mouse {
@Override
public void productMouse() {
System.out.println("遊戲鼠標");
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class OfficeMouse extends Mouse {
@Override
public void productMouse() {
System.out.println("辦公鼠標");
}
}
複製代碼
抽象工廠
/** * @author cuishifeng * @create 2018-08-02 **/
public interface IFactory {
/** * 獲取計算機 * @return */
Computer getComputer();
/** * 獲取鼠標 * @return */
Mouse getMouse();
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class ProductFactoryA implements IFactory {
@Override
public Computer getComputer() {
return new PcComputer();
}
@Override
public Mouse getMouse() {
return new OfficeMouse();
}
}
複製代碼
/** * @author cuishifeng * @create 2018-08-02 **/
public class ProductFactoryB implements IFactory {
@Override
public Computer getComputer() {
return new ServerComputer();
}
@Override
public Mouse getMouse() {
return new GameMouse();
}
}
複製代碼
概要
1.添加具體產品須要修改工廠 違反OCP開放封閉原則
抽象產品類
/** * @author cuishifeng * @create 2018-07-09 **/
public interface Car {
/** * 我有一輛什麼車 */
void myCar();
}
複製代碼
具體產品
/** * @author cuishifeng * @create 2018-07-09 **/
public class BenChiCar implements Car {
@Override
public void myCar() {
System.out.println("我有一輛奔馳車!");
}
}
複製代碼
public class FerrariCar implements Car {
@Override
public void myCar() {
System.out.println("我有一輛法拉利!");
}
}
複製代碼
public class LamborghiniCar implements Car {
@Override
public void myCar() {
System.out.println("我有一輛蘭博基尼!");
}
}
複製代碼
工廠類
public class CarFactory {
public static Car createCar(String carName){
if (carName == null){
return null;
}
switch (carName){
case "BenChiCar":
return new BenChiCar();
case "FerrariCar":
return new FerrariCar();
case "LamborghiniCar":
return new LamborghiniCar();
default:
return null;
}
}
}
複製代碼
Car car = CarFactory.createCar("BenChiCar");
car.myCar();
Car car2 = CarFactory.createCar("FerrariCar");
car2.myCar();
輸出
我有一輛奔馳車!
我有一輛法拉利!
複製代碼
簡單工廠模式最大的有點就是工廠內有具體的邏輯去判斷生成什麼產品,將類的實例化交給了工廠,這樣當咱們須要什麼產品只須要修改客戶端的調用而不須要去修改工廠,對於客戶端來講下降了與具體產品的依賴
工廠方法模式是簡單工廠的擴展,工廠方法模式把原先簡單工廠中的實現那個類的邏輯判斷交給了客戶端,若是像添加功能只須要修改客戶和添加具體的功能,不用去修改以前的類。
抽象工廠模式進一步擴展了工廠方法模式,它把原先的工廠方法模式中只能有一個抽象產品不能添加產品族的缺點克服了,抽象工廠模式不單單遵循了OCP原則(對擴展開放,對修改關閉),並且能夠添加更多產品(抽象產品),具體工廠也不單單能夠生成單一產品,而是生成一組產品,抽象工廠也是聲明一組產品,對應擴展更加靈活,可是要是擴展族系就會很笨重。