简体   繁体   English

用于现实世界场景的Java架构类设计

[英]Java architectural class design for a real world scenario

This is the problem I need to implement it in Java: 这是我需要在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. 在这里我需要实现OOP概念,例如当应该调用汽油类型的汽油模式方法中运行的混合动力汽车时,以及如果应该调用柴油类运行方法的柴油。

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. 我是OOP的新手,我想出了以下设计我知道如果有人能帮助我,那就错了。

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 这是正确的是有关OOP最佳实践的任何问题

I would use a single class with an EnumSet of fuels. 我会使用一个带有EnumSet燃料的单一类。

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. 如果不使用Enum,您将使用不可变类。

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());
       }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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