Am sure that , everyone will be aware of the Transformers ( Optimus Prime , Megatron etc).
Am trying to represent that in the form of classes and interface. For now, am not considering the attributes. Am just taking some functionalities.
My Design is
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 Transformer
says, it can perform both Car and Robot operations
During instantiation
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 ,here two objects that are getting created Robot R
and Car C
. So, this convey the Robot is getting created and Car is getting created . But what I want is The Car is Getting Transformed to A Robot and Vice Versa
How to implement this in design.
Class I units are insulated and protected by an earth terminal. Class II transformers have double insulation or reinforced insulation. Class III transformers have outputs at Safety Extra-Low Voltages (SELV) below 50 V ac or 120 V dc.
The Class I input power supply allows the use of protective ground conductors as a means to provide safety from electrical shock in addition to insulation and spacings. Class II does not have a ground connection. There are only two connections, namely line, and neutral connection.
The two most common and basic designs of transformer construction are the Closed-core Transformer and the Shell-core Transformer. In the “closed-core” type (core form) transformer, the primary and secondary windings are wound outside and surround the core ring.
As I understand it, you want the Transformer class to have either Robot functionality OR Car functionality but not both at the same time.
To model that, I would favor Composition over Inheritance.
e.g.:
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;
}
To instantiate a new Transformer, you would need to define its Car-Form and its Robot-Form, e.g.:
new Transformer(new OptimusPrimeRobot(), new GiantTruck());
with the Constructor:
Transformer(Robot r, Car c) {
this.robot = r;
this.car = c;
}
with OptimusPrimeRobot implementing Robot and GiantTruck implementing Car.
If you want, you can even lazy initialize your contained classes. Define fields:
Class carClass;
Class robotClass;
and a Constructor:
Transformer(Class robotC, Class carC) {
this.robotClass = robotC;
this.carClass = carC;
}
Then make getter Methods for Robot and Car:
private Robot getRobot() {
if(robot == null) {
robot = robotClass.newInstance();
}
return robot;
}
and adjust your code, so it uses the Getters instead of the fields:
getRobot().fight();
Now you just specify what Classes your Transformer consists of and their objects are only instantiated when needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With