Skip to content

Instantly share code, notes, and snippets.

@tamizhvendan
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save tamizhvendan/062dc9ce4593c16aaf82 to your computer and use it in GitHub Desktop.

Select an option

Save tamizhvendan/062dc9ce4593c16aaf82 to your computer and use it in GitHub Desktop.
Dynamic Polymorphism
private void btnAttack_Click(object sender, EventArgs e)
{
weapon.Attack();
}
public class Bomb : IWeapon
{
public void Attack()
{
System.Windows.Forms.MessageBox.Show("Bomb!!");
}
}
private void rboSword_CheckedChanged(object sender, EventArgs e)
{
weapon = new Sword();
}
private void rboGun_CheckedChanged(object sender, EventArgs e)
{
weapon = new Gun();
}
private void rboBomb_CheckedChanged(object sender, EventArgs e)
{
weapon = new Bomb();
}
public class Gun : IWeapon
{
public void Attack()
{
System.Windows.Forms.MessageBox.Show("Gun!!");
}
}
public interface IWeapon
{
void Attack();
}
public partial class MainForm : Form
{
private IWeapon weapon = new Sword();
}
public class Sword : IWeapon
{
public void Attack()
{
System.Windows.Forms.MessageBox.Show("Sword!!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment