当前位置:网站首页>Bridging mode -- law firm
Bridging mode -- law firm
2022-06-24 20:34:00 【zhanyd】
List of articles
Introduction
Recently, Xiaoshuai encountered a worry , He partnered with his friends to start a company , As a result, his friend took private jobs behind his back . Xiaoshuai wants to break up in a fit of anger , But the other side just doesn't admit , And the internal ownership structure of the company is complicated , Xiaoshuai decides to sue him in court , I want to find a lawyer who is familiar with economic cases to help .
Xiaomei is not happy recently , She took a photo of herself on her microblog , It was used by an unscrupulous company as an activity poster , She found out accidentally . Xiaomei goes to that company to discuss something , The other party was perfunctory to her , Is unwilling to apologize for compensation , Xiaomei decides to find a civil lawyer who is good at infringement cases to sue the company in the court .
There are specialized skills , The law covers all aspects of life , According to the business they are good at , Lawyers can be divided into civil lawyers 、 Criminal lawyer , Economic lawyer , Intellectual property lawyer , Foreign lawyers, etc .
Xiaoshuai and Xiaomei are looking for economic lawyers and civil lawyers respectively , If you encounter intellectual property problems next time, you need to find an intellectual property lawyer , But there are not so many lawyer friends around , I can't keep in touch with so many lawyers at the same time .
Now , Xiaoshuai and Xiaomei can go to a law firm , There are many types of lawyers working for you in the law firm , What kind of cases do you encounter , Just find a lawyer of any kind .
If there is no law firm , Xiaoshuai and Xiaomei , Or others, Xiao Wang and Xiao Zhang have to keep in touch with all kinds of lawyers , Maybe one day it will be used by others , Different lawyers are required to represent different cases .

It's different with a law firm , We just need to deal with law firms , What kind of lawyer is needed to be assigned to us by the law firm . Citizens here and lawyers in law firms can change independently , such as : Added citizen Xiao Wang and Xiao Zhang ; Added intellectual property lawyers , Foreign lawyers will not affect each other .

This situation is the bridge mode that I will talk about next .
Bridging mode
Bridging mode (Bridge Pattern): Separate the abstract from its implementation , So that they can all change independently . It's an object structured pattern .

