I am designing a class library which will be used like this:
Engine eng = new Engine();
eng.AddPart(part).AddPart(otherPart).Run();
The problem with the class is that the parts are kept by the engine so if you want Run
it with some other parts you will need to remove the ones that you have already added. There are several possible solutions but I am not sure which one is the best.
1.
Ignore it in class library, the users will call new
every time they need to discard old parts.
2.
Automatically discard parts when Run
is called
3.
Add Clear
method
4.
Instead of returning current object from the AddPart
method (return this
) return new instance of Engine
and do not modify the current one.
5.
Implement IDisposable
so that the user can wrap it in using statement even though no resources are hold that need to be disposed. Users will use it like this:
Engine eng = new Engine();
using(eng)
{
eng.AddPart(part).Run();
}
The disadvantage of the second one is that it users might need to reuse the same one and will not expect that parts are cleared. The third one is nice but users might forget to call Clear
method. The fourth option will allocate many instances and the fifth one is unintuitive.
Any suggestions?