Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 959731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:04:21+00:00 2026-05-16T01:04:21+00:00

I am using an abstract factory to create user interface components such as dialogs.

  • 0

I am using an abstract factory to create user interface components such as dialogs. The abstract factory used is returned from a currently selected generic “INode” which is the base class for several different types of node. So for instance, if I want to add a new node of the same type as the selected node, the scenario goes something like this:

(please note this is semi-pseudo code)

User clicks node and the node gets stored for later use:

void onTreeNodeSelected(INode *node)
{
    selectedNode = node;
}

User clicks “add” on the user interface:

void onAddClicked()
{
    IFactory *factory = selectedNode->getFactory();
    Dialog *dialog = factory->createAddDialog(parentWidget);
    dialog->show();
}

Which all seems fine. The problem comes when I want to edit the selected node:

void onEditClicked()
{
    IFactory *factory = selectedNode->getFactory();
    Dialog *dialog = factory->createEditDialog(selectedNode, parentWidget);
    dialog->show();
}

Oh dear.. I’m passing in an INode object. At some point I’m going to have to downcast that to the correct node type so the dialog can use it properly.

I’ve studied the “PostgreSQL Admin 3” source code, and they do something similar to this. They get round it by doing something like this:

FooObjectFactoryClass::createDialog(IObject *object)
{
    FooObjectDialog *dialog = new FooObjectDialog((FooObject*)object);
}

Yeck.. cast!

The only way I can think around it and still able to use my factories is to inject the node itself into the factory before it is returned:

FooNode : INode
{
    FooNodeFactory* FooNode::getFactory()
    {
        fooNodeFactory->setFooNode(this);
        return fooNodeFactory;
    }
}

So then my edit event can do this:

void onEditClicked()
{
    IFactory *factory = selectedNode->getFactory();
    Dialog *dialog = factory->createEditDialog(parentWidget);
    dialog->show();
}

And it will use the injected node for context.

I suppose if there is no injected code, the createEditDialog could assert false or something.

Any thoughts?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:04:22+00:00Added an answer on May 16, 2026 at 1:04 am

    A common solution is “double-dispatch”, where you call a virtual function on one object, which in turn calls a virtual function on the other, passing this, which now has the correct static type. So, in your case, the factory can contain “create” functions for the various types of dialogues:

    class IFactory
    {
    public:
        ....
        virtual Dialog* createEditDialog(ThisNode*, IWidget*);
        virtual Dialog* createEditDialog(ThatNode*, IWidget*);
        virtual Dialog* createEditDialog(TheOtherNode*, IWidget*);
        ....
    };
    

    then each type of node has a virtual createEditDialog that dispatches to the correct factory function:

    class INode
    {
    public:
        ....
        virtual Dialog* createEditDialog(IWidget* parent) = 0;
        ....
    };
    
    class ThisNode : public INode
    {
    public:
        ....
        virtual Dialog* ThisNode::createEditDialog(IWidget* parent)
        {
            return getFactory()->createEditDialog(this, parent);
        }
        ....
    };
    

    Then you can create the correct dialogue as

    void onEditClicked()
    {
        Dialog *dialog = selectedNode->createEditDialog(parentWidget);
        dialog->show();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a hierarchy of generic collection classes that derive from an abstract
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace EfTestFactory { public abstract class
Currently, I use an abstract factory to allow the specification of a custom class
How the factory pattern is using inheritance and abstract factory using composition to return
I am using an abstract factory to return instances of concrete subclasses.I would like
Using an EntityManager in an abstract class, I am trying to create a function
Short question Where should I put the abstract factory interface and the actual factory?
I have a simple Poco-Model using abstract classes, and it seems not to work
I'm using a abstract class in another module for reading and input for my
How can I test a private method of an abstract class using reflection (using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.