(Transfer) Design Patterns Notes 3: Bridge Yakuu [Original]

xiaoxiao2021-03-06  86

Foreword: Sorry, the previous time due to various reasons, it wrote. This is cheering. ^ _ ^ Concept: Bridge: Separate the abstraction section with its implementation, so that they can change independently. -------------------------------------- The sun, when it is empty; there is no wind, it is really It feels too angry. I remembered that the code was written in the house without the air-conditioned house last summer, ^ _ ^, is really a test of humanity. Andytao is thinking, I don't have to laugh. Lunch break is too fast, continue to write my code. "Andy, come over to help me see!" A string of silver tones came over. "Hey, beautiful women invites, how can I not move." Andyta is thinking, I didn't dare to say it. "I said, what happened?" "" "" "" "Last technical discussion" listened to you, if a abstract class or interface has multiple concrete subclass, in order not to Since the specific implementation of the abstract or interface is used to use simple inheritance, the code confusion and reusability are not strong. You should use Bridge design mode. How is this? You see that I now use the inheritance is not very good. "Oh, I have a look." Public interface draw {public void pressure ();} public class drawcircle import () ("Paint circle"); ......} ... ...} public class dramas {public void Paint () {system.out.println ("Paint Angle"); ......} ......} "You see, I am not doing everything here, it is very good. "Oh, listen to me. Typically, when there is a number of concrete subclass, these Concrete should have the following cases: The first is, more The specific implementation is just a parallelism, just like your code, there are two concrete class: draw rounds and drawing triangles; these two shapes are parallel, there is no relatively conceptual repetition, then we only need It can be used inheritance. ... "" Don't sell Guan Chu is good! "" ... "" Well, I invite you to drink Coke? "Hey, the rape thought, Andytao continued," But we want Considering the second case, if we have a concept repeated between two or more specific implementations, we need to separate the abstract common partial and behaviors. It is ready to put it in an interface. Two interfaces, laying abstract parts and behavior. "" Good abstraction, I don't understand! "" Well, let's take an example, um ..., take Coke, let's say we have a cola. The big cup and the small cup are divided, and there are ice and do not add ice. This way, if we use simple inheritance to achieve these four specific implementation (big cup add ice, big cup does not add ice, small cup add ice Small cups don't add ice), so it is easy to cause overlap between each other, and the code is confusing, not easy to maintain. So ... "" So, how? Continue. "" So, we will use Bridge mode To redesign the structure of the class.

If Bridge mode is used, we need to define two interfaces or abstractions, which are partially separated from the abstract part and behavior. "" Slightly, drink the water first. "" Come come, use me. "That ..., I am really embarrassed, 嘿嘿 ..." "We use a cola as an example." Define Cola as an abstract class, some common implementation code can be placed inside, add ice and do not add ice, then we define it into behavior interfaces. "" Then, we can achieve the following abstraction class. "Public abstract class Coke {CokeImp cokeImp; public void setCokeImp (CokeImp cokeImp) {this.cokeImp = cokeImp;} public CokeImp getCokeImp () {return this.cokeImp;} public abstract void distributeCoke ();} public abstract class CokeImp {public Abstract void distributecokeimp ();} "Now, we have two abstract classes (or interfaces). Above, Coke is an abstract part, and Cokeimp is a defined behavior abstraction class. In order to achieve the function of the four dynamic combination of the four kinds of dynamic combination, we need to achieve a role in the specific implementation of the category. "" This is a great cola: "public class BigCoke extends Coke {public BigCoke () {} public void distributeCoke () {System.out.print (" BigCoke "); CokeImp cokeImp = this.getCokeImp (); cokeImp.distributeCokeImp ( );}} "this is a small Coke:" public class SmallCoke extends Coke {public SmallCoke () {} public void distributeCoke () {System.out.print ( "SmallCoke"); cokeImp cokeImp = this.getCokeImp (); cokeImp .distributecokeimp ();}} "I want to add ice:" Public class Icecokeimp extends Cokeimp {Icecokeimp () {} public void distributecoke () {system.out.print ("Ice added");}} "Don't ice: "Public Class Nonicecokeimp Extends Cokeimp {NoniceCokeimp () {} public void distributecokeimp () {system.out.print (" Havn't Ice ");}}" Here you need to pay attention, because our Cokeimp and Coke are one-on-one The relationship, so you have to find this * IMP from our use case is a more critical and difficult thing.

转载请注明原文地址:https://www.9cbs.com/read-95334.html

New Post(0)