Design classes for transformers

I am sure that everyone will know about Transformers (Optimus Prime, Megatron, etc.).

I am trying to present this as classes and an interface. While I do not consider attributes. Just perform some functions.

My project

interface Car{
 public void run();
 public void stop();
}

interface Robot{
 public void walk();
 public void fight();
}

class Transformer implements Car, Robot {
 // implementing all the methods
}

So the class Transformersays that it can perform Car and Robot operations

During instance creation

Robot R = new Transformer(); // Now the transformer is in Robot format
Car C = new Transformer();  // Now the transformer is in Car format

My question is: two objects are created here Robot Rand Car C. So this means that the robot is createdand Car is created . But I wantThe Car is Getting Transformed to A Robot and Vice Versa

How to implement this in design.

+5
source share
7 answers

Just some form appeared here ...

, WALKS CAR . , . , !

public class TransformersClass{

    public TransformersClass() {

        Car transformer = new Transformer(); // Initially they are Cars
        Robot robot = (Robot) transformer; // Now transformed to Robot. No new objects are created

    }

}

class Transformer implements Robot, Car {


    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

    @Override
    public void walk() {
        // TODO Auto-generated method stub

    }

    @Override
    public void attack() {
        // TODO Auto-generated method stub

    }

    @Override
    public void fire() {
        // TODO Auto-generated method stub

    }

}


interface Robot {
    public void walk();
    public void attack();
    public void fire();

}

interface Car {
    public void run();
}
0

, , Transformer Robot, Car Car, .

, .

:.

class Transformer {

private Car car;
private Robot robot;
private Class currentState = Car.class;

 public void fight() {
   if (currentState.equals(Robot.class)
      robot.fight();
   }

 public void drive() {
  if (currentState.equals(Car.class)
      car.drive();
  }

 public void transform() {
  if (currentState.equals(Car.class) 
      currentState = Robot.class;
  else
      currentState = Car.class;
}

Transformer, Car-Form Robot-Form, :

new Transformer(new OptimusPrimeRobot(), new GiantTruck());

:

Transformer(Robot r, Car c) {
   this.robot = r;
   this.car = c;
}

OptimusPrimeRobot, Robot GiantTruck, Car.

, . :

Class carClass;
Class robotClass;

:

Transformer(Class robotC, Class carC) {
   this.robotClass = robotC;
   this.carClass = carC;
}

getter :

private Robot getRobot() {
  if(robot == null) {
     robot = robotClass.newInstance();
  }
  return robot;
}

, Getters :

   getRobot().fight();

, , .

+4

, . - , , . . , , - :

class Transformer implements Car, Robot {

  public Car transformToCar() {
     return (Car)this;
  }

  public Robot transformToRobot() {
    return (Robot) this;
  }
}

:

Transformer optimus = new Transformer();
Car optimusCar = optimus.transformToCar();
//but this is the same as writing:
Car optimusCar = (Car) optimus;

, transformToXXX() , Transformer , .

EDIT: , , transformToCar() Robot transformToRobot() Car. Transformer- .

+1

:

interface TransformableCar extends Car {

    TransformableRobot asRobot();
}

Robot :

interface TransformableRobot extends Robot {

    TransformableCar asCar();
}

, :

public class Megatron {

    private MegatronCar car = new MegatronCar();
    private MegatronRobot robot = new MegatronRobot();

    public TransformableCar asCar() {
        return car;
    }

    public TransformableRobot asRobot() {
        return robot;
    }

    class MegatronCar implements TransformableCar {

        @Override
        public TransformableRobot asRobot() {
            return Megatron.this.asRobot();
        }

        // TODO: Implement Car methods
    }

    class MegatronRobot implements TransformableRobot {

        @Override
        public TransformableCar asCar() {
            return Megatron.this.asCar();
        }

        // TODO: Implement Robot methods
    }
}

:

Megatron megatron = new Megatron();
TransformableCar car = megatron.asCar();
TransformableRobot robot = car.asRobot();
+1

strategy Transformer , RobotStrategy CarStrategy. TransformerStrategy

public interface TransformerStrategy{
    public void move();//implemented by both
    public void stop();//implemented by both
    public void fight();//implemented by only Robot, NO-OP for car
    //etc
}
+1

. Collections. .

public static Robot transformCar(Car car){
        return (Robot)car;
    }
+1

, .

. , , , , . , , , , . Transformer, Car and Robot, . , , , - . .

0

All Articles