Preface the previous sections are about the simple use of "parts". In the future, we may want to upgrade "parts", rather than upgrading itself, it can be equipped with weapons. Do you feel that "parts" are becoming more and more powerful, and things are evolving in a process that must be improved and improved? This is the purpose of "IronMan" design. Well, there is no more nonsense. Now, the job we need to do is to design this "Weapon" so that we can combine it well with "parts" in the subsequent sections. Let's take a look at the definition of "Weapon: copy code 1 // <summary> 2 // Weapon 3 /// </summary> 4 public abstract class Weapon 5 {6 /// <summary> 7 /// attack 8 // </summary> 9 public abstract void Attack (); 10} 11 /// <summary> 12 // laser weapon 13 /// </summary> 14 public class LaserWeapon: Weapon15 {16 17 public override void Attack () 18 {19 // LaserAttack20} 21} 22 // <summary> 23 // Missile Weapon 24 /// </summary> 25 public class MissileWeapon: Weapo N26 {27 28 public override void Attack () 29 {30 // MissileAttack31} 32} copy the code and it seems that there is nothing wrong with it, because of the comprehensiveness of the design and the convenience of future expansion, we should try our best to make it as comprehensive as possible. The use of "weapons" is also subject to environmental restrictions or influences. How can it be displayed here? Let's assume that two types of bad environment weapons are used, one is underwater, and the other is in space (vacuum state) copy code 1 // <summary> 2 // underwater laser weapon 3 /// </summary> 4 public class UnderwaterLaserWeapon: LaserWeapon 5 {6 public override void Attack () 7 {8 // logic processing 9 base. attack (); 10 // logic processing 11} 12} 13 // <summary> 14 // laser weapon in a vacuum state 15 /// </summary> 16 public class EmptyLaserWeapon: laserWeapon17 {18 public override void Attack () 19 {20 // logic processing 21 base. attack (); 22 // logic processing 23} 24} 25 // /<Summary> 26 // underwater Missile Weapon 27 /// </summary> 28 public class UnderwaterMissileWeapon: MissileWeapon29 {30 public override void Attack () 31 {32 // logic processing 33 base. attack (); 34 // logical processing 35} 36} 37 // <summary> 38 // missile weapons under a vacuum 39 /// </summary> 40 public class EmptyMissileWeapon: missileWeapon41 {42 public override void Attack () 43 {44 // logic processing 45 base. attack (); 46 // logic processing 47} 48} the overall design of the copied code is not serious, and there is no problem when adding a "Weapon" type. Follow the principle of opening and closing. If the principle of opening and closing is followed when adding a bad environment, but it violates the single responsibility principle of the class, and such design has too many inheritance relationships and is difficult to maintain and expand, if the "Weapon" is multidimensional and changes toward the multidimensional front at the same time, the structure of this "Weapon" will become enormous. Now it is time to make full use of "bridging". We need to decouple the two (or more) changing directions of "weapons" and turn strong coupling into weak connections. The structure of the original "Weapon" remains unchanged. You only need to extract the abstract (Implementation class Interface) from one of the dimensions, and then "Weapon" (abstract class) just reference the abstraction extracted from the dimension. Let's take a look at the structure: copy code 1 // <summary> 2 // environment 3 // </summary> 4 public interface IEnvironment 5 {6 // <summary> 7 // Environment abstract 8 /// </summary> 9 void EnvironmentJudge (); 10} 11 /// <summary> 12 // Underwater environment 13 /// </summary> 14 public class Underwater: IEnvironment15 {16 17 public void EnvironmentJudge () 18 {19 Console. writeLine ("underwater "); 20} 21} 22 // <summary> 23 // 24 // in a vacuum environment </summary> 25 public class Empty: IEnvironment26 {27 28 public void EnvironmentJudge () 29 {30 Console. writeLine ("under the vacuum"); 31} 32} copy the structure of the environment dimension above the code. The following describes the structure of the "Weapon" type dimension: copy code 1 // <summary> 2 // Weapon 3 // </summary> 4 public abstract class Weapon 5 {6 protected IEnvironment Environment = null; 7 /// <summary> 8 /// Attack 9 /// </summary> 10 public abstract void Attack (); 11} 12 // <summary> 13 // laser weapon 14 /// </summary> 15 public class LaserWeapon: Weapon16 {17 public LaserWeapon (): this (new Underwater () {} // The default environment is 18 public LaserWeapon (IEnvironment environment) 19 {20 base. environment = environment; 21} 22 public override void Attack () 23 {24 // LaserAttack25 base. environment. environmentJudge (); 26 Console. writeLine ("laser weapon"); 27} 28} 29 // <summary> 30 // Missile Weapon 31 /// </summary> 32 public class MissileWeapon: weapon33 {34 public MissileWeapon (): this (new Underwater () {} 35 public MissileWeapon (IEnvironment environment) 36 {37 base. environment = environment; 38} 39 public override void Attack () 40 {41 // MissileAttack42 base. environment. environmentJudge (); 43 Console. writeLine ("missile weapons"); 44} 45}