bridge pattren: 个人简单理解
当出现两个角色,且它们之间存在一定的制约关系,纬度高的角色含有对维度低角色的引用,这个引用就相当于桥梁。这样避免了它们之间出现继承关系,使得纬度低的角色不用继承或实现纬度高的角色,这样一来两个类都可以独立的变化而不受到影响。当纬度高的角色需要通过纬度低的角色实现某种业务时,就可以通过桥梁来访问即可,举个栗子:如图:战区司令部对军队有指挥权,战区司令部含有类型为军队的属性,相当于有了兵权,打仗时直接调就行。而战区司令部又包括中部战区,东部战区,西部战区等,添加的时候直接实现战区司令部接口就行,同时军队又分海军,空军,陆军,甚至可以细分为军,师,旅,团,营,连也只需要实现军队接口就行。这样他们都可以各自独立的变化,实现解耦合,灵活度更高。后期添加个特种兵直接实现军队接口,而不用和战区司令部有联系。
(下面的可以不用看了)实验报告:
将桥接模式的教材P93例子自动茶水销售机做修改。由两个维度扩展到三个维度
在已有TeaSize ,TeaKind.上再加一个维度TeaType,实现加糖维度,比如加红糖或黑糖.
修改后:
teaSize:
public interface TeaSize {
public abstract float getPrice();
}
//中杯
public class MediumCup implements TeaSize{
//含有对TeaKind 和TeaType的引用
private TeaKind tk;
private TeaType tt;
public MediumCup(TeaKind tKind,TeaType tType){
tk = tKind;
tt=tType ;
}
public float getPrice(){
float teaPrice = tk.price()+tt.addSugar();
return teaPrice;
}
}
//大杯
public class SuperCup implements TeaSize{
//含有对TeaKind 和TeaType的引用
private TeaKind tk;
private TeaType tt ;
public SuperCup(TeaKind tKind,TeaType tType){
tk = tKind;
tt = tType ;
}
public float getPrice(){
float teaPrice = 1.5f * tk.price()+tt.addSugar();
return teaPrice;
}
}
TeaKind:
public interface TeaKind {
public abstract float price();
}
//绿茶
public class GreenTea implements TeaKind{
private final float PRICE = 2.0f;
public float price(){
return PRICE;
}
}
//红茶
public class RedTea implements TeaKind{
private final float PRICE = 3.0f;
public float price(){
return PRICE;
}
}
加糖:
public interface TeaType {
public abstract float addSugar();
}
//不加糖
public class NoSugar implements TeaType {
private final float PRICE = 0.0f;
@Override
public float addSugar() {
return PRICE;
}
}
//红糖
public class redSugar implements TeaType{
private final float PRICE=0.5f;
@Override
public float addSugar() {
return PRICE ;
}
}
//黑糖
public class blackSugar implements TeaType{
private final float PRICE=0.7f ;
@Override
public float addSugar() {
return PRICE;
}
}
GUI :
//GUI
public class ClientTeaGUI extends JFrame {
public static final String FINDPRICE = "Find Price";
public static final String EXIT = "Exit";
public static final String SUPERCUP = "Super Cup";
public static final String MEDIUMCUP = "Medium Cup";
public static final String REDTEA = "Red Tea";
public static final String GREENTEA = "Green Tea";
public static final String REDSUGAR = "Red Sugar";
public static final String BLACKTEA = "Black Sugar";
public static final String NOSUGAR ="No Sugar" ;
private JLabel lblCupSize, lblTeaKind,lblTeaType, lblTeaPrice, lblChosenTeaPrice;
private JComboBox cmbCupSize, cmbTeaKind,cmbTeaType;
private JButton findBtn, exitButton;
public ClientTeaGUI() {
super(" Bridge Pattern: Tea Seller Machine ");
cmbCupSize = new JComboBox();
cmbCupSize.addItem(SUPERCUP);
cmbCupSize.addItem(MEDIUMCUP);
cmbTeaKind = new JComboBox();
cmbTeaKind.addItem(REDTEA);
cmbTeaKind.addItem(GREENTEA);
cmbTeaType=new JComboBox() ;
cmbTeaType.addItem(BLACKTEA);
cmbTeaType.addItem(REDSUGAR);
cmbTeaType.addItem(NOSUGAR);
lblCupSize = new JLabel("Choose Cup Size");
lblTeaKind = new JLabel("Choose Tea Kind");
lblTeaType = new JLabel("Choose To Add Sugar");
lblTeaPrice = new JLabel("Tea Price:");
lblChosenTeaPrice = new JLabel("Tea Price will be shown here");
//Create the open button
findBtn = new JButton(FINDPRICE);
findBtn.setMnemonic(KeyEvent.VK_V);
exitButton = new JButton(EXIT);
exitButton.setMnemonic(KeyEvent.VK_X);
ButtonListener objButtonListener = new ButtonListener();
findBtn.addActionListener(objButtonListener);
exitButton.addActionListener(new ButtonListener());
JPanel buttonPanel = new JPanel();
buttonPanel.add(findBtn);
buttonPanel.add(exitButton);
JPanel UIPanel = new JPanel();
//****************************************************
GridBagLayout gridbag = new GridBagLayout();
UIPanel.setLayout(gridbag);
GridBagConstraints gbc = new GridBagConstraints();
UIPanel.add(lblCupSize);
UIPanel.add(cmbCupSize);
UIPanel.add(lblTeaKind);
UIPanel.add(cmbTeaKind);
UIPanel.add(lblTeaType) ;
UIPanel.add(cmbTeaType) ;
UIPanel.add(lblTeaPrice);
UIPanel.add(lblChosenTeaPrice);
UIPanel.add(buttonPanel);
gbc.insets.top = 5;
gbc.insets.bottom = 5;
gbc.insets.left = 5;
gbc.insets.right = 10;
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 0;
gridbag.setConstraints(lblCupSize, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gridbag.setConstraints(cmbCupSize, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gridbag.setConstraints(lblTeaKind, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gridbag.setConstraints(cmbTeaKind, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gridbag.setConstraints(lblTeaPrice, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gridbag.setConstraints(lblChosenTeaPrice, gbc);
gbc.insets.left = 2;
gbc.insets.right = 2;
gbc.insets.top = 40;
gbc.gridx = 1;
gbc.gridy = 5;
gridbag.setConstraints(buttonPanel, gbc);
Container contentPane = getContentPane();
contentPane.add(UIPanel, BorderLayout.CENTER);
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
SwingUtilities.updateComponentTreeUI(
ClientTeaGUI.this);
} catch (Exception ex) {
// System.out.println(ex);
throw new RuntimeException(ex);
}
}
public static void main(String[] args) {
JFrame frame = new ClientTeaGUI();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(1080, 500);
frame.setVisible(true);
}
public String getTeaSize() {
return (String) cmbCupSize.getSelectedItem();
}
public String getTeaKind() {
return (String) cmbTeaKind.getSelectedItem();
}
public String getTeaType() {
return (String) cmbTeaType.getSelectedItem();
}
class ButtonListener implements ActionListener {
TeaKind tKind;
TeaSize tSize;
TeaType tType;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(EXIT)) {
System.exit(1);
}
if (e.getActionCommand().equals(FINDPRICE)) {
String size = getTeaSize();
String kind = getTeaKind();
String Type = getTeaType();
//Create a customer object
if (kind.compareTo(GREENTEA) == 0)
tKind = new GreenTea();
if (kind.compareTo(REDTEA) == 0)
tKind = new RedTea();
if (size.compareTo(SUPERCUP) == 0)
tSize = new SuperCup(tKind,tType);
if (size.compareTo(MEDIUMCUP) == 0)
tSize = new MediumCup(tKind,tType);
if(Type.compareTo(REDSUGAR)==0)
tType =new redSugar();
if(Type.compareTo(BLACKTEA)==0)
tType =new blackSugar();
if(Type.compareTo(NOSUGAR)==0)
tType =new NoSugar();
float price = tSize.getPrice();
lblChosenTeaPrice.setText("" + price + " dollars");
}
}
}
}
输入输出:
类图:
时序图: