C # combination

Source: Internet
Author: User

IronMan combination

 

In the previous article, we talked about how to deploy "weapons" on "components". We still need to talk about "weapons", but it is about "weapons.

This article introduces the rational use of "weapons" without talking nonsense. Let's look at the problems encountered at first:

Let's take a look at the definition of "Weapon:

1 public abstract class WeaponUpgradeLevel1 2 {3 protected List <WeaponUpgradeLevel1> weaponUpgrades = new List <WeaponUpgradeLevel1> (); 4 5 public List <WeaponUpgradeLevel1> WeaponUpgrades 6 {7 get 8 {9 return weaponUpgrades; 10} 11 12} 13 // <summary> 14 // determine whether it is a "partial" object or a "whole" Object 15 /// </summary> 16 public bool IsSingle17 {18 get19 {20 if (weaponUpgrades. count> 0) 21 {22 return false; 23} 24 else25 {26 Return true; 27} 28} 29} 30 // <summary> 31 // Attack 32 // </summary> 33 public abstract void Attack (); 34 // <summary> 35 // Add a "partial" object or "whole" object to "whole" Object 36 /// </summary> 37 // <param name = "weaponupgrade"> </param> 38 // <returns> </returns> 39 public virtual bool Add (WeaponUpgradeLevel1 weaponupgrade) 40 {41 if (weaponupgrade! = Null) 42 {43 WeaponUpgrades. add (weaponupgrade); 44 return true; 45} 46 else {return false ;} 47 48} 49 // <summary> 50 // remove the "partial" object from the "Overall" object or the "Overall" Object 51 // </summary> 52/ // <param name = "weaponupgrade"> </param> 53 // <returns> </returns> 54 public virtual bool Remove (WeaponUpgradeLevel1 weaponupgrade) 55 {56 if (weaponupgrade! = Null) 57 {58 if (WeaponUpgrades. contains (weaponupgrade) 59 {60 WeaponUpgrades. remove (weaponupgrade); 61 return true; 62} 63 else64 {65 return false; 66} 67} 68 else {return false;} 69} 70} 71 public class Rocket: weaponUpgradeLevel172 {73 private string _ name = "rocket launcher"; 74 75 public override void Attack () 76 {77 Console. writeLine ("using" + _ name + "for attack"); 78} 79} 80 public class RocketLevel1: WeaponUpgradeLevel181 {82 private string _ name = "Rocket Launcher (EX.2X2 )"; // composed of four rocket cannons 83 84 public override void Attack () 85 {86 Console. writeLine ("using" + _ name + "for attack"); 87} 88}

 

The preceding three types are defined. WeaponUpgradeLevel1 is the abstraction of "Weapon" and some attributes and methods are defined in it,

Indicates whether the "Weapon" abstraction type is the core weapon (Part) or the shell of the core weapon (whole ),

In addition, the "Weapon" abstraction is implemented later, namely the core weapon of the Rocket (Part) and the shell of the core weapon of RocketLevel1 (whole). After such a structure is defined, let's look at it all at once, how to use them:

 1 WeaponUpgradeLevel1 weaRocket1 = new Rocket(); 2 WeaponUpgradeLevel1 weaRocket2 = new Rocket();             3 WeaponUpgradeLevel1 weaRocket3 = new Rocket();            4 WeaponUpgradeLevel1 weaRocket4 = new Rocket(); 5  6 WeaponUpgradeLevel1 weaRocketlevel1 = new RocketLevel1(); 7 weaRocketlevel1.Add(weaRocket1); 8 weaRocketlevel1.Add(weaRocket2); 9 weaRocketlevel1.Add(weaRocket3);10 weaRocketlevel1.Add(weaRocket4);

In this case, the weaRocketlevel1 example is as follows:

Figure 1

Figure 2

If weaRocketlevel1 is used, the real purpose is not to use it, but to use a small rocket gun (weaRocket1…) in it ......). If you want to use the small rocket launcher in it, you can simply traverse it. From the current situation, it is indeed a simple traversal.

