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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:12:32+00:00 2026-06-19T03:12:32+00:00

I often use the class-factory pattern whereby a class has a private constructor and

  • 0

I often use the class-factory pattern whereby a class has a private constructor and a static method to create the class. This allows for the situation where the class cannot be constructed for some reason, and a null is returned – very handy.

I would like to be able to extend this to a factory method which creates a particular class from a hierarchy of derived classes depending on conditions. However I can’t see a way of then hiding the constructors of the derived classes to force the use of the factory method. If the factory method is in the base class it no longer has access to the private constructors of derived classes. Putting a factory method in every derived class doesn’t work as the required type must then be known beforehand. Nested classes might be a way if a class had access to the private members of a nested class, but sadly it seems that the nested classes have access to the private members of the enclosing class, but not the other way round.

Does anyone know of a way of doing this?

  • 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-06-19T03:12:34+00:00Added an answer on June 19, 2026 at 3:12 am

    Here’s the sample code I was working on when Daniel posted his answer. It looks like it’s doing what he suggested:

    public static class BaseFactory
    {
        public static Base Create(bool condition)
        {
            if (condition)
            {
                return Derived1.Create(1, "TEST");
            }
            else
            {
                return Derived2.Create(1, DateTime.Now);
            }
        }
    }
    
    public class Base
    {
        protected Base(int value)
        {
        }
    
        protected static Base Create(int value)
        {
            return new Base(value);
        }
    }
    
    public sealed class Derived1: Base
    {
        private Derived1(int value, string text): base(value)
        {
        }
    
        internal static Derived1 Create(int value, string text)
        {
            return new Derived1(value, text);
        }
    }
    
    public sealed class Derived2: Base
    {
        private Derived2(int value, DateTime time): base(value)
        {
        }
    
        internal static Derived2 Create(int value, DateTime time)
        {
            return new Derived2(value, time);
        }
    }
    

    [EDIT] And for Daniel’s second suggestion:

    public static class BaseFactory
    {
        public static Base Create(bool condition)
        {
            if (condition)
            {
                return new Derived1Creator(1, "TEST");
            }
            else
            {
                return new Derived2Creator(1, DateTime.Now);
            }
        }
    
        private sealed class Derived1Creator: Derived1
        {
            public Derived1Creator(int value, string text): base(value, text)
            {
            }
        }
    
        private sealed class Derived2Creator: Derived2
        {
            public Derived2Creator(int value, DateTime time): base(value, time)
            {
            }
        }
    }
    
    public class Base
    {
        protected Base(int value)
        {
        }
    
        protected static Base Create(int value)
        {
            return new Base(value);
        }
    }
    
    public class Derived1: Base
    {
        protected Derived1(int value, string text): base(value)
        {
        }
    
        protected static Derived1 Create(int value, string text)
        {
            return new Derived1(value, text);
        }
    }
    
    public class Derived2: Base
    {
        protected Derived2(int value, DateTime time): base(value)
        {
        }
    
        protected static Derived2 Create(int value, DateTime time)
        {
            return new Derived2(value, time);
        }
    }
    

    Note that this second approach means that the classes can’t be sealed, unfortunately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all whenever I use the synchronized statement, I often use this pattern: private
I often use the following pattern to create a cancellable thread: public class CounterLoop
I often use convenience functions that return pointers to static buffers like this: char*
In code I've seen, some people often use private variables, e.g. private static int
In my Java code I often use the very handy method(Class... args) varargs. As
I often use this statement for extending class without needs of writing a whole
How often should I use static methods generally? If I have like: Class1 _class34
I have a custom class called Rows that implements IEnumerable<Row> . I often use
I often use the System.Data.Entity.DbExtensions Include() method to cause complex entity fields to be
I often take the classes that linq2sql generates and create a simple data-only class

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.