I want to select one of two strategies for my classes. plz see and say which of them is better and why. thx I defined several interfaces and component based procedures for soldior class but one time used concrete fields (soldior1,soldio2,..) and the other one used interface fields(soldior). Code (CSharp):
using System;
public interface IMovable {
void Move();
}
public interface IAttackable {
void Attack(ILivable _liveBehaviour);
}
public interface IDisplayable {
void Display();
}
public interface ILivable {
void Die();
void DecreaseHealth(int _hitDamage);
}
public class CLiveBehaviour : ILivable {
private int _m_health;
public int m_health {
get {
return _m_health;
}
private set {
if (value <= 0) {
_m_health = 0;
Die();
}
else {
_m_health = value;
}
}
}
public CLiveBehaviour(int _health) {
m_health = _health;
}
public void Die() {
}
public void DecreaseHealth(int _hitDamage) {
m_health -= _hitDamage;
}
}
public class CAttackBehaviour : IAttackable {
int _m_hitDamage;
int m_hitDamage {
get {
return _m_hitDamage;
}
private set {
_m_hitDamage = value;
}
}
public CAttackBehaviour(int _hitDamage) {
m_hitDamage = _hitDamage;
}
public void Attack(ILivable _liveBehaviour) {
_liveBehaviour.DecreaseHealth(m_hitDamage);
///
}
}
public class CNoAttackBehaviour : IAttackable {
public CNoAttackBehaviour() {
}
public void Attack(ILivable _liveBehaviour) {
//empty
}
}
public class CMoveBehaviour : IMovable {
public void Move() {
///
}
}
public class CDisplayBehaviour : IDisplayable {
public void Display() {
///
}
}
public class CNoDisplayBehaviour : IDisplayable {
public void Display() {
//empty
}
}
public class CSoldiorBase {
private string _m_name;
public string m_name {
get {
return _m_name;
}
private set {
_m_name = value;
}
}
private int _m_id;
public int m_id {
get {
return _m_id;
}
private set {
_m_id = value;
}
}
public CSoldiorBase(string _name, int _id) {
m_name = _name;
m_id = _id;
}
}
public class CSoldior1 : CSoldiorBase {// without interface fields(soldior1 class)
CMoveBehaviour m_moveBehaviour;
CAttackBehaviour m_attackBehaviour;
CDisplayBehaviour m_displayBehaviour;
CLiveBehaviour m_liveBehaviour;
public CSoldior1(string _name, int _id, CLiveBehaviour _liveBehaviour, CMoveBehaviour _moveBehaviour,
CAttackBehaviour _attackBehaviour, CDisplayBehaviour _displayBehaviour) : base(_name, _id) {
m_moveBehaviour = _moveBehaviour;
m_liveBehaviour = _liveBehaviour;
m_attackBehaviour = _attackBehaviour;
m_displayBehaviour = _displayBehaviour;
}
}
public class CSoldior2 : CSoldiorBase {// without interface fields(soldior2 class)
CMoveBehaviour2 m_moveBehaviour;
CAttackBehaviour2 m_attackBehaviour;
CDisplayBehaviour m_displayBehaviour;
CLiveBehaviour m_liveBehaviour;
public CSoldior2(string _name, int _id, CLiveBehaviour _liveBehaviour, CMoveBehaviour2 _moveBehaviour,
CAttackBehaviour2 _attackBehaviour, CDisplayBehaviour _displayBehaviour) : base(_name, _id) {
m_moveBehaviour = _moveBehaviour;
m_liveBehaviour = _liveBehaviour;
m_attackBehaviour = _attackBehaviour;
m_displayBehaviour = _displayBehaviour;
}
}
public class CSoldior3 : CSoldiorBase {// without interface fields(soldior3 class) can not attack
CMoveBehaviour m_moveBehaviour;
CDisplayBehaviour m_displayBehaviour;
CLiveBehaviour m_liveBehaviour;
public CSoldior3(string _name, int _id, CLiveBehaviour _liveBehaviour, CMoveBehaviour _moveBehaviour,
CDisplayBehaviour _displayBehaviour) : base(_name, _id) {
m_moveBehaviour = _moveBehaviour;
m_liveBehaviour = _liveBehaviour;
m_displayBehaviour = _displayBehaviour;
}
}
public class CSoldior4 : CSoldiorBase {// without interface fields(soldior4 class) can not display
CMoveBehaviour m_moveBehaviour;
CAttackBehaviour m_attackBehaviour;
CLiveBehaviour m_liveBehaviour;
public CSoldior4(string _name, int _id, CLiveBehaviour _liveBehaviour, CMoveBehaviour _moveBehaviour,
CAttackBehaviour _attackBehaviour) : base(_name, _id) {
m_moveBehaviour = _moveBehaviour;
m_liveBehaviour = _liveBehaviour;
m_attackBehaviour = _attackBehaviour;
}
}
public class CSoldior : CSoldiorBase { // with interface fields
IAttackable m_attackBehaviour;
IMovable m_moveBehaviour;
IDisplayable m_displayBehaviour;
ILivable m_liveBehaviour;
public CSoldior(string _name, int _id, ILivable _liveBehaviour, IMovable _moveBehaviour,
IAttackable _attackBehaviour, IDisplayable _displayBehaviour) : base(_name, _id) {
m_liveBehaviour = _liveBehaviour;
m_moveBehaviour = _moveBehaviour;
m_attackBehaviour = _attackBehaviour;
m_displayBehaviour = _displayBehaviour;
}
}
void main() {
//different type of soldiors
//----------------------------------------------
CSoldior1 soldior1_1 = new CSoldior1("mahdi", 0, new CLiveBehaviour(100), new CMoveBehaviour(),
new CAttackBehaviour(40), new CDisplayBehaviour());
CSoldior2 soldior2_1 = new CSoldior2("Jun", 1, new CLiveBehaviour(200), new CMoveBehaviour2(),
new CAttackBehaviour2(45), new CDisplayBehaviour());
CSoldior3 soldior3_1 = new CSoldior3("Ali", 2, new CLiveBehaviour(130), new CMoveBehaviour(),
new CDisplayBehaviour());
CSoldior4 soldior4_1 = new CSoldior4("Michel", 3, new CLiveBehaviour(180), new CMoveBehaviour(),
new CAttackBehaviour(20));
//----------------------------------------------
CSoldior soldior_1 = new CSoldior("Sara", 4, new CLiveBehaviour(240), new CMoveBehaviour(), new CAttackBehaviour(15),
new CDisplayBehaviour());
CSoldior soldior_2 = new CSoldior("Zahra", 5, new CLiveBehaviour(768), new CMoveBehaviour(), new CNoAttackBehaviour(),
new CDisplayBehaviour());
CSoldior soldior_3 = new CSoldior("Hafez", 6, new CLiveBehaviour(456), new CMoveBehaviour(), new CAttackBehaviour(35),
new CNoDisplayBehaviour());
CSoldior soldior_4 = new CSoldior("Sadi", 7, new CLiveBehaviour(550), new CMoveBehaviour2(), new CAttackBehaviour2(10),
new CNoDisplayBehaviour());
}
if I use interface fields into the class, I have a flexible class that can suit for many conditions (soldior types) and also can remove a component like display with nodisplayClass. Is it suitable that I implement nocomponentClass like noDisplayClass or pass null to constructor or write new class without this component thx in advance
Aucun commentaire:
Enregistrer un commentaire