, Get each rocket gun and use them, but what if this "Rocket Gun (EX.2X2)" is changed to "Rocket Gun (EX.8X8? In addition, "Rocket Gun (EX.8X8)" is composed of four "Rocket Gun (EX.4X4)", and each "Rocket Gun (EX.4X4)" is composed of four "Rocket Gun (EX.2X2. In this case, what should I do? That's right, we use recursion to traverse, and then we can see 3:

Figure 3

In this way, there is indeed no major problem, but the coupling is relatively high.
The design mode can be used for decoupling under specific circumstances, which is suitable for the Composite mode.

Combine objects into a tree structure to represent the "part-whole" hierarchy. The Composite mode enables users to combine a single object

Object usage is consistent.

[GOF design patterns]

 

Based on the design idea, let's take a look at the modified structure:

1 public abstract class WeaponUpgradeLevel1 2 {3 protected List <WeaponUpgradeLevel1> weaponUpgrades = new List <WeaponUpgradeLevel1> (); 4 5 public List <WeaponUpgradeLevel1> WeaponUpgrades 6 {7 get 8 {9 return weaponUpgrades; 10} 11 12} 13 /// <summary> 14 // determines whether the object is a "partial" object or a "whole" object. 15 // true is a "partial" object. Opposite to 16 /// </summary> 17 public bool IsSingle 18 {19 get 20 {21 if (weaponUpgrades. count> 0) 22 {23 return false; 24} 25 else 26 {27 return true; 28} 29} 30} 31 // <summary> 32 // Attack 33 // </summary> 34 public virtual void Attack () 35 {36 ActionAttack (this); 37} 38 private void ActionAttack (WeaponUpgradeLevel1 weaponupgrade) 39 {40 if (weaponupgrade. isSingle) 41 {42 weaponupgrade. attack (); 43} 44 else 45 {46 foreach (WeaponUpgradeLevel1 weapon in weaponupgrade. weaponUpgrades) 47 {48 ActionAttack (weapon ); 49} 50} 51} 52 // <summary> 53 // Add a "partial" object or "whole" object to "whole" Object 54 // </summary> 55 // <param name = "weaponupgrade"> </param> 56 // <returns> </returns> 57 public virtual bool Add (WeaponUpgradeLevel1 weaponupgrade) 58 {59 if (weaponupgrade! = Null) 60 {61 WeaponUpgrades. add (weaponupgrade); 62 return true; 63} 64 else {return false ;} 65 66} 67 // <summary> 68 // remove the "partial" object from the "Overall" object or the "Overall" Object 69 /// </summary> 70/ // <param name = "weaponupgrade"> </param> 71 // <returns> </returns> 72 public virtual bool Remove (WeaponUpgradeLevel1 weaponupgrade) 73 {74 if (weaponupgrade! = Null) 75 {76 if (WeaponUpgrades. contains (weaponupgrade) 77 {78 WeaponUpgrades. remove (weaponupgrade); 79 return true; 80} 81 else 82 {83 return false; 84} 85} 86 else {return false;} 87} 88} 89 public class Rocket: weaponUpgradeLevel1 90 {91 private string _ name = "rocket launcher"; 92 93 public override void Attack () 94 {95 Console. writeLine ("using" + _ name + "for attack"); 96} 97} 98 public class RocketLevel1: WeaponUpgradeLevel1 99 {100 private string _ name = "Rocket Launcher (EX.2X2 )"; // consists of four rocket cannons: 101 102 public override void Attack () 103 {104 base. attack (); 105 Console. writeLine ("attack with" + _ name + "); 106} 107}

Let's take a look at how the current client uses the "rocket launcher ":

 1 WeaponUpgradeLevel1 weaRocket1 = new Rocket(); 2 WeaponUpgradeLevel1 weaRocket2 = new Rocket(); 3 WeaponUpgradeLevel1 weaRocket3 = new Rocket(); 4 WeaponUpgradeLevel1 weaRocket4 = new Rocket(); 5 WeaponUpgradeLevel1 weaRocketlevel1 = new RocketLevel1(); 6 weaRocketlevel1.Add(weaRocket1); 7 weaRocketlevel1.Add(weaRocket2); 8 weaRocketlevel1.Add(weaRocket3); 9 weaRocketlevel1.Add(weaRocket4);10 weaRocketlevel1.Attack();

Result 4 is displayed.

Figure 4

The difference between modifying the code according to the idea of the design pattern is that the "core weapon" is used uniformly in the "Weapon" abstraction. You can understand it yourself.

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.