Reputation:
Let's suppose we have a shop that sells cars. So, we have a class that describes order:
class Order{
private int id;// THIS IS ID OF THE ORDER
private Date date;//THIS IS THE DATE OF THE ORDER
private BigDecimal sum;//THIS IS THE SUM OF THE ORDER
private int carId;//THIS IS ID OF THE CAR
private int carColor;//THIS IS COLOR OF THE CAR
}
What confuses me are two fields - carId and carColor. According to OOP principles - I should have class Order and class Car. However, I don't think that in such situation I need class Car because, I am not going to work with car - only with its color. So we have some fields which are linked with order and some fields linked with car. Is this code wrong?
Upvotes: 0
Views: 67
Reputation: 1486
OOP should look like this:
class Order {
private int id; //THIS IS ID OF THE ORDER
private Date date; //THIS IS THE DATE OF THE ORDER
private BigDecimal sum; //THIS IS THE SUM OF THE ORDER
private Car car; //THIS IS THE CAR, bear in mind, this only works if you have 1 car for 1 order!
//getters and setters
}
class Car {
private int carId; //THIS IS ID OF THE CAR
private Color carColor; //THIS IS COLOR OF THE CAR
//getters and setters
}
What good is an Order
object with only a Color
?
If you want to find orders with a specified Color
make another class
class SearchModel {
private List<Order> orders;
public List<Order> getOrdersByCarColor(Color carColor) {
List<Order> res = new ArrayList<Order>();
for (Order order : orders) {
if (order.getCar().getCarColor() == carColor)
res.add(order);
}
return res;
}
}
Upvotes: 0
Reputation: 1279
Your code is't wrong, but it conflicts with OOP principle
If you want to add properties to Car, you don't have Car class, you can't extend it
So you must edit class Order, so you conflict Open-closed principle
You need to create two class is Car and Order
Upvotes: -1
Reputation: 3966
I would implement a own Car
class and add the car properties there. Because if we assume your code grows and the Car
gets some other properties e.g. size, weight, consumption, etc. it is easier to maintain the code. Same goes for testing.
Also when you want to get some information about specific cars it is better to iterate over the list of cars instead of the orders.
Code example:
class Car {
private int carId;
private int carColor;
// Additional information
private double consumption;
....
}
class Order {
private int id;
private Date date;
private BigDecimal sum;
// An order can also contain more than one car
private List<Car> cars;
...
}
Upvotes: 0
Reputation: 3256
Yes, you need to have two classes.
you should be able to get cars from Order by orderID
One order -> many Cars (1:M) ->
new Order(orderID)->getCars()-> each iteration -> getColor()
Upvotes: 2
Reputation: 6363
Yes it is wrong. If for some reason carId
becomes hash your code will be broken. That's why you should define car properties inside it's own class and here just have a reference to it.
Moreover if your car object changes in memory, your order object won't be updated as it has a value of carColor
, not a reference to it.
Upvotes: 0