大话设计模式-桥接模式-2020-10-26

2020-10-26  本文已影响0人  勇往直前888

定义

将抽象部分与它的实现部分分离,使它们都可以独立地变化。

结构图

image.png

合成/聚合复用原则

尽量使用合成/聚合,尽量不要使用类继承。

image.png

合成/聚合复用原则的好处是,优先使用对象的合成/聚合将有助于你保持每个类并封装,并被集中在单个任务上。这样类和类继承层次会保持较小规模,并且不太可能增长为不可控制的庞然大物。

手机软硬分离的例子

桥接模式

手机品牌是硬件,采用聚合模式,手机软件作为手机硬件的成员,实现解耦。

软硬耦合的方式

通过对比,采用继承方式的“软硬不分”的方式耦合过大,不利于扩展。而通过聚合方式实现的“软硬分离”的桥接方式,实现了解耦,软硬两边的扩展都非常方便。

/**
 * 手机软件
 */
abstract class HandsetSoft {
    public abstract String run();
}
/**
 * 手机游戏
 */
class HandsetGame extends HandsetSoft {
    @Override
    public String run() {
        return "运行手机游戏";
    }
}

/**
 * 手机通讯录
 */
class HandsetAddressList extends HandsetSoft {
    @Override
    public String run() {
        return "运行手机通讯录";
    }
}

/**
 * MP3
 */
class HandsetMP3 extends HandsetSoft {
    @Override
    public String run() {
        return "手机运行MP3";
    }
}
/**
 * 手机品牌
 */
class HandsetBrand {
    protected String type = "未知品牌手机";
    protected HandsetSoft soft;

    // 设置手机软件
    // 品牌需要关注软件,所以可在机器中安装软件,以备运行
    public void setSoft(HandsetSoft soft) {
        this.soft = soft;
    }

    // 展示品牌,并且调用手机软件运行
    public String run() {
        return type + ":\n" + soft.run();
    }
}
class HandsetBrandM extends HandsetBrand {
    public HandsetBrandM() {
        type = "手机品牌M";
    }
}

class HandsetBrandN extends HandsetBrand {
    public HandsetBrandN() {
        type = "手机品牌N";
    }
}
image.png
public class BridgeActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, BridgeActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    RadioButton brandM;
    RadioButton brandN;
    RadioButton game;
    RadioButton addressList;
    RadioButton mp3;
    TextView messageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bridge);
        setTitle("桥接模式");

        brandM = findViewById(R.id.radioButtonBrandM);
        brandN = findViewById(R.id.radioButtonBrandN);
        game = findViewById(R.id.radioButtonGame);
        addressList = findViewById(R.id.radioButtonAddressList);
        mp3 = findViewById(R.id.radioButtonMP3);

        messageView = findViewById(R.id.textViewMessage);
    }

    public void onRunClick(View view) {
        HandsetBrand brand = null;
        HandsetSoft soft = null;

        // 确定选了哪个手机品牌
        if (brandM.isChecked()) {
            brand = new HandsetBrandM();
        } else if (brandN.isChecked()) {
            brand = new HandsetBrandN();
        } else {
            // 未定义,什么也不做
        }

        // 确定选了哪个软件
        if (game.isChecked()) {
            soft = new HandsetGame();
        } else if (addressList.isChecked()) {
            soft = new HandsetAddressList();
        } else if (mp3.isChecked()) {
            soft = new HandsetMP3();
        }

        // 组合好了,运行
        if ((null != brand) && (null != soft)) {
            brand.setSoft(soft);
            messageView.setText(brand.run());
        }

    }
}

桥接模式:只需要2 + 3 = 5 个类就可以了。
继承耦合模式:需要 2 * 3 = 6 个不同的子类。
在数字小的时候还不觉得,如果品类增多,乘比加的复杂度要大得多。

Demo地址

https://gitee.com/zhangxusong888/Android/tree/master/design_pattern

上一篇下一篇

猜你喜欢

热点阅读