qxwevr1
qxwevr1

Reputation: 15

One class makes instances of another class. These instances need to use information in original class, how to organize?

I'm fairly new to object oriented programming and I still have trouble organizing my programs.

I'm making a simulation of a factory in c#. I have one instance of a Simulation class. When initialized, the constructor creates several instances of a Machine class.

The Simulation class has an event list. The Machine class has methods that need to add events to this list. Right now, the only way I see how to do this is to pass a reference of the simulation to the Machine constructor. However, these seems messy to me and I feel like there has got to be a better way of organizing this.

public class Simulation
{
    public LinkedList<Event> event_list = new LinkedList<Event>();

    public Simulation()
    {
        Machine m1 = new Machine(this);
        Machine m2 = new Machine(this);
    }

    static void Main(string args[])
    {
        Simulation sim = new Simulation();
    }
}

public class Machine
{
    public Simulation sim;

    public Machine(Simulation sim)
    {
        this.sim = sim;
    }

    public void Schedule_Event()
    {
        sim.event_list.AddLast(new Event(etc etc));
    }
}

public class Event
{
}

Is there a way to add events to Simulation.event_list without having to pass the this reference? In a sense each Machine "belongs" to the Simulation class, and I want to show this somehow.

Upvotes: 0

Views: 108

Answers (3)

Dave Bish
Dave Bish

Reputation: 19646

You could use a more Observer-pattern type approach, using Action<T>

Add a public action to Machine:

public class Machine
{
    public Action<Event> EventAdded;

    private void Schedule_Event()
    {
        if(EventAdded != null)
            EventAdded(new Event());
    }
}

And Subscribe to the action upon creation:

public class Simulation
{
    public LinkedList<Event> event_list = new LinkedList<Event>();

    public Simulation()
    {

        Machine m1 = new Machine() { EventAdded = AddEvent };
    }

    static void Main(string[] args)
    {
        Simulation sim = new Simulation();
    }

    public void AddEvent(Event e)
    {
        event_list.AddLast(new Event(etc etc));
    }
}

Then, the only contract you have between your two objects is the Event type.

Upvotes: 0

elnigno
elnigno

Reputation: 1821

You can have a method in Machine that returns Event objects, then Simulation can take care of adding it to event_list:

public class Simulation
{
    public LinkedList<Event> event_list = new LinkedList<Event>();

    public Simulation()
    {
        Machine m1 = new Machine();
        Machine m2 = new Machine();

        event_list.AddLast(m1.GetEvent());
        event_list.AddLast(m2.GetEvent());
    }

    static void Main(string args[])
    {
        Simulation sim = new Simulation();
    }
}

public class Machine
{
    public Machine() {}

    public Event GetEvent()
    {
        return new Event(etc etc);
    }
}

Upvotes: 2

Exceptyon
Exceptyon

Reputation: 1582

seems pretty much ok to me, I'd just hide the event_list implementation in the Simulation class:

public class Simulation
{
    private LinkedList<Event> event_list = new LinkedList<Event>();

    public void addEven(Event x) {event_list.Add(x); }
}

passing this in constructor is ok, if you really want to you can put a factory method in simulation:

public class Simulation
{
    public Machine CreateNewMachine() { return new Machine(this); }
}

to just allow Machine m1 = Simulation.CreateNewMachine()

Upvotes: 0

Related Questions