PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How avoid switching or if/else with differents objects

 October 25, 2022     java, oop     No comments   

Issue

I have a case where I would like to avoid the switch or if/else structure. I have two objects that perform the same action but are completely different. I have tried to use polymorphism but since they are so different it has not worked.

The idea is to have a method that performs that action and that can grow in the future.

public class Motorbike { 
  private String handlerbar; 
  private String wheelA;
  private String wheelB;

  public void repair(String handlerbar, String wheelA, String wheelB) {...}
  ...
}    

public class Car {
  private String steeringWheel;
  private String wheelA;
  private String wheelB;
  private String wheelC;
  private String wheelD;

  public void repair(String steeringWheel, String wheelA, String wheelB, String wheelC, String wheelD) {...}

}


public class Mechanic {
  
  public void repairVehicle(String type, String handlerbar, String steeringWheel, String wheelA..){
  
  //structure to avoid
  if (type == "car") {
      Car c = new Car(...);
      c.repair(...);
     ...
  } else if (type == "motorbike"){
    ... 
  }
}

Solution

Here's an implementation using the visitor pattern. Each vehicle has a repeair method that accepts a mechanic. The vehicle knows what parts it has so it can instruct the mechanic to repair them. That way the mechanic can repair everything without knowing what parts the vehicle has.

You can simply run

Mechanic mechanic = new Mechanic();
mechanic.repairVehicle(new Motorbike());
mechanic.repairVehicle(new Car());

and it will print

Repairing handlebar bar of Motorbike
Repairing wheel front of Motorbike
Repairing wheel rear of Motorbike
Repairing steering wheel steer of Car
Repairing wheel front-left of Car
Repairing wheel front-right of Car
Repairing wheel rear-left of Car
Repairing wheel rear-right of Car

The code for that is

interface Vehicle {
    String name();
    void repair(Mechanic mechanic);
}
 
class Motorbike implements Vehicle {
    private String handlerbar = "bar";
    private String wheelA = "front";
    private String wheelB = "read";
 
    @Override
    public String name() {
        return "Motorbike";
    }
 
    @Override
    public void repair(Mechanic mechanic) {
        mechanic.repairHandlebar(this, this.handlerbar);
        mechanic.repairWheel(this, wheelA);
        mechanic.repairWheel(this, wheelB);
    }
}
 
class Car implements Vehicle {
    private String steeringWheel = "steer";
    private String wheelA = "front-left";
    private String wheelB = "front-right";
    private String wheelC = "rear-left";
    private String wheelD = "rear-right";
 
    @Override
    public String name() {
        return "Car";
    }
 
    @Override
    public void repair(Mechanic mechanic) {
        mechanic.repairSteeringWheel(this, steeringWheel);
        mechanic.repairWheel(this, wheelA);
        mechanic.repairWheel(this, wheelB);
        mechanic.repairWheel(this, wheelC);
        mechanic.repairWheel(this, wheelD);
    }
}
 
 
class Mechanic {
 
    public void repairVehicle(Vehicle vehicle) {
        vehicle.repair(this);
    }
 
    public void repairHandlebar(Vehicle vehicle, String handlerbar) {
        System.out.println("Repairing handlebar " + handlerbar + " of " + vehicle.name());
    }
 
    public void repairWheel(Vehicle vehicle, String wheel) {
        System.out.println("Repairing wheel " + wheel + " of " + vehicle.name());
    }
 
    public void repairSteeringWheel(Vehicle vehicle, String steeringWheel) {
        System.out.println("Repairing steering wheel " + steeringWheel + " of " + vehicle.name());
    }
}


Answered By - zapl
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing