java原廠模式Factory

來源:互聯網
上載者:User

標籤:static   body   null   factor   sdi   圖片   com   get   整理   

 原廠模式:

1)我們首先要設計車汽車需要那些流程(face)

package light.zhang.model.model.facotry;/** * 車子建造過程,定義好流程 * @author witts * */public interface ICarface {    /**     * 繪製圖片     */    void drawImage();    /**     * 設計模型     */    void createModel();    /**     * 實現生產     */    void implCar();    /**     * 測試車子     */    void testCar(); }

2.汽車生產商(實現)

package light.zhang.model.model.facotry;/** * 奧迪生廠商 * @author witts * */public class AodiCompany  implements ICarface {     @Override    public void drawImage() {         System.out.println("奧迪公司-圖片模型");    }    @Override    public void createModel() {         System.out.println("奧迪公司-建立模型");    }    @Override    public void implCar() {        System.out.println("奧迪公司-生產車體");    }    @Override    public void testCar() {         System.out.println("奧迪公司-測試車體");    }         }

package light.zhang.model.model.facotry;

/**
 * 寶馬公司
 * @author witts
 */
public class BMWCompany implements ICarface {
    @Override
    public void drawImage() {
        System.out.println("寶馬公司-圖片模型");
    }

    @Override
    public void createModel() {
        System.out.println("寶馬公司-建立模型");
    }

    @Override
    public void implCar() {
        System.out.println("寶馬公司-生產車體");
    }

    @Override
    public void testCar() {
        System.out.println("寶馬公司-測試車體");
    }
 
}

3)4S店現在要整理奧迪寶馬的車,4S店就是一個工廠,他負責把奧迪寶馬的汽車整理,使用者去買車只需要告訴銷售人員我要買什麼車

package light.zhang.model.model.facotry;/** * 4S店開始整理要銷售那些車? * @author witts * */public class Car4SDianFactory {        public ICarface getCar(int type) {         if(type == 1) {             return new AodiCompany();         }         if(type == 2) {             return new BMWCompany();         }         return null;    }}

4)使用者開始買車(調用)

package light.zhang.model.model.facotry;/** * 使用者開始去4S店買車 * @author witts * */public class UserShoppingCar {        public static void main(String[] args) {        Car4SDianFactory factory=new Car4SDianFactory();        ICarface aodiCar=factory.getCar(1);//奧迪公司的汽車        aodiCar.testCar();                ICarface bmwCar =  factory.getCar(2);//寶馬公司的汽車        bmwCar.testCar();    }}

 

java原廠模式Factory

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.