bridgeパターンのJava実装例

2010/11/02 11:56 Update
TAGS: bridge | OOD | オブジェクト指向 | java | コード | GoF | デザインパターン
package com.test.bridge;

public class Client {

    public static void main(String[] args) {
        SetCarEngine carEngine1500cc = new SetCarEngine1500cc();
        SetCarEngine carEngine2000cc = new SetCarEngine2000cc();
        
        Car truck1500cc = new Truck(carEngine1500cc);
        Car truck2000cc = new Truck(carEngine2000cc);
        
        truck1500cc.setEngine();
        truck2000cc.setEngine();
        
        Car bus1500cc = new Bus(carEngine1500cc);
        Car bus2000cc = new Bus(carEngine2000cc);
        
        bus1500cc.setEngine();
        bus2000cc.setEngine();
    }

}


/** "Abstraction" */

abstract class Car {
    SetCarEngine setCarEngine;
    
    public abstract void setEngine();
}

class Truck extends Car {
    public Truck(SetCarEngine setCarEngine) {
        this.setCarEngine = setCarEngine;
    }
    
    public void setEngine() {
        System.out.print("Set Truck Engine: ");
        
        setCarEngine.setEngine();
    }
}

class Bus extends Car {
    public Bus(SetCarEngine setCarEngine) {
        this.setCarEngine = setCarEngine;
    }
    
    public void setEngine() {
        System.out.print("Set Bus Engine: ");
        setCarEngine.setEngine();
    }
}


/** "Implementor" */

interface SetCarEngine {
    public void setEngine();
}


class SetCarEngine1500cc implements SetCarEngine {
    public void setEngine() {
        System.out.println("1500cc");
    }
}

class SetCarEngine2000cc implements SetCarEngine {
    public void setEngine() {
        System.out.println("2000cc");
    }
}
GoF,デザインパターンの一つ「bridgeパターン」のJava実装例。

bridgeパターンについて
Bridge パターン - ブリッジパターン - オブジェクト指向設計

Sponsored Link


有关作者
Syboos.jp編集長AJavaやオープンソース情報の執筆、Webサイトの開発や運営全般の業務に携わる。

Relative Articles