To my knowledge, to use global variable can make code smell bad because of its tight coupling or the problem of testing.
And in my situation, I want to use some objects with IOC/DI and these objects might be dependent within any of the activities of fragments.
So I make a class named Global and put static variables.
public class Global {
private static ConvertStrategy convertStrategy;
private static EntityController entityController;
public static void init(ConvertStrategy convertStrategy, EntityController entityController){
Global.convertStrategy = convertStrategy;
Global.entityController= entityController;
}
public ConvertStrategy getConvertStrategy(){
return convertStrategy;
}
public EntityController getEntityController (){
return entityController;
}
}
Then I can init it in the MainActivity,
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
Global.init(new OfflineConvertStrategy() , new OfflineController());
...
...
}
So obviously now I'm using Offline algorithm, then other days I can create other type of strategy or controller for online version without changing codes within any dependency to fragments or activities, this is how IOC/DI benefits me.
Sorry for my bad description, hope this image concretize it:
But when the IOC/DI object amount growth up, I feel it's start hard to maintain.
For example ...
public class Global {
public static Resources resources;
public static DateConvertStrategy dateConvertStrategy;
private static MemberController memberController;
private static ProjectSearcher projectSearcher;
private static OfficeController officeController;
private static EntityController<Project> projectController;
private static EntityController<IssueType> issuetypeController;
private static EntityController<Issue> issueController;
private static EntityController<IssueComment> issueCommentController;
private static EntityController<Timeline> timelineController;
private static EntityController<MemberIdCardModel> memberIdCardController;
private static EntityController<TodoTask> todotaskController;
...
...
}
I think somehow I overdosed using IOC/DI and global variables...
Please share your idea.
Aucun commentaire:
Enregistrer un commentaire