1. 程式人生 > >設計模式:抽象工廠-abstract factory

設計模式:抽象工廠-abstract factory

提供一個建立一系列相關或者相互依賴物件的介面,而無須指定他們具體的類

這裡寫圖片描述

優點:

 良好的封裝性:每個產品類與高層之間的關係不需要被client知道,client只需要知道工廠類能夠創建出一個需要的物件即可

 產品族內的約束關係為非公開的狀態 

缺點:

 產品族擴充套件較難,產品等級擴充套件容易 

程式碼

Main

public class Main {

    public static void main(String[] args) {
        DoorFactory ldf=new LeftDoorFactory();
        DoorFactory rdf=new
RightDoorFactory(); CarDoor audiLeftDoor=ldf.getAUDIDoor(); audiLeftDoor.getColor(); audiLeftDoor.getDir(); CarDoor bmwRightDoor=rdf.getBMWDoor(); bmwRightDoor.getColor(); bmwRightDoor.getDir(); DoorFactory bdf=new BackDoorFactory(); CarDoor bmwBackDoor=bdf.getBMWDoor(); bmwBackDoor.getColor(); bmwBackDoor.getDir(); } }

interface:CarDoor

public interface CarDoor {
    public void getColor();
    public void getDir();
}

interface:DoorFactory

public interface DoorFactory {
    public CarDoor getBMWDoor();
    public CarDoor getAUDIDoor();
    public CarDoor getTOYOTADoor();
}

imp:BackDoorFactory

public class BackDoorFactory
implements DoorFactory {
@Override public CarDoor getBMWDoor() { return new BMWBackDoor(); } @Override public CarDoor getAUDIDoor() { // TODO Auto-generated method stub return null; } @Override public CarDoor getTOYOTADoor() { // TODO Auto-generated method stub return null; } }

imp:LeftDoorFactory

public class LeftDoorFactory implements DoorFactory {

    @Override
    public CarDoor getBMWDoor() {
        return new BMWLeftDoor();
    }

    @Override
    public CarDoor getAUDIDoor() {
        return new AUDILeftDoor();
    }

    @Override
    public CarDoor getTOYOTADoor() {
        return new TOYOTALeftDoor();
    }

}

imp:RightDoorFactory

public class RightDoorFactory implements DoorFactory {

    @Override
    public CarDoor getBMWDoor() {
        return new BMWRightDoor();
    }

    @Override
    public CarDoor getAUDIDoor() {
        return new AUDIRightDoor();
    }

    @Override
    public CarDoor getTOYOTADoor() {
        return new TOYOTARightDoor();
    }

}

imp:AUDIDoor

public class AUDIDoor implements CarDoor {

    @Override
    public void getColor() {
        System.out.println("奧迪一直都是黑色的");
    }

    @Override
    public void getDir() {
        // TODO Auto-generated method stub

    }

}

imp:BMWDoor

public class BMWDoor implements CarDoor {

    @Override
    public void getColor() {
        System.out.println("寶馬的門向來都是紅色的");
    }

    @Override
    public void getDir() {
        // TODO Auto-generated method stub

    }

}

imp:TOYOTADoor

public class TOYOTADoor implements CarDoor {

    @Override
    public void getColor() {
        System.out.println("豐田一直都是白色的");
    }

    @Override
    public void getDir() {
        // TODO Auto-generated method stub

    }

}

AUDILeftDoor

public class AUDILeftDoor extends AUDIDoor {
    @Override
    public void getDir() {
        System.out.println("這是左門");
    }
}

AUDIRightDoor

public class AUDIRightDoor extends AUDIDoor {

    @Override
    public void getDir() {
        System.out.println("這是右門");
    }
}

BMWLeftDoor

public class BMWLeftDoor extends BMWDoor {
    @Override
    public void getDir() {
        System.out.println("這是作門");
    }
}

BMWRightDoor

public class BMWRightDoor extends BMWDoor {

    @Override
    public void getDir() {

        System.out.println("這是右門");
    }
}

BMWBackDoor

public class BMWBackDoor extends BMWDoor {

    @Override
    public void getDir() {
        System.out.println("這是寶馬的尾門");
    }
}

TOYOTALeftDoor

public class TOYOTALeftDoor extends TOYOTADoor {
    @Override
    public void getDir() {
        System.out.println("這是左門");
    }
}

TOYOTARightDoor

public class TOYOTARightDoor extends TOYOTADoor {

    @Override
    public void getDir() {
        System.out.println("這是右門");
    }
}