O
O
oandrew2011-06-30 01:25:44
OOP
oandrew, 2011-06-30 01:25:44

Question about OOP?

I am writing an application and I can not figure out how to do it right.
There are classes Page and PageHandler.

class Page {
    int x,y,height, width;
    ...
}

class PageHandler {
    vector<Page> pages;
    ...
}

The PageHandler itself encapsulates a set of pages.
How to correctly implement the resizing of one page, with the condition that the pages must always be placed vertically, that is, all pages are in contact with the borders.
So, if we reduce the page height by 50 px, then all the lower pages need to be moved up by 50 px.
Question: who should be responsible for such functionality?
If PageHandler, then you need to disable direct access to Page and do all operations only through PageHandler?
Or what's more logical?
P.S.
Another option came to mind. If necessary, simply change the page size. And then we run the PageHandler::updatePositions() method, which will run through all the pages and recalculate the x, y coordinates.
Only here the question is - in which method to do this - PageHandler::setSize(int page,int width,int height) or Page::setSize(int width, int height). In the second one, you will have to store a reference to the PageHandler in order to run updatePositions().

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rafael Osipov, 2011-06-30
@Rafael

In my opinion PageHandler is not quite a good term. I would choose Book.
Which contains Pages.
Since the page size cannot be larger than the book size (although there are rare funny exceptions), we store the page size in Book, not in Page.
When adding a page to a Book, we give it the size it needs to "squeeze" into.
Responsible for the functionality of shifting the pages of the book when the page is resized, the BookView class should be responsible, speaking in Model-View-Controller terminology.
The instance of which, in turn, sends a message to all instances of the PageView class that the dimensions are now different.

L
lalaki, 2011-06-30
@lalaki

x, y are not the properties of the page itself as a data element, but the properties of the presentation of a particular page, moreover, they are calculated:
the order of the pages is transferred to the general view, conditionally a “feed of pages”, and it calculates the location by their sizes.
therefore, there must be a separate change in the size of the page - in the page itself, separately - a change in the location - in the view, while they must be connected.
There are many options, the simplest and most universal for MVC: “1) change the data; 2) update the view”: first the page resize is called, then the view is redrawn, each does its own.
You can dig similar interface elements in different languages ​​and libraries - see how it is there, because. task is typical.

I
int02h, 2011-06-30
@int02h

Certainly not the pages should move themselves.
I would implement the required logic in the PageHandler by implementing the updatePositions() method (actually as you yourself suggested). That is, you resize the page, and then call the specified method. If you do not like direct access to pages, then you can move the functionality of changing page parameters to PageHandler. For example:

pageHandler->setPageSize(int pageIndex, Size size);

Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question