oop - Java architectural class design for a real world senario -


this problem need implement in java:

a car can petrol car or diesel car, , hybrid car plugged petrol or diesel not both. also, hybrid car has ability run on electricity, while not using petrol or diesel @ , decided @ run time whether select electricity or other fuel source (petrol or diesel appropriate).

here need implement oop concepts in mind example when hybrid car running in petrol mode method in petrol type should invoked if diesel diesel class running method should invoked.

i new oop , came following design know it's wrong if 1 can me please.

and tried decorator design pattern correct me if deign wrong 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 @ run time user can select whether car 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 correct there short of issues regarding oop best practice in advance

i use single class 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 enum use 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());        }     } } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -