Bad Way:
interface Monster {}
class Drawf : Monster {}
class Giant : Monster {}
class Door
{
public virtual void OpenBy(Monster monster)
{
Console.WriteLine("Who are u?");
}
public virtual void OpenBy(Drawf dwarf)
{
Console.WriteLine("It's slowly opened");
}
public virtual void OpenBy(Giant giant)
{
Console.WriteLine("It's just easily brokenCrasp!");
}
}
class HeavyDoor : Door
{
public override void OpenBy(Drawf dwarf)
{
Console.WriteLine("It won't open");
}
public override void OpenBy(Giant giant)
{
Console.WriteLine("It's slowly opened");
}
}
class Game
{
static void Main()
{
Door ironDoor = new HeavyDoor();
List<Monster> monsters = new List<Monster>();
monsters.Add(new Drawf());
monsters.Add(new Giant());
foreach (Monster m in monsters)
ironDoor.OpenBy(m);
}
}
class Drawf : Monster {}
class Giant : Monster {}
class Door
{
public virtual void OpenBy(Monster monster)
{
Console.WriteLine("Who are u?");
}
public virtual void OpenBy(Drawf dwarf)
{
Console.WriteLine("It's slowly opened");
}
public virtual void OpenBy(Giant giant)
{
Console.WriteLine("It's just easily brokenCrasp!");
}
}
class HeavyDoor : Door
{
public override void OpenBy(Drawf dwarf)
{
Console.WriteLine("It won't open");
}
public override void OpenBy(Giant giant)
{
Console.WriteLine("It's slowly opened");
}
}
class Game
{
static void Main()
{
Door ironDoor = new HeavyDoor();
List<Monster> monsters = new List<Monster>();
monsters.Add(new Drawf());
monsters.Add(new Giant());
foreach (Monster m in monsters)
ironDoor.OpenBy(m);
}
}
Double-Dispatch Way:
interface Monster
{
void Open(Door d);
}
class Drawf : Monster
{
public void Open(Door d)
{
d.OpenBy(this);
}
}
class Giant : Monster
{
public void Open(Door d)
{
d.OpenBy(this);
}
}
class Door
{
public virtual void OpenBy(Monster monster)
{
Console.WriteLine("Who are u?");
}
public virtual void OpenBy(Drawf dwarf)
{
Console.WriteLine("It's slowly opened");
}
public virtual void OpenBy(Giant giant)
{
Console.WriteLine("It's just easily brokenCrasp!");
}
}
class HeavyDoor : Door
{
public override void OpenBy(Drawf dwarf)
{
Console.WriteLine("It won't open");
}
public override void OpenBy(Giant giant)
{
Console.WriteLine("It's slowly opened");
}
}
class Game
{
static void Main()
{
Door ironDoor = new HeavyDoor();
List<Monster> monsters = new List<Monster>();
monsters.Add(new Drawf());
monsters.Add(new Giant());
foreach (Monster m in monsters)
m.Open(ironDoor);
}
}
{
void Open(Door d);
}
class Drawf : Monster
{
public void Open(Door d)
{
d.OpenBy(this);
}
}
class Giant : Monster
{
public void Open(Door d)
{
d.OpenBy(this);
}
}
class Door
{
public virtual void OpenBy(Monster monster)
{
Console.WriteLine("Who are u?");
}
public virtual void OpenBy(Drawf dwarf)
{
Console.WriteLine("It's slowly opened");
}
public virtual void OpenBy(Giant giant)
{
Console.WriteLine("It's just easily brokenCrasp!");
}
}
class HeavyDoor : Door
{
public override void OpenBy(Drawf dwarf)
{
Console.WriteLine("It won't open");
}
public override void OpenBy(Giant giant)
{
Console.WriteLine("It's slowly opened");
}
}
class Game
{
static void Main()
{
Door ironDoor = new HeavyDoor();
List<Monster> monsters = new List<Monster>();
monsters.Add(new Drawf());
monsters.Add(new Giant());
foreach (Monster m in monsters)
m.Open(ironDoor);
}
}
Reflection Way:
interface Monster {}
class Drawf : Monster {}
class Giant : Monster {}
class Door
{
public virtual void OpenBy(Monster monster)
{
Type[] types = new Type[] { monster.GetType() };
MethodInfo mi = this.GetType().GetMethod("OpenBy", types);
if (mi == null)
Console.WriteLine("Who are u?");
else
mi.Invoke(this, new object[] { monster });
}
public virtual void OpenBy(Drawf dwarf)
{
Console.WriteLine("It's slowly opened");
}
public virtual void OpenBy(Giant giant)
{
Console.WriteLine("It's just easily brokenCrasp!");
}
}
class HeavyDoor : Door
{
public override void OpenBy(Drawf dwarf)
{
Console.WriteLine("It won't open");
}
public override void OpenBy(Giant giant)
{
Console.WriteLine("It's slowly opened");
}
}
class Game
{
static void Main()
{
Door ironDoor = new HeavyDoor();
List<Monster> monsters = new List<Monster>();
monsters.Add(new Drawf());
monsters.Add(new Giant());
foreach (Monster m in monsters)
ironDoor.OpenBy(m);
}
}
class Drawf : Monster {}
class Giant : Monster {}
class Door
{
public virtual void OpenBy(Monster monster)
{
Type[] types = new Type[] { monster.GetType() };
MethodInfo mi = this.GetType().GetMethod("OpenBy", types);
if (mi == null)
Console.WriteLine("Who are u?");
else
mi.Invoke(this, new object[] { monster });
}
public virtual void OpenBy(Drawf dwarf)
{
Console.WriteLine("It's slowly opened");
}
public virtual void OpenBy(Giant giant)
{
Console.WriteLine("It's just easily brokenCrasp!");
}
}
class HeavyDoor : Door
{
public override void OpenBy(Drawf dwarf)
{
Console.WriteLine("It won't open");
}
public override void OpenBy(Giant giant)
{
Console.WriteLine("It's slowly opened");
}
}
class Game
{
static void Main()
{
Door ironDoor = new HeavyDoor();
List<Monster> monsters = new List<Monster>();
monsters.Add(new Drawf());
monsters.Add(new Giant());
foreach (Monster m in monsters)
ironDoor.OpenBy(m);
}
}