Understand the definition “ abstract ” and “ Realization ” Two concepts , Is the key to understanding it . In the definition “ abstract ”, It doesn't mean “ abstract class ” or “ Interface ”, It's a set of abstractions “ Class library ”, It only contains skeleton code , The real business logic needs to be delegated to... In the definition “ Realization ” To complete . And in the definition “ Realization ”, Also not “ Implementation class of interface ”, But a set of independent “ Class library ”.“ abstract ” and “ Realization ” Independent development , Through the composition relationship between objects , Put it together .( Geek time - Beauty of design pattern )
" Separate the abstract from its implementation ” This sentence is rather awkward , The abstract part here is the citizen , The implementation part is the law firm .
“ So that they can all change independently ” That is to say, the change of citizens and the change of lawyers in law firms will not affect each other .
The sample code is as follows :
Attorney ( Equivalent to a law firm ) class :
/** * Attorney ( Equivalent to a law firm ) */
public interface Lawyer {
/** * debate */
public void debate();
}
Economic lawyers :
/** * Economic lawyer */
public class EconomicLawyer implements Lawyer{
/** * debate */
@Override
public void debate() {
System.out.println(" Economic lawyers debate economic cases in court .");
}
}
Civil lawyers :
/** * A civil lawyer */
public class CivilLawyer implements Lawyer{
/** * debate */
@Override
public void debate() {
System.out.println(" Civil lawyers debate civil cases in court .");
}
}
Citizen category :
/** * a citizen */
public abstract class Citizen {
/** * Attorney */
protected Lawyer lawyer;
public Citizen(Lawyer lawyer) {
this.lawyer = lawyer;
}
/** * appear in court */
public abstract void appearInCourt();
}
Xiaomei :
/** * Xiaomei */
public class XiaoMei extends Citizen{
public XiaoMei(Lawyer lawyer) {
super(lawyer);
}
/** * appear in court */
@Override
public void appearInCourt() {
System.out.println(" Xiaomei appears in court :");
super.lawyer.debate();
}
}
Xiaoshuai :
/** * Xiaoshuai */
public class XiaoShuai extends Citizen{
public XiaoShuai(Lawyer lawyer) {
super(lawyer);
}
/** * appear in court */
@Override
public void appearInCourt() {
System.out.println(" Xiao Shuai appears in court :");
super.lawyer.debate();
}
}
test :
/** * court */
public class Court {
public static void main(String[] args) {
// Xiaoshuai finds an economic lawyer through a law firm
Citizen xiaoShuai = new XiaoShuai(new EconomicLawyer());
// Xiao Shuai appears in court
xiaoShuai.appearInCourt();
// Xiaomei finds a civil lawyer through a law firm
Citizen xiaoMei = new XiaoMei(new CivilLawyer());
// Xiaomei appears in court
xiaoMei.appearInCourt();
}
}
Output :
Xiao Shuai appears in court :
Economic lawyers debate economic cases in court .
Xiaomei appears in court :
Civil lawyers debate civil cases in court .
The difference between bridge mode and policy mode
The strategy pattern (Strategy) And bridge mode (Bridge) It looks very similar , In fact, there is a big difference between them : Policy patterns change the behavior of objects , The bridging pattern changes the structure of the class .
The strategy pattern is a dynamic pattern of behavior , Solve the problem of calling between objects ( Study how to change the behavior of objects during program operation ).
Bridge mode is a structural mode , Solve the structural problems between classes ( Study how to reconstruct the structure of classes ).
The intentions of these two design patterns are different .
summary
If you do not want to use inheritance or the number of system classes increases dramatically because of multi-level inheritance , We can consider using the bridge mode , Take two. ( Or more ) The dimension of independent change , By combination , Let these two ( Or more ) Dimensions can be expanded independently .
Give another example to illustrate , For example, a car has two latitudes , One is the brand : Mercedes 、 BMW 、 Audi, wait , There is also a gear : automatic 、 manual 、 Hand in hand, etc .
If you follow the inherited design pattern , Suppose there is M Car brand ,N A total of gears need to be written M*N Class to describe all combinations of cars and gears , It will cause a kind of explosion .
If we use bridge mode , The brand and gear can be changed independently , When using, just combine the brand and gear , It only needs M+N Just a class .
The bridging mode is to extract all the independent latitudes of the system , Then, when using, it can be combined as required .
Let's look at the advantages and disadvantages of the bridge mode :
advantage
- Comply with opening and closing principle : It improves the scalability of the system , The abstract part and the implementation part can be added , Expand one of the two change dimensions , No need to modify the original system , And they don't interact with each other .
- The implementation replaces the inheritance relationship through the composition relationship , Avoid exponential explosion of inheritance hierarchy .
shortcoming
- Using this pattern for highly cohesive classes can make the code more complex .
边栏推荐
- The four stages of cloud computing development have finally been clarified
- Two fellow countrymen from Hunan have jointly launched a 10 billion yuan IPO
- The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
- 年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
- DX12引擎开发课程进度-这个课程到底讲到哪里了
- 【建议收藏】时间序列预测应用、paper汇总
- VXLAN 与 MPLS:从数据中心到城域以太网
- C語言實現掃雷(簡易版)
- Error in Android connection database query statement
- [performance tuning basics] performance tuning strategy
猜你喜欢
随机推荐
Accurate calculation of task progress bar of lol mobile game
伯克利、MIT、剑桥、DeepMind等业内大佬线上讲座:迈向安全可靠可控的AI
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
消息称腾讯正式宣布成立“XR”部门,押注元宇宙;谷歌前 CEO:美国即将输掉芯片竞争,要让台积电、三星建更多工厂...
图像PANR
VMware virtual machine setting static IP
Mapstacks: data normalization and layered color layer loading
16个优秀业务流程管理工具
Vxlan and MPLS: from data center to Metro Ethernet
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
The name of the button in the Siyuan notes toolbar has changed to undefined. Has anyone ever encountered it?
Byte and Tencent have also come to an end. How fragrant is this business of "making 30million yuan a month"?
在Dialog中使用透明的【X】叉叉按钮图片
科技抗疫: 运营商网络洞察和实践白皮书 | 云享书库NO.20推荐
Leetcode (455) - distribute cookies
[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock
基于SSM的物料管理系统(源码+文档+数据库)
Leetcode(135)——分发糖果
Map跟object 的区别
网络安全审查办公室对知网启动网络安全审查,称其“掌握大量重要数据及敏感信息”









