rlv-dan
rlv-dan

Reputation: 1104

Where to place a method for listing objects

I'm making my first Java project, and trying to learn OO programming as well as MVC.

Say that you have a class "Products" and a number of objects of that class. Your GUI want to display a list of these objects, possibly also with some filter etc. I can think of several ways to do this:

What is the proper way to do this, and where would you put the code?

Upvotes: 2

Views: 102

Answers (2)

melihcelik
melihcelik

Reputation: 4599

As you want to learn MVC, you already know that in order to implement MVC pattern you need three layers: Model, View, Controller. In your case, you have the two of the layers, Model (Product) and View (ProductGUI, assuming this is a different class). What you are missing is the Controller.

In order to leverage from MVC, I suggest you to implement the Controller layer with an API approach so that you have an interface, ProductController; and a class implementing the interface, ProductControllerImpl. This interface-class separation lets you to switch between alternative implementations and also allows Mocking your services for user interface testing (See Wikipedia for further explanations).

Let me also try to explain this with a simple example.

Assume you create the interface for your Controller layer as such:

public interface ProductController {
    public List<Product> listProducts();
    public List<Product> listProducts(NameFilter nameFilter);
}

Then you can create a Mock implementation for this interface for testing purposes as such:

public class ProductControllerImplMock implements ProductController {
    List<Product> products;

    public ProductControllerImplMock() {
        products = new ArrayList<Product>();
    }

    public List<Product> listProducts() {
        products.add(new Product("A"));
        products.add(new Product("B"));
        return products;
    }

    public List<Product> listProducts(NameFilter nameFilter) {
        products.add(new Product("A"));
        return products;
    }
}

Note: Assumining you have a NameFilter class that aims at filtering Product's by name.

If you decide to use a database to store your products, then you can implement ProductControllerDatabaseImpl which will query your database and retrieve entities for listing and which will most probably introduce WHERE clause to the query for listProducts(NameFilter) method.

Actually, things are never this simple. What is shown in this example will be referred to as DAO (Data Access Object) in more complex applications and there will be a separate layer, generally called Business layer instead of Controller layer, which will implement the actual logic for the application. But I tried to keep the example as simple as possible for simplicity.

Upvotes: 2

mre
mre

Reputation: 44250

IMHO, the OOP approach would be to expose the collection via a basic accessor method in the Products class. If you're concerned about the collection being modified, either wrap the collection with one of the unmodifiable collections made available by the Collections class, or return a copy.

Edit -
And concerning the GUI, be aware of concurrency-related issues.

Example -

final class Products
{
    private final List<Foo> listOfFoo;

    Products(List<Foo> listOfFoo)
    {
        this.listOfFoo = listOfFoo;
    }

    public List<Foo> getListOfFoo()
    {
        if(listOfFoo.isEmpty())
        {
            return Collections.emptyList();
        }

        return Collections.unmodifiableList(listOfFoo);
    }
}

Upvotes: 0

Related Questions