简体   繁体   中英

Java architectural class design for a real world scenario

This is the problem I need to implement it in Java:

A Car can be a Petrol car or a Diesel Car, and a Hybrid Car is plugged with Petrol or Diesel but not both. Also, a hybrid car has the ability to run on electricity, while not using the petrol or diesel at all and it is decided at run time only whether to select electricity or the other fuel source (petrol or diesel as appropriate).

Here I need to implement with OOP concepts in mind for an example when the Hybrid car running in petrol mode method in the petrol type should be invoked as well as if its diesel the diesel class running method should be invoked.

I am very new to OOP and I came up with the following Design I know It's wrong if any one can help me please.

And I tried this with decorator design pattern just correct me if the deign is wrong for the above scenario..

Car Interface

public interface Car {
        public void running();
}

Petrol Car Class

class PetrolCar implements Car{

    public void running() {
        System.out.println("Running in Petrol");
    }

}

Diesel Car class

public class DieselCar implements Car{

    public void running() {
        System.out.println("Running in Diesel");
    }

}

Abstract Hybrid

public abstract class Hybrid implements Car{

    Car car;

    public Hybrid(Car car) {
        this.car=car;
    }

    public void running(){
        car.running();
    }

    abstract void hybridRunning();

}

Implementing Hybrid class

public class HybridCar extends Hybrid{

    public HybridCar(Car car) {
        super(car);
    }

    @Override
    void hybridRunning() {
        System.out.println("Running in Hybrid Mood");
    }

}

Testing as at run time user can select whether the car is hybrid petrol or hybrid diesel or petrol or diesel...

public class App {

    public static void main(String[] args) {
        String neededType = "Petrol";
        boolean hybrid = true;

        if (hybrid) {
            Hybrid hCar=null;
            if (neededType.equalsIgnoreCase("Petrol")) {
                hCar=(Hybrid)new HybridCar(new PetrolCar());    
            } else if (neededType.equalsIgnoreCase("Diesel")) {
                hCar=new HybridCar(new DieselCar());
            }
            hCar.hybridRunning();
            hCar.running();
        } else {
            Car car=null;
            if (neededType.equalsIgnoreCase("Petrol")) {
                car=new PetrolCar();
            } else if (neededType.equalsIgnoreCase("Diesel")) {
                car=new DieselCar();
            }

        }
    }
}

Is this Correct is there any short of issues regarding to OOP best practice

I would use a single class with an EnumSet of fuels.

public interface Car {
    static Car create(Fuel fuel, Fuel... others) {
        return new CarImpl(EnumSet.of(fuel, others));
    }

    Set<Fuel> fuels();
    void running();

    enum Fuel {
       Petrol, Diesel, LPG, Hydrogren, Electric
    }
}

Without using an Enum you would use an immutable class.

public interface Car {
    static Car create(Fuel fuel, Fuel... others) {
        Set<Fuel> fuels = new HashSet<>();
        fuels.add(fuel);
        Collections.addAll(fuels, others);
        return new CarImpl(fuels);
    }

    Set<Fuel> fuels();
    void running();
    void setMode (Fuel fuel) throws IllegalArgumentException;
     Fuel getMode ();

    class Fuel {
       private final String name;
       public Fuel(String name) { this.name = name; }
       public String name() { return name; }
       public String toString() { return name(); }
       public int hashCode() { return name().hashCode(); }
       public boolean equals(Object o) {
           return o instnaceof Fuel && ((Fuel) o).name().equals(name());
       }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM