Difference between revisions of "Composition - 2017"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Other Examples)
Line 1: Line 1:
 
Composition is where if you delete the main class the subclasses are deleted along with it, or they no longer work.
 
Composition is where if you delete the main class the subclasses are deleted along with it, or they no longer work.
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
 
   class Location
 
   class Location
 
   {
 
   {
Line 13: Line 12:
 
     }
 
     }
 
   }
 
   }
 +
</syntaxhighlight>
 +
 +
As you can see Location is creates an instance of Fox & Warren. So if you remove the instance of location then the Fox & Warren instances for that location will also be destroyed.
 +
 +
==Other Examples==
 +
 +
Warrens & Rabbits - warren creates and destroys rabbits:
  
  class Simulation
+
<syntaxhighlight lang="csharp">
 +
class Warren
 
   {
 
   {
     private Location[,] Landscape;
+
     private const int MaxRabbitsInWarren = 99;
    private int TimePeriod = 0;
+
     private Rabbit[] Rabbits;
     private int WarrenCount = 0;
+
     private int RabbitCount = 0;
     private int FoxCount = 0;
 
    private bool ShowDetail = false;
 
    private int LandscapeSize;
 
    private int Variability;
 
    private static Random Rnd = new Random();
 
 
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
As you can see Location is part of Simulation, so if you remove the class of Location, Simulation would not work as Location forms a variable within the class that is frequently used.
+
The Warren class declare an array of Rabbit. The constructor method for Warren creates each rabbit:
  
==Other Examples==
+
<syntaxhighlight lang="csharp">
 
+
public Warren(int Variability)
Warrens & Rabbits - warren creates and destroys rabbits
+
    {
 +
      this.Variability = Variability;
 +
      Rabbits = new Rabbit[MaxRabbitsInWarren];
 +
      RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
 +
      for (int r = 0; r < RabbitCount; r++)
 +
      {
 +
        Rabbits[r] = new Rabbit(Variability);
 +
      }
 +
    }
 +
</syntaxhighlight>
  
Foxes & Warrens - both are ultimately created and destroyed by the simulation
+
So if a Warren is destroyed it will destroy all of the rabbits in that warren also.

Revision as of 06:56, 26 May 2017

Composition is where if you delete the main class the subclasses are deleted along with it, or they no longer work.

  class Location
  {
    public Fox Fox;
    public Warren Warren;

    public Location()
    {
      Fox = null;
      Warren = null;
    }
  }

As you can see Location is creates an instance of Fox & Warren. So if you remove the instance of location then the Fox & Warren instances for that location will also be destroyed.

Other Examples

Warrens & Rabbits - warren creates and destroys rabbits:

class Warren
  {
    private const int MaxRabbitsInWarren = 99;
    private Rabbit[] Rabbits;
    private int RabbitCount = 0;

The Warren class declare an array of Rabbit. The constructor method for Warren creates each rabbit:

public Warren(int Variability)
    {
      this.Variability = Variability;
      Rabbits = new Rabbit[MaxRabbitsInWarren];
      RabbitCount = (int)(CalculateRandomValue((int)(MaxRabbitsInWarren / 4), this.Variability));
      for (int r = 0; r < RabbitCount; r++)
      {
        Rabbits[r] = new Rabbit(Variability);
      }
    }

So if a Warren is destroyed it will destroy all of the rabbits in that warren also.