Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Determine number of classes to be used

3.20/5 (4 votes)
22 Feb 2011CPOL 12K  
Use XML representation of data to figure out object oriented design

This tip is especially for those friends who just started using OOPS concepts and are facing difficulty in determining number of classes to be used for any given scenario. I used this visual technique for getting a quick hint during days as a fresher so thought of writing about it.

For any scenario (given as requirement), try to visualize and draw its equivalent structure in XML format, through it you can at least have a better picture about classes needed.

E.g. We are trying to describe a zoo that can have number of animals and staff members:

XML
<Zoo>
  <Animals>
    <Animal Name="Elephant">
      <Cage Number="102">
      </Cage>
      <!-- Additional details -->
    </Animal>
    <Animal Name="Lion">
      <Cage Number="104">
      </Cage> 
      <!-- Additional details -->
    </Animal>
  </Animals>
  <Staff>
    <Keeper Name="Adam"></Keeper>
  </Staff>
</Zoo>

The corresponding classes will look like:

C#
public class Zoo 
{
    public Collection<Animal> Animals { get; set; }
    public Collection<Keeper> Staff { get; set; }
}

public class Animal 
{
    public string Name { get; set; }
    public Cage Cage { get; set; }
}

public class Cage 
{
    public int Number { get; set; }
}

public class Keeper 
{
    public string Name { get; set; }
}

Now as per more clear needs, keep modifying XML inputs and correspondingly your classes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)