Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Design for Transformers

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.

like image 406
madhairsilence Avatar asked Jul 19 '12 10:07

madhairsilence


People also ask

What is the class of transformer?

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.

What is the difference between Class 1 and Class 2 transformers?

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.

What is the basic design of a transformer?

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.


1 Answers

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.

like image 68
Jonas Eicher Avatar answered Sep 22 '22 00:09

Jonas Eicher