简体   繁体   中英

Aggregating-based architecture issues

i'm need your help again.

I have an document viewer application what can read two different kinds of documents:

  1. Special one (based on PDF, with custom header)
  2. Standart one ("raw" PDF).

With raw PDF viewer should acts like any other one.
With custom - execute some additional actions during it's opening,
which are not available to raw PDF.
These actions should be available later in the application's menu only. And only for custom document.

Project OOP architecture (designed by other man) looks like this:

class GenericDocument
    class PdfLibDocument
        class CustomDocumentHighLevel
            class CustomDocumentLowLevel

Ie each higher-level class contains the lower-level one as member:

class GenericDocument
{
   SmartPointer< PdfLibDocument > m_document;
   ...
};

and so on.

Custom document has much specific functionaly:

class CustomDocumentLowLevel
{
     public:
        void DoSomeBlackMagic();
        ...
        // Another black magic
};

The problem occurs then i need to "pull" some low-level method from CustomDocumentLowLevel to GenericDocument (to show in app menu) - because i need to add this method to ALL FOUR classes!
And probably in the future i need to "pull" more methods from custom document.
Looks like this software architecture is bad choice in such case, isn't it?

So i need find a way to refactor this code. Should i replace aggregating with inheritance? Introduce interfaces?

Usually composition is preferred over inheritance, but it doesn't seem applicable to your case. Why not to inherit Viewers from one another? Make a base viewer with a simple functionality, available all over and then inherit specialised one from it adding new functionality.

As for menu and actions, connected with it, they should be represented as a separate objects. Check Command pattern for that.

UI can request available command list from your Viewers. Each viewer can fill its internal command list on initialization or construction or whatever you choose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM