I have a method which should return a snapshot of the current state, and another method which restores that state.
public class MachineModel
{
public Snapshot CurrentSnapshot { get; }
public void RestoreSnapshot (Snapshot saved) { /* etc */ };
}
The state Snapshot
class should be completely opaque to the caller--no visible methods or properties--but its properties have to be visible within the MachineModel
class. I could obviously do this by downcasting, i.e. have CurrentSnapshot
return an object
, and have RestoreSnapshot
accept an object
argument which it casts back to a Snapshot
.
But forced casting like that makes me feel dirty. What's the best alternate design that allows me to be both type-safe and opaque?
Update with solution:
I wound up doing a combination of the accepted answer and the suggestion about interfaces. The Snapshot
class was made a public abstract class, with a private implementation inside MachineModel
:
public class MachineModel
{
public abstract class Snapshot
{
protected internal Snapshot() {}
abstract internal void Restore(MachineModel model);
}
private class SnapshotImpl : Snapshot
{
/* etc */
}
public void Restore(Snapshot state)
{
state.Restore(this);
}
}
Because the constructor and methods of Snapshot
are internal
, callers from outside the assembly see it as a completely opaque and cannot inherit from it. Callers within the assembly could call Snapshot.Restore
rather than MachineModel.Restore
, but that's not a big problem. Furthermore, in practice you could never implement Snapshot.Restore
without access to MachineModel
's private members, which should dissuade people from trying to do so.