I'm playing with some application and doing a little refactoring in order to make it more flexible. As I expected, a lot of problems arose. For example i'm trying to find optimal way to delete items from collections in Spaceship class
class Spaceship{
Weapon Weapons {get;set;}
Shield Shields {get;set;}
public void RemoveEquipment(Equipment item){
switch(item.EquipmentType )
{
case EquipmentType.Weapon:
weapons.Remove(item);
break;
case EquipmentType.Shield:
shields.Remove(item);
break;
}
}
public void AddEquipment(Equipment item){
//works the same way as RemoveEquipment
}
}
class Equipment{
//EquipmentType is enum
EquipmentType Type {get;set;};
}
class Weapon: Equipment{
}
class Shield:Equipment{
}
class AutoCannon:Weapon{
}
class LaserGun:Weapon {
}
Is there a better way? What if I want to delete group of items of the same subtype (e.g. every LaserGun in weapons collection - without using reflection) And if I want to add a new type of Equipment, the methods responsible for adding/deleting have to be modified (switch/case).
Aucun commentaire:
Enregistrer un commentaire