0
TypeEnum
    MATERIAL,
    OPERATION,
    SIGNATURE;

class AbstractModule
    TypeEnum typeEnum
    String name
    String color

class MaterialModule extends AbstractModule
    some properties unique to this module

class OperationModule extends AbstractModule
    some properties unique to this module

class SignatureModule extends AbstractModule
    someProperties unique to this module

at some point a lot of xxxModule are made and stored in a AbstractModule list. Then later all that list is being iterated and a switch case is use to cast back the abstract module to its child class and do stuff

switch (module.getTypeEnum) {
            case MATERIAL:
                MaterialModule materialModule = (MaterialModule) module;
                //do stuff with materialModule unique property
                break;
            case OPERATION:
                OperationModule operationModule = (OperationModule) module;
                //do stuff with operationModule unique property
                break;
            case SIGNATURE:
                SignatureModule signatureModule = (SignatureModule) module;
                //do stuff with signature module unique property
                break;

each "do stuff with module unique property" should be in a method in each child class

that way you can just call module.doModuleStuff() instead of doing a switch, well no because I still need to cast it back to its child class for that.

Problem is, is If put this code in each child class, how do I call the right method when my list contains AbstractModules? And I mean without having this typeEnum and switch case.

Thanks.

5
  • You can have a abstract method like ´calculate()´ and this method do the stuff with the "unique" properties itself. And you can call the method without casting and have different behaviors. Commented Jul 8, 2018 at 10:32
  • @pL4Gu33 and I let the method empty in the abstract class and when I will call the AbstractModule.calculate() it will call the child class calculate() implementation? Commented Jul 8, 2018 at 10:33
  • you can mark it as abstract and implement it in each class... see docs.oracle.com/javase/tutorial/java/IandI/abstract.html Commented Jul 8, 2018 at 10:34
  • @pL4Gu33 wonderful :) Commented Jul 8, 2018 at 10:35
  • to use class by its type you should use if(abstractModule instanceof MaterialModule){} also you can Override even if it not abstract class. I have full example, but I see you already got your answer Commented Jul 8, 2018 at 11:24

1 Answer 1

1

You can create a method that is public that will call private method with the unique properties you want. For example :

public abstract class AbsModule{
    public void abstract doSomething();
}

public class Module1 implements AbsModule{
    public void doSomething(){
        doSomethingForModule1();
    }
    private void doSomethingForModule1(){
        Your code here
    }
}

public class Module2 implements AbsModule{
    public void doSomething(){
        doSomethingForModule2();
    }
    private void doSomethingForModule2(){
        Your code here
    }
}

You can also check design pattern that can do the job.

Sign up to request clarification or add additional context in comments.

1 Comment

@pL4Gu33 done, it was to explain the concept but proper code is better :) !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.