I have a parent class , 'Scene' and multiple classes that inherit it. I want to take an 'id' value and initialize a certain class, but have a problem with duplicate types if a switch / if statements are used.
if (sceneType == 1) {
Scene scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
scene.doSomething throws error as in the scope outside the if statements, scene has not been initialized.
Scene scene;
if (sceneType == 1) {
Scene scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
Alternatively if I define scene in the scope of my function, initializing the different types of scene are duplicate local variables.
I also have the problem when a general function that's return type is specific to parent object, but if I want it to return the object type passed into it, rather than the parent by default.
CodePudding user response:
When you declare a variable in a method, you can't re-declare it.
This code should works :
Scene scene;
if (sceneType == 1) {
scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
CodePudding user response:
It is possible to use Dictionary<TKey, TValue> in C# or HashTable in Java to avoid multiple if else
statements.
Let me show an example via C#.
Your base and derived classes:
public class Scene
{
public string Name { get; set; }
}
public class AnswerScene : Scene { }
public class InformationScene : Scene { }
and scene types:
public enum SceneType
{
Base, Answer, Information
}
And factory which creates an instance of Scene
by SceneType
:
public class SceneToType
{
public Dictionary<SceneType, Scene> SceneByType { get; private set; } = new()
{
{ SceneType.Information, new InformationScene() },
{ SceneType.Answer, new AnswerScene() },
{ SceneType.Base, new Scene() }
};
}
and then you can create objects by calling a method:
Scene GetInstance(SceneType sceneType)
{
return new SceneToType().SceneByType[sceneType];
}