Saturday, July 16, 2011

CQ Simulation: Consuming Objects

Today, I'm going to write a class that consumes the DataUser object. It will maintain a list of DataUser objects and process the list.


I plan to use the List[LESS_THAN][GREATER_THAN] type for this. It will allow the list to grow and shrink. I'm not sure what all I will need to do to manipulate the list, but List[LESS_THAN][GREATER_THAN] is well documented and does a lot.


(As an aside, when I started out programming I always shied away from things that seemed this powerful. I assumed that something that did as much as List would take up lots of resources for no good reason.


As I did more programming, especially C, I learned that this wasn't true. Lists are generally implemented as linked lists. Each item has a pointer to the next item. So, when a list has 5 items it takes up x amount of space, 10 items, about 2 times x. Using List for just a few items is not a huge overhead hit, as I was imagining.)


Here's the list field:

...

using System.Collections.Generic;



public class Simulation

{

private List[LESS_THAN]DataUser[GREATER_THAN] computerQueue;

}

…


Here's a constructor. All it does is initializes the list.


public Simulation ()

{

computerQueue = new List[LESS_THAN]DataUser[GREATER_THAN]();

}


And now for some tests.


All I can think of for the constructor is an instantiation test:



try {

$a = new Simulation


}

catch {"FAIL: Simulation constructor failed."}



(To review, the way that I do tests is to write something that is silent when nothing goes wrong. I also want it to display my own FAIL message, not a general PS message.)


Next, the Simulation object needs to consume DataUser objects. I'll also stub out a tick() method for later.


public void takeUser (DataUser x)

{

computerQueue.Add(x);

}

public void tick()

{

}


And a test:



$test = new Simulation;

$dataUser = new DataUser(7, 9, 9)


$test.takeUser($dataUser)


I need to play around with this, of course, but the method does work.


Next time, whatever validation I decide to do with the objects as it reads them in.

No comments:

Post a Comment