17. [Serializable]
public abstract class IPrototype<T>
{
public T DeepCopy()
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Seek(0, SeekOrigin.Begin);
T copy = (T)formatter.Deserialize(stream);
stream.Close();
return copy;
}
}
20. public class C
{
private C() { /* nothing here */ }
class CMaker {
static CMaker () { }
internal static readonly C instance = new C();
}
public static C Instance {
get { return CMaker.instance; }
}
}
52. class Context {
ICarState state;
public void GoGoGo() {
state.Go(this);
state.Go(this); // yeah, right :)
state.Go(this);
}
}
53. class CrashedState : class MovingState :
ICarState ICarState
{ {
void Go(Context ctx) void Go(Context ctx)
{ {
MessageBox.Show( // driving
“Are you crazy?”); // is that a rock?
}
} // CRASH!
The state ctx.state =
just new CrashedState();
changed! }
}