After doing design, you may end up with a class diagram. When the implementation part of a project begins you will have to code those classes. There are certain techniques that make mapping a diagram to code easier. This significantly improves the speed at which implementation happens. I will illustrate this using an example. The example will be using Java.
First, let’s start with the Car class. First, the attributes are easy enough to code. The + or – at the left of the attributes tells us if the attribute is public or private, + is public and – is private. After the sign, there’s the attribute name and the type. For the Car class, the attributes will be as follows:
public String modelType;
public int Doors;
public String autoMaker;
For the methods, you just have to write the name, if the method has arguments, the arguments should also be written in the code. After the name of the method, the return type may be defined. If the method takes arguments, the type must be inferred. Of course, the body of the method isn’t defined, the developer will have to do that himself. For the Car class, the method will be as follows:
private void Radio() {}
public void windshieldWiper() {}
public void changeDirection() {}
The last part is mapping the relationship between the classes. In this case, the cardinality of the relationship is what’s important. For the Car class, the relationship getRoadTrip will turn into a method. The return type of the method is the receiving class, RoadTrip. The cardinality defines if the method returns one object or an array of objects. If the cardinality is 1, it returns an object and if the cardinality is *, it returns an array. The method will be as follows:
RoadTrip[] getRoadTrip() {}
Following these rules, the RoadTrip class will be as follows:
public String snacks;
public int fuelLevel;
private String radioStation;
public void route(int start, int end) {}
private void detour() {}
private void pottyBreak() {}
Car assignedCar() {}
The language choice matters here. Ideally, you will want to choose an object-oriented language. It is possible to depict objects without using classes, but it’s a bit more complicated. You will mainly want to do this if the language offers something that object-oriented languages can’t. This an example of how to depict an object in Python without using classes.
Knowing how to properly map a diagram to code can greatly speed up the implementing process. Sure, the implementation of the methods isn’t included in the diagram, but the structure of the code is mostly done. This type of mapping can simplify a developer’s work.