Mobile phones have evolved to the present, and with the brutal competition, you can sing to me, the old boss Nokia, BlackBerry, and other manufacturers, now have a thin sky, the emerging Android (Samsung) and Apple mobile phones have long left them far behind. One day, a whimsy: Is there a credit for the "Bridge Mode" in this situation?
If you want to download the software from nokia3210c or 7610, you have to find the software that matches your mobile phone model and System (s40, s60v2, s60v3, and so on. Currently, Android phones can be used directly regardless of the model or manufacturer, as long as they meet the requirements of the system (such as android2.3 and android4.0. This greatly facilitates our mobile phone users. Why? Let's look at the evolution of the Structure Design below:
Assume that a mobile phone of the M brand and an n brand needs the address book and game functions. Then, we can implement the structure chart by brand classification:
The architecture can also be implemented by software classification:
If you need to add an MP3 function to your mobile phone, simply add the branch according to the classification. If you create an S-brand Mobile Phone, MP3, address book, and game functions are required, the branches of the two classification methods will be very bloated! I won't draw it here. This is the adverse effect of inheritance! In fact, inheritance may cause problems in many cases, such:The inheritance relationships of objects are defined at compilation, so the implementation of inheritance from the parent class cannot be changed at runtime. The implementation of a subclass has a very close dependency with its burden, so that any change in the line of sight of the parent class will inevitably lead to a change in the subclass. When you need to reuse child classes, if the inherited implementation is not suitable for solving new problems, the parent class must be overwritten or replaced by other more appropriate classes. This dependency limits flexibility and ultimately limits reusability. Its essence is:Inheritance is a strongly coupled structure. If the parent class is changed, the subclass must be changed.
We can use the following classification method:
Similarly, add an MP3 function and an S-branded mobile phone with all functions. In this mode, you only need to add a branch to the "mobile phone software" and "mobile phone brand" categories. The architecture is much simpler and more thorough than the two classification methods described above. The above two classification methods can represent Nokia mobile phones and Saipan systems. The last classification method can represent Android phones and Android systems.
In this classification method,"Synthesis/Principle of aggregation Reuse":Try to use Merging/Do not use class inheritance for aggregation. The advantage is that the synthesis of objects is preferred./Aggregation will help us keep every class encapsulated and concentrated on a single task. Such classes and class inheritance layers will remain relatively small and are unlikely to grow into an uncontrollable giant.
In fact, this classification method is the essence of the bridge model. Let's take a look at this amazing FAIRY: The Bridge Model.
Bridge Mode (Bridge): Separates abstract parts from their implementations so that they can all change independently.
In other words, the implementation system may have multi-angle classification, and each category may change. In this case, the multi-angle classification will allow them to change independently and reduce coupling between them. It is the third method of the above three classification methods.
Its UML diagram:
Related code:
Implementor class
abstract class Implementor { public abstract void Operation(); }
Concreteimplementora, concreteimplementorb, and other derived classes
Class concreteimplementora: implementor {public override void operation () {console. writeline ("Implementation of a Method ");}}
Class concreteimplementorb: implementor {public override void operation () {console. writeline ("Implementation of Method B ");}}
Invalid action class
class Abstraction { protected Implementor implementor; public void SetImplementor(Implementor implementor) { this.implementor = implementor; } public virtual void Operation() { implementor.Operation(); } }
Refinedmediaaction class
class RefinedAbstraction :Abstraction { public override void Operation() { implementor.Operation(); } }
Client implementation
class Program { static void Main(string[] args) { Abstraction ab = new RefinedAbstraction(); ab.SetImplementor(new ConcreteImplementorA ()); ab.Operation(); ab.SetImplementor(new ConcreteImplementorB ()); ab.Operation(); Console.Read(); } }
Through this example, we can understand that the more classes, the better, and the more inheritance, the better. When only inheritance is used, a large number of classes will be added and the open-closed principle cannot be met, the bridge mode should be taken into account.
When the design pattern comes to the present, we do not simply use a pattern, but more follow the five design principles that need to be used in the design process: single responsibility principle, open and closed principle, dependency reversal principle, Demeter rule and synthesis/aggregation Reuse Principle. In the future, when these design patterns are used skillfully, there will be no more patterns, but simply follow the design principles.