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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:27:07+00:00 2026-05-26T22:27:07+00:00

This is my abstract class which must be derived each time I want to

  • 0

This is my abstract class which must be derived each time I want to make a Singleton:

public abstract class Singleton<T> where T : Singleton<T>
{
    private static readonly Lazy<T> _instance = new Lazy<T>(() =>
    {
        var constructor = typeof(T).GetConstructor(BindingFlags.NonPublic |
            BindingFlags.Instance, null, new Type[0], null);

        return (T)constructor.Invoke(null);
    });
    public static T Instance { get { return _instance.Value; } }
    public Singleton() { }
}

So, every time I need to follow the Singleton design pattern, I can just make this:

sealed class Server : Singleton<Server>
{
    private Server() { }
    ...
}

Is this completely right, and, if not, why?

Edit:

  • Added private constructor on derived class example and invoking on abstract base.

Edit:

  • Reworked type parameter initialization.
  • 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-26T22:27:08+00:00Added an answer on May 26, 2026 at 10:27 pm

    Self implemented singletons are an anti-pattern. The need to inherit, and lock your classes into a specific form goes away if you just implement a factory:

    public class Server {} //Not coupled to any inheritance hierarchy.
    
    public class Factory
    {
        private readonly Lazy<Server> _server = new Lazy<Server>(() => new Server());
    
        public Server Server { get { return _server.Value; } }
    }
    

    However, you’re really using the factory as a service locator and service locator is also considered an anti-pattern as you can easily just use DI to inject the Server instance into your consuming classes.

    public class MyServerConsumer
    {
        public MyServerConsumer(Server server)
        {
          //Do stuff.      
        }
    }
    

    Windsor style registration:

     ... 
     Component.For<Server>();
     ...
    

    Notice that the word singleton is never mentioned? You still get ‘a single instance of an object’, but you don’t have to write code to maintain that relationship, and your classes are not constrained and corrupted by the concept of ‘singleton’ from the start

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

Sidebar

Related Questions

I want something like this public abstract class abc { public abstract void test();
I have an abstract base class from which many classes are derived. I want
I have a base class which contains an abstract/MustInherit method. I want this method
I frequently find myself creating classes which use this form (A): abstract class Animal
public abstract class MyAbs implements Comparable<MyAbs> This would work but then I would be
In my Base repo I have this code which works fine: abstract class BaseRepo
Quick code with the question included: abstract class ClassParent { public static $var1 =
My current non-compiling code is similar to this: public abstract class A { }
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
According to the PHP manual , a class like this: abstract class Example {}

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.