A simple OOP question

This question came to my mind quite a few times.

Let me explain my question with an example.

Let's say I have two classes: 1- Grid. 2- Cell.

Now the cell location should be stored in the grid class, and not in the cell class itself. Let's say that the cell wanted to get its location through the method in the grid.

How can I do that? Keep in mind that the cell was created / initialized by the Grid class.

What is a good approach to solve this problem?

thanks

+4
source share
13 answers

I do not think this is a good design. If a cell needs to know its location, it must contain it itself and provide methods for accessing it.

+9
source

I would look at it like this:

  • "Grid" is just a collection of "Cell" s
  • Each "cell" has its own location in the "Grid" as data elements.
  • Each "cell" may contain a "Data" object, which contains the actual contents of the user "Cells"

Thus, you can consider the โ€œcellโ€ as the coordinate inside the grid.

+4
source

Why should the cell know its location in the grid? It seems that the cell does not need to know where it is. If you still need to, the cell may contain a pointer to its closing grid, and it may request it for a location (for example, inside the cell class: getGrid (). GetLocation (this))

+2
source

A better approach would be to set the grid for a particular cell.

Another solution is to give the cell a pointer to the grid in the constructor. When a cell is requested for its location, it can return the location from the grid function.

+1
source

If you want the cell to know its location, just put it in the cell. There is no point in keeping it outside the class, and not that one cell can be used in different grids (since you already want to get its location and which places such a restriction).

Alternatively, if the cell method that needs to know the location is being called from the grid, then I just pass the location as parameters to this method.

In some very special cases, you can store a link to the grid in a cell or pass it as a parameter for it, but it just makes a mistake, introduces too many links.

+1
source

It's simple, just make the cell constructor take its location, suppose it's a constructor:

public: Cell(/*assume this is a generic type*/T value, int i, int j) { } 

Why would you do this? Good at first, because after you have created a cell of your location, it is pretty much fixed, unless you perform specialized operations such as "insert". I do not recommend calling methods in the Grid or storing the Grid instance locally.

+1
source

For me, it is not necessary that the cell knows its location in the Grid. The important thing is that not the Cell gets its location from the Grid, but the Grid gives the location of the cell when necessary! So Grid calls the Cell method to do this. Not the other way around.

The most logical is that the Grid contains many cells, so it "owns" them (for example, creates cells, moves them, or something else). Therefore, Cell should not know anything about the Grid.

The cell itself contains some information. This information may need to be updated or something else, depending on the location in the grid. Then you can have a method in the cell called by the grid to do this. This method can accept location parameters if necessary, but the cell itself does not contain them as members.

+1
source

As Neil says, if a cell needs to know its location, it must store it.

Speaking, I do not think that the cell needs to know its location. The grid is responsible for managing the cells, including their location, telling them where to draw themselves, etc. Cells must be responsible for what cells contain - for example, data, formatting data, etc.

Therefore, the cell never needs to know its location in the grid. You may have to do things like drawing itself, but this should be done in terms of the screen coordinates transmitted by the grid. The grid must control which cells are colored and where. Cells should just do what they are told and where.

+1
source

A grid can search for a cell by itself using some function, this function can be optimized using the cache or, possibly, a dictionary / map of cells with n-tuples of integers.

0
source

I would think about saving a position in each cell object, and the grid object should contain a collection of cells, the amount that was created. In this way, I will solve some problems that I will have to face.

0
source

If this is the responsibility of the grid , then you should let the cell know which grid belongs to. Add this to the cell constructor.

The only question that you should consider is really who is responsible for knowing the location.

0
source

I assume that you say that the location of the cell in the grid is not an integral part of its " Cell -ness", so to speak, that you want to have a Cell concept independent of the Grid , so the cell object should not have any information related to the grid.

I'm not sure how valuable this difference is, but if you want it, there are a couple of options:

  • Make Cell base class and GridCell from it. GridCell contains either a back pointer to the containing Grid object, or its location, as other answers indicate.
  • Use something like the Decorator pattern to add grid information and interfaces to the Cell object at runtime.

It smells again, as if we are doing it harder than this - I'm not sure it makes sense to have a Cell that is not part of the Grid , and that I do not see any harm when the Cell objects retain their position in the grid. Actually, it seems to me that it is easier for the Grid object to support a simple set of Cell objects, each of which is responsible for knowing its location in the Grid .

0
source

Factory Method Design Pattern

Define an interface for creating an object, but let your subclass decide which class should instantiate. The Factory method defines an interface for creating objects, but allows subclasses to decide which classes should be created.

0
source

Source: https://habr.com/ru/post/1309310/


All Articles