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 6103579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:44:41+00:00 2026-05-23T13:44:41+00:00

The following code works very well when all involved classes are in the same

  • 0

The following code works very well when all involved classes are in the same project (determineSubClass is a member of BaseClass):

protected static BaseClass determineSubClass(String p1, int p2, Boolean p3) {

    BaseClass baseObj = null;
    if ( (baseObj = SubClassOne.ofType(p1, p2, p3)) != null )
      return baseObj;
    else if ( (baseObj = SubClassTwo.ofType(p1, p2, p3)) != null )
      return baseObj;
    else if ( (baseObj = SubClassThree.ofType(p1, p2, p3)) != null )
      return baseObj;
    else if ( (baseObj = SubClassFour.ofType(p1, p2, p3)) != null )
      return baseObj;
    else
      return new SubClassDefault(p1, p2, p3);
}

But now, I want to move the BaseClass to a shared Library project, in which SubClassOne, SubClassTwo, SubClassThree and SubClassFour are not defined in the library but rather in the applications using this library.

I could of course move BaseClass back to each and every application using this library, but I wonder:

  • Is there a better solution?
  • Is there a solution that would let me
    keep BaseClass in the Library
    project and eliminate the need for it
    to know about all superclasses
    derived from it?

EDIT (answering @ahmet alp balkan question below):

ofType() of each subclass does 2 things:

  1. Determines, based on the content of
    String p1 and the other parameters
    p2 and p3, whether the subclass to
    be instantiated is of its type.
  2. If the answer is positive, it
    instantiates an object of self
    subclass. Otherwise, returns null.

As for your second question, BaseClass at this point holds common data members and methods to all subclasses and only this single static method which is aimed at delegating the responsibility of determining subclass to be instantiated.

BTW, thanks to your question I noticed a horrible typo in my original post: “SuperClassOne” should be “SubClassOne” etc.

  • 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-23T13:44:41+00:00Added an answer on May 23, 2026 at 1:44 pm

    Your static determineSubClass method is a factory method. It should obviously not be located on the BaseClass, since not only the base class should not know anything about the subclasses, but in your case it can’t know anything about it, since you want to locate the base class in another project. No, this method should be located in a factory class that is responsible for creating BaseClass instances. What you should do is define an interface (or base type) for creating BaseClass instances next to the BaseType and defines an implementation in the composition root of your application. When you have multiple applications, they probably each have a different set of BaseClass sub types, so each application will have a different factory. When you have this construction in place, you can inject the factory into classes that need BaseClass instances.

    It might look something like this:

    // Shared library
    public interface IBaseClassFactory
    {
        BaseClass CreateNew(String p1, int p2, Boolean p3);
    }
    
    public abstract class BaseClass
    {
    }
    
    // Application code
    public class SubClassOne : BaseClass
    {
    }
    
    public class SubClassTwo : BaseClass
    {
    }
    
    // Note that this consumer depends on IBaseClassFactory.
    public class SomeConsumer
    {
        private IBaseClassFactory baseClassFactory;
    
        public SomeConsumer(IBaseClassFactory factory)
        {
            this.baseClassFactory = factory;
        }
    
        public void Consume()
        {
            BaseClass instance = this.baseClassFactory
                .CreateNew("foo", 0, false);
    
            // use instance
        }
    }  
    
    // Composition root
    class BaseClassFactory : IBaseClassFactory
    {
        public BaseClass CreateNew(String p1, int p2, Boolean p3)
        {
            BaseClass baseObj = null;
    
            if ((baseObj = SubClassOne.ofType(p1, p2, p3)) != null)
               return baseObj;
            // etc
            else
                return new SubClassDefault(p1, p2, p3);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code works great in IE, but not in FF or Safari. I
The following code works for 90+ % of global security groups, but for one
The following html code works in Firefox, but for some reason fails in IE
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
I was loading an image resource with the following code and it works fine
Given the following code,the hover function works correctly, but when clicked, loses the clicked_no_event_box
I have code that looks like the following, which works fine for displaying the
I'm trying to show all images within a specified directory. The following code lists
I've got the following requirement that works well in the OO space but I

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.