-1

I have two classes:

public class Car
{
    private int id;
    private String name;
    private String vendor;

    /* ... */
}

public class Garage 
{
    private int id;
    private String name;
    private String address;

    /* get-set and others stuffs */

    public boolean addCar(Car c) {
        /*
            This will insert a car to tblCars
            but belong to this garage
        */
    }

    public List<Car> getListCar {
        /* 
            This will perform a SELECT query on tblCars
            to choose cars belong to this Garage
        */
    }
}

Is it allowed in OOAD?

Bình Nguyên
  • 2,252
  • 6
  • 32
  • 47

2 Answers2

1

Allowed? What do you think is going to happen - an OOAD cop pull you over to the side and give you a ticket?

You can do anything you want. You'll find out if it's misguided after the fact.

Your design looks acceptable, except the Garage does not have a Collection<Car>.

This is a one-to-many relationship between Car and Garage. That's how it's usually done.

I think the Smalltalk MVC ideas are a little outdated. Here's how I see layers for a web app:

view-->controller-->service--+-->persistence
                             |
                             +--> model

Controller and view and tightly coupled together.

The service is the thing that knows about DAOs and model objects. They fulfill use cases and own units of work.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Thanks, I mean, for example, in 3-tier design pattern, Presentation layer shouldn't make a direct call to Data Access Layer but throught a Control layer, so, are these any rules that prohibit one entity call/access/return to other entites? – Bình Nguyên May 03 '13 at 13:01
  • view should have no reference to controller (in MVC). The arrow should be from controller to view. – Oliver Watkins May 03 '13 at 13:14
  • It's intended to show info flow, not dependency – duffymo May 03 '13 at 13:56
1

It all depends on your requirements. If you are required to save a car every time it goes into a garage then use a delegate to handle the DB insert :

public boolean addCar(Car c) {
    Delegate.getInstance().addCarToGarage(this, c);
}

public List<Car> getListCar {
    Delegate.getInstance().getAllCars();
}

Otherwise just leave it as

public boolean addCar(Car c) {
    myCarList.add(c);
}

public List<Car> getListCar {
    return myCarList();
}

And save it at some other point.

Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225