Lying design pattern notes (XIX) の bridge mode

for example

M and N in two brands of cell phones to make contacts, and each game two software. Prerequisite: M and N are not common between mobile phones.

Counterexample ideas 1

As the main mobile phone brand, to achieve their software.

Counterexample ideas 2

Mobile phone software as the main body, each adapter brand.

Description malpractice

  • Object inheritance is defined at compile time Okay, so I can not change inherited from a parent class implements at runtime.
  • Implementation subclass and its parent classes have very close dependencies, so that any changes in the parent class implementation will inevitably lead to changes in the subclass.
  • When you need to reuse a subclass, if the inherited implementation does not fit to solve new problems, the parent class must be rewritten or replaced by other more appropriate class.
  • This dependency limits flexibility and ultimately limit the reusability.

Extension: synthetic / polymeric multiplexing principles (the CARP)

definition

Namely: to make use of synthetic / polymeric, try not to use class inheritance.

  1. Means a polymerizable weak 'own' relationship, the object A is reflected objects may contain B, but the object is not a part B A of the object;
  2. It is a strong synthetic 'have relations', reflecting the strict relationship between parts and the whole, the parts and the whole life cycle of the same.

benefit

Synthesis of the object using the priority / polymerization will help you keep each class is encapsulated, and concentrated on a single task. Such classes and class hierarchies will remain small, and less likely to grow to uncontrollable monster.

Examples of application of the above principles to transform

Talk is cheap, show me the code

(Shit less, grading up)

/**
 * 手机软件
 * Created by callmeDevil on 2019/12/14.
 */
public abstract class HandsetSoft {
    public abstract void run();
}
/**
 * 手机游戏
 * Created by callmeDevil on 2019/12/14.
 */
public class HandsetGame extends HandsetSoft{
    @Override
    public void run() {
        System.out.println("运行手机游戏");
    }
}
/**
 * 手机通讯录
 * Created by callmeDevil on 2019/12/14.
 */
public class HandsetAddressList extends HandsetSoft{
    @Override
    public void run() {
        System.out.println("运行手机通讯录");
    }
}
/**
 * 手机品牌
 * Created by callmeDevil on 2019/12/14.
 */
public abstract class HandsetBrand {

    protected HandsetSoft soft;

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

    public abstract void run();

}
/**
 * 手机品牌M
 * Created by callmeDevil on 2019/12/14.
 */
public class HandsetBrandM extends HandsetBrand{
    @Override
    public void run() {
        soft.run();
    }
}
/**
 * 手机品牌N
 * Created by callmeDevil on 2019/12/14.
 */
public class HandsetBrandN extends HandsetBrand{
    @Override
    public void run() {
        soft.run();
    }
}
public class Test {
    public static void main(String[] args) {
        HandsetBrand ab;
        System.out.println("---手机品牌N:");
        ab = new HandsetBrandN();
        ab.setHandsetSort(new HandsetGame());
        ab.run();
        ab.setHandsetSort(new HandsetAddressList());
        ab.run();
        System.out.println("---手机品牌M:");
        ab = new HandsetBrandM();
        ab.setHandsetSort(new HandsetGame());
        ab.run();
        ab.setHandsetSort(new HandsetAddressList());
        ab.run();
    }
}

operation result

---手机品牌N:
运行手机游戏
运行手机通讯录
---手机品牌M:
运行手机游戏
运行手机通讯录

The demand for change

Add an MP3 music player, just add a class, the other involves no modification

/**
 * 手机MP3
 * Created by callmeDevil on 2019/12/14.
 */
public class HandsetMP3 extends HandsetSoft{
    @Override
    public void run() {
        System.out.println("运行手机MP3");
    }
}

If you are adding a brand, only need to add a class

/**
 * 手机品牌S
 * Created by callmeDevil on 2019/12/14.
 */
public class HandsetBrandS extends HandsetBrand{
    @Override
    public void run() {
        soft.run();
    }
}

Bridge Mode

definition

The abstraction from its implementation so that they can vary independently.

Note that, to achieve the separation, so that does not mean separation of abstract class and its derived classes, because it does not make any sense. Realization refers to the abstract class and its derived classes to achieve their target.

UML diagrams

to sum up

  • Achieve classification system may have more angles, each classification are likely to change, then put this isolated leave them alone, to reduce the coupling between them angles .
  • As long as real in-depth understanding of the design principles, in fact, a lot of design patterns is the application of the principle of it, perhaps in the use of design patterns unknowingly .

Guess you like

Origin www.cnblogs.com/call-me-devil/p/12039921.html