Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Post Reopened by mdfst13, Graipher, 200_success
Rollback to Revision 6
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

 

EDIT:

Based on Ilgorbek Kuchkarovs answer (original from StackOverflow) (similar problem here: Dispatching SOAP function calls) I have implemented the solution as this:

Interface ServiceMethod.java:

interface ServiceMethod {
    public void execute();
}

WorkingClass.java:Should

public class WorkingClass {

    static OrderObject orderObject;

    public static Map<String, ServiceMethod> serviceMethodMap = new HashMap<>();
    public static Map<String, String> allCarAttributes = new HashMap<>();
    
    public static void main(String[] args) {

        orderObject = new OrderObject();
        
        allCarAttributes.put("CAR_DOORS", "4");
        allCarAttributes.put("CAR_WHEELS", "10");
        

        serviceMethodMap.put("CAR_DOORS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarDoors(input);
            }
        });

        serviceMethodMap.put("CAR_WHEELS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarWheels(input);
            }
        });

        for (Map.Entry<String, String> attribute : allCarAttributes.entrySet()) {
            serviceMethodMap.get(attribute.getKey()).execute(attribute.getValue());
        }

        System.out.println(orderObject.toString());
    }
}

This works as intended, however I dont see the advantageget rid of using the interface-approach to usingit at all? The switch-case.

I still have to write 80 times:

serviceMethodMap.put("CAR_DOORS / WHEELS / COLORS / ...", new ServiceMethod() {

    @Override
    public void execute(String input) {
        orderObject.setCarDoors / -Wheels / -Colors / ...(input);
    }
});

This does not seem any shorter works fine as it is, it is just very long and even complexer?

Could anyone explain the advantages of using this approach?ugly.


 

EDIT:

Based on Ilgorbek Kuchkarovs answer (original from StackOverflow) (similar problem here: Dispatching SOAP function calls) I have implemented the solution as this:

Interface ServiceMethod.java:

interface ServiceMethod {
    public void execute();
}

WorkingClass.java:

public class WorkingClass {

    static OrderObject orderObject;

    public static Map<String, ServiceMethod> serviceMethodMap = new HashMap<>();
    public static Map<String, String> allCarAttributes = new HashMap<>();
    
    public static void main(String[] args) {

        orderObject = new OrderObject();
        
        allCarAttributes.put("CAR_DOORS", "4");
        allCarAttributes.put("CAR_WHEELS", "10");
        

        serviceMethodMap.put("CAR_DOORS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarDoors(input);
            }
        });

        serviceMethodMap.put("CAR_WHEELS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarWheels(input);
            }
        });

        for (Map.Entry<String, String> attribute : allCarAttributes.entrySet()) {
            serviceMethodMap.get(attribute.getKey()).execute(attribute.getValue());
        }

        System.out.println(orderObject.toString());
    }
}

This works as intended, however I dont see the advantage of using the interface-approach to using switch-case.

I still have to write 80 times:

serviceMethodMap.put("CAR_DOORS / WHEELS / COLORS / ...", new ServiceMethod() {

    @Override
    public void execute(String input) {
        orderObject.setCarDoors / -Wheels / -Colors / ...(input);
    }
});

This does not seem any shorter and even complexer?

Could anyone explain the advantages of using this approach?

Should I get rid of it at all? The switch-case works fine as it is, it is just very long and ugly.

Added my implementation of the solution and asked for further explanations, since my solution is working, but I fail to see the advantages.
Source Link
 

ShouldEDIT:

Based on Ilgorbek Kuchkarovs answer (original from StackOverflow) (similar problem here: Dispatching SOAP function calls) I get ridhave implemented the solution as this:

Interface ServiceMethod.java:

interface ServiceMethod {
    public void execute();
}

WorkingClass.java:

public class WorkingClass {

    static OrderObject orderObject;

    public static Map<String, ServiceMethod> serviceMethodMap = new HashMap<>();
    public static Map<String, String> allCarAttributes = new HashMap<>();
    
    public static void main(String[] args) {

        orderObject = new OrderObject();
        
        allCarAttributes.put("CAR_DOORS", "4");
        allCarAttributes.put("CAR_WHEELS", "10");
        

        serviceMethodMap.put("CAR_DOORS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarDoors(input);
            }
        });

        serviceMethodMap.put("CAR_WHEELS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarWheels(input);
            }
        });

        for (Map.Entry<String, String> attribute : allCarAttributes.entrySet()) {
            serviceMethodMap.get(attribute.getKey()).execute(attribute.getValue());
        }

        System.out.println(orderObject.toString());
    }
}

This works as intended, however I dont see the advantage of it at all? Theusing the interface-approach to using switch-case works fine as it is, it is just very long and ugly.

I still have to write 80 times:

serviceMethodMap.put("CAR_DOORS / WHEELS / COLORS / ...", new ServiceMethod() {

    @Override
    public void execute(String input) {
        orderObject.setCarDoors / -Wheels / -Colors / ...(input);
    }
});

This does not seem any shorter and even complexer?

Could anyone explain the advantages of using this approach?

Should I get rid of it at all? The switch-case works fine as it is, it is just very long and ugly.

 

EDIT:

Based on Ilgorbek Kuchkarovs answer (original from StackOverflow) (similar problem here: Dispatching SOAP function calls) I have implemented the solution as this:

Interface ServiceMethod.java:

interface ServiceMethod {
    public void execute();
}

WorkingClass.java:

public class WorkingClass {

    static OrderObject orderObject;

    public static Map<String, ServiceMethod> serviceMethodMap = new HashMap<>();
    public static Map<String, String> allCarAttributes = new HashMap<>();
    
    public static void main(String[] args) {

        orderObject = new OrderObject();
        
        allCarAttributes.put("CAR_DOORS", "4");
        allCarAttributes.put("CAR_WHEELS", "10");
        

        serviceMethodMap.put("CAR_DOORS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarDoors(input);
            }
        });

        serviceMethodMap.put("CAR_WHEELS", new ServiceMethod() {

            @Override
            public void execute(String input) {
                orderObject.setCarWheels(input);
            }
        });

        for (Map.Entry<String, String> attribute : allCarAttributes.entrySet()) {
            serviceMethodMap.get(attribute.getKey()).execute(attribute.getValue());
        }

        System.out.println(orderObject.toString());
    }
}

This works as intended, however I dont see the advantage of using the interface-approach to using switch-case.

I still have to write 80 times:

serviceMethodMap.put("CAR_DOORS / WHEELS / COLORS / ...", new ServiceMethod() {

    @Override
    public void execute(String input) {
        orderObject.setCarDoors / -Wheels / -Colors / ...(input);
    }
});

This does not seem any shorter and even complexer?

Could anyone explain the advantages of using this approach?

added 3 characters in body
Source Link

How to refactor a long switch-case statement which contains various methods for each case? Loading attributes of cars and orders from JSON

How can I refactorget rid of this huge switch-case?

Should I refactor thisget rid of it at all? The switch-case works fine as it is, it is just very long and ugly.

How to refactor a long switch-case statement which contains various methods for each case?

How can I refactor this?

Should I refactor this? The switch-case works fine as it is, it is just very long and ugly.

Loading attributes of cars and orders from JSON

How can I get rid of this huge switch-case?

Should I get rid of it at all? The switch-case works fine as it is, it is just very long and ugly.

Added the JSON
Source Link
Loading
added the information for JSON
Source Link
Loading
Changed the title, removed tags that are not part of my problem, added some explanations to make my question more clear.
Source Link
Loading
Post Closed as "Needs details or clarity" by forsvarir, Marc-Andre, Quill, TheCoffeeCup, mdfst13
deleted 350 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Loading
Source Link
Loading