Answer the question
In order to leave comments, you need to log in
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;
...
}
Answer the question
In order to leave comments, you need to log in
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.
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.
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question