samedi 19 août 2017

how can we configure null removable components(object field) of a class?

suppose we have a class with some components(object fields) that some of them can be null(removed) Is it suitable to create a new class without the (component) field or use interface to handle it. suppose we have several components that can be removed for some objects Thx in advance

public class Class1{
   Component1 component1;
   Component2 component2;
   Component3 component3;//can be null (removed)
   Component4 component4;//can be null (removed)
}
public class Class2{//removed component3,component4(new class)
   Component1 component1;
   Component2 component2;      
}
public class Class3{//removed component3(new class)
   Component1 component1;
   Component2 component2;  
   Component4 component4;  
}
public class Class4{//removed component4(new class)
   Component1 component1;
   Component2 component2;      
   Component3 component3;      
}

or use interface

public interface IComponent1{

}
public class Component1:IComponent1{

}
public class NoComponent1:IComponent1{

}
public interface IComponent2{

}
public class Component2:IComponent2{

}
public class NoComponent2:IComponent2{

}
public interface IComponent3{

}
public class Component3:IComponent3{

}
public class NoComponent3:IComponent3{

}
public interface IComponent4{

}
public class Component4:IComponent4{

}
public class NoComponent4:IComponent4{

}
public class Class1{
    IComponent1 componentBehaviour1;
    IComponent2 componentBehaviour2;
    IComponent3 componentBehaviour3;
    IComponent4 componentBehaviour4;
}

Aucun commentaire:

Enregistrer un commentaire