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

  • Home
  • SEARCH
  • 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 6174145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:42:07+00:00 2026-05-23T23:42:07+00:00

I have a generic object factory FactoryBase<T> with a factory method: public abstract class

  • 0

I have a generic object factory FactoryBase<T> with a factory method:

public abstract class FactoryBase<T> where T : new()
{
    public virtual T CreateInstance()
    {
        T thing = new T();

        // Tweak 'thing' a bit...

        return thing;
    }
}

This works great for creating concrete factories for various types:

public class FruitFactory : FactoryBase<Fruit>
{
    public override Fruit CreateInstance()
    {
        Fruit fruit = base.CreateInstance();

        // Do some Fruit polishing stuff...

        return fruit;
    }
}

However, the pattern breaks down if I want to create a factory type derived from Fruit:

First attempt: (WORKS, but INCORRECT)

Here, AppleFactory directly inherits FactoryBase<Apple> — this is wrong because AppleFactory should rely upon FruitFactory to build a nice polished Fruit upon which to build our Apple:

// Should inherit FruitFactory, not FactoryBase
public class AppleFactory : FactoryBase<Apple>
{
    public override Apple CreateInstance()
    {
        Apple apple = base.CreateInstance();

        // FruitFactory is left out above, so...
        // ...we have to do all the Fruit polishing stuff...
        // ... and any apple stuff...

        return apple;
    }
}

Second attempt: (Complete fail)

Inheriting FruitFactory is clearly an error because it is non-generic and returns a Fruit not an Apple:

// COMPILE ERRORS
public class AppleFactory : FruitFactory
{
    public override Apple CreateInstance()  // ERROR: Override method must return Fruit
    {
        Apple apple = base.CreateInstance(); // ERROR: FruitFactory returns a Fruit
        return apple;
    }
}

The “Ugly” Solution?

By refactoring FruitFactory into a genericFruitFactory<T> I can create a properly derived AppleFactory:

public abstract class FruitFactory<T> where T : Fruit, new()
{
    public override T CreateInstance()
    {
        T fruit = (T)base.CreateInstance();

        // Do some Fruit polishing stuff...

        return fruit;
    }
}

// AppleFactory that property calls FruitFactory<Apple>
public class AppleFactory : FruitFactory<Apple>
{
    public override Apple CreateInstance()
    {
        Apple apple = base.CreateInstance();

        // ... ONLY apple stuff needed!!!

        return apple;
    }
}

I purposely make FruitFactory<T> abstract because it seems ugly and redundant for client code to call upon a a FruitFactory<Fruit>. Thus, I provide a non-generic FruitFactory which needs no body to be fully functional:

// Concrete FruitFactory
public class FruitFactory : FruitFactory<Fruit> { }

Complaints:

Each time I need a factory for a more-derived type I have to convert the corresponding base type’s factory to an abstract generic.

In other words, if I have a FooFactory I have to convert it to a FooFactory<T> in order to create DerivedFooFactory : FooFactory<DerivedFoo>

Simultaneously, I must make a new concrete FooFactory : FooFactory<Foo>.

Or… I could the generic factories directly:

var fruitFactory = new FruitFactory<Fruit>(); // Double-Fruity! sigh...

Is there a cleaner way to create an AppleFactory that first gets an Apple out of a FruitFactory without needing generic classes for each ‘middle-man’ type in the inheritance chain?

Note: I really don’t want to use reflection or complex schemes.

  • 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-23T23:42:07+00:00Added an answer on May 23, 2026 at 11:42 pm

    I’m still having trouble following your question, but I think it would be solved by making FruitFactory<T> non-abstract:

    public class FruitFactory<T> : FactoryBase<T> where T : Fruit, new()
    {        
        // I'm assuming this is in FactoryBase<T>
        public override T CreateInstance()
        {
            return new T();
        }
    }
    

    Then you can derive from that and override CreateInstance when you write AppleFactory.

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

Sidebar

Related Questions

I have a generic class in C# with 2 constructors: public Houses(params T[] InitialiseElements)
I have a generic list... public List<ApprovalEventDto> ApprovalEvents The ApprovalEventDto has public class ApprovalEventDto
I have a method of an object which is something like a factory. You
Why isn't Collection.remove(Object o) generic? Seems like Collection<E> could have boolean remove(E o); Then,
I have a method to return a group of objects as a generic list
I have a generic class that should allow any type, primitive or otherwise. The
I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T>
I have a generic class that I'm trying to implement implicit type casting for.
I have created a generic method that enables clients to specify both the concrete
Suppose I have a factory method, which wants to construct an instance of a

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.