Java設計モード-ブリッジモード(Bridge)


ブリッジモード(Bridge)は抽象クラスに注目した設計モードである.ブリッジモードの意図は,抽象クラスと抽象法の実装を互いに分離してデカップリングを実現し,両者が互いに独立して変化できるようにすることである.
     例えば、ラーメン屋のうどんは、辛いものが好きな人もいれば、薄いものが好きな人もいます.牛肉麺が好きな人もいれば、三鮮麺が好きな人もいます.どんな麺でも麺です.しかし、それぞれの麺の味は個人の好みで調節することができます.このシーンはブリッジモードでうまく解決できます.
/**
 *       
 * @author Lenovo
 * @version $Id: Noodle.java, v 0.1 2014 9 26    5:26:06 Lenovo Exp $
 */
public abstract class Noodle {

    /**
     *        
     */
    private Style style;

    public Noodle(Style style) {
        super();
        this.style = style;
    }

    public void eat() {
        System.out.println("  " + style.getPreference() + "     " + getName() + "   ");
    }

    /**
     *     ,       
     *
     * @return
     */
    protected abstract String getName();

    /**
     * Getter method for property <tt>style</tt>.
     *
     * @return property value of style
     */
    public Style getStyle() {
        return style;
    }

    /**
     * Setter method for property <tt>style</tt>.
     *
     * @param style value to be assigned to property style
     */
    public void setStyle(Style style) {
        this.style = style;
    }

}
/**
 *
 * @author Lenovo
 * @version $Id: Style.java, v 0.1 2014 9 26    5:24:35 Lenovo Exp $
 */
public interface Style {

    /**
     *        
     *
     * @return
     */
    public String getPreference();

}

 現在、麺屋では麺2種類、牛肉麺、三鮮麺を提供しています
/**
 *
 * @author Lenovo
 * @version $Id: BeefNoodle.java, v 0.1 2014 9 26    5:35:20 Lenovo Exp $
 */
public class BeefNoodle extends Noodle {

    /**
     * @param style
     */
    public BeefNoodle(Style style) {
        super(style);
    }

    /**
     * @see com.cathy.demo.pattern.bridge.Noodle#getName()
     */
    @Override
    protected String getName() {
        return "  ";
    }

}

/**
 *
 * @author Lenovo
 * @version $Id: SeafoodNoodle.java, v 0.1 2014 9 26    5:36:33 Lenovo Exp $
 */
public class SeafoodNoodle extends Noodle {

    /**
     * @param style
     */
    public SeafoodNoodle(Style style) {
        super(style);
    }

    /**
     * @see com.cathy.demo.pattern.bridge.Noodle#getName()
     */
    @Override
    protected String getName() {
        return "  ";
    }

}

 
/**
 *
 * @author Lenovo
 * @version $Id: HotStyle.java, v 0.1 2014 9 26    5:37:38 Lenovo Exp $
 */
public class HotStyle implements Style {

    /**
     * @see com.cathy.demo.pattern.bridge.Style#getPreference()
     */
    public String getPreference() {
        return " ";
    }

}

/**
 *
 * @author Lenovo
 * @version $Id: LightStyle.java, v 0.1 2014 9 26    5:38:18 Lenovo Exp $
 */
public class LightStyle implements Style {

    /**
     * @see com.cathy.demo.pattern.bridge.Style#getPreference()
     */
    public String getPreference() {
        return "  ";
    }

}

 テスト:
/**
 *
 * @author Lenovo
 * @version $Id: Client.java, v 0.1 2014 9 26    5:36:54 Lenovo Exp $
 */
public class Client {

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        Noodle n = new BeefNoodle(new HotStyle());
        n.eat();
        n = new BeefNoodle(new LightStyle());
        n.eat();

        n = new SeafoodNoodle(new HotStyle());

        n.eat();

        n = new SeafoodNoodle(new LightStyle());

        n.eat();
    }

}

 結果は次のとおりです.
辛い牛肉麺を食べてあっさりした牛肉麺を食べて辛い三鮮麺を食べてあっさりした三鮮麺を食べます