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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:58:35+00:00 2026-05-12T05:58:35+00:00

What are the scenarios when one would use a Monostate pattern instead of singleton

  • 0

What are the scenarios when one would use a Monostate pattern instead of singleton inorder to maintain a global object?

Edit:
I know what Singleton and Monostate patterns are. Have also implemented Singleton in quite a few scenarios. Just want to know the scenarios (case examples) where MonoState pattern needs to be implemented.

For eg. I need to maintain list of columns per screen in my windows forms app. I could use a Singleton Dictionary in this case. However, I am storing a List in the static global var and I wanted to provide indexers (since I need to dynamically add new entry to the list if key is not present) where I could specify ScreenDetails.ScreenName as a key & get the ScreenDetails.ColumnsTable. Since indexers can’t operate on a static class I changed the pattern to Monostate.

So I would like to know which other scenarios may compel a user to use Monostate instead of Singletons.

  • 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-12T05:58:35+00:00Added an answer on May 12, 2026 at 5:58 am

    At its base Monostate is just syntactic sugar around Singleton. Where Monostate gets interesting is when you start subclassing, because the subclasses can decorate the shared state with different behavior.

    A simple — if somewhat contrived and not very efficient 🙂 — example:

    public class GlobalTable implements Iterable<Key> {
    
      /** Shared state -- private */    
      private static final Map<Key, Value> MAP = new LinkedHashMap<Key, Value>();
    
      /** Public final accessor */    
      public final Value get(Key key) {
        return MAP.get(key);
      }
    
      /** Public final accessor */    
      public final boolean put(Key key, Value value) {
        return MAP.put(key);
      }
    
      /** Protected final accessor -- subclasses can use this to access
          the internal shared state */    
      protected final Set<Key> keySet() {
        return MAP.keySet();
      }
    
      /** Virtual -- subclasses can override for different behavior */    
      public Iterator<Key> iterator() {
        return Collections.unmodifiableSet(MAP.keySet()).iterator();
      }
    }
    

    Now what if we want indexed access?

    public class IndexedGlobalTable extends GlobalTable {
    
      public List<Key> getKeysAsList() {
        return Collections.unmodifiableList(new ArrayList<Key>(keySet()));
      }
    
      public Key getKeyAt(int index) {
        return getKeysAsList().get(index);
      }
    
      public Value getValueAt(int index) {
        return get(getKeyAt(index));
      }
    }
    

    How about sorted keys?

    public class SortedGlobalTable extends GlobalTable {
    
      @Override
      public Iterator <Key> iterator() {
        return Collections
          .unmodifiableSortedSet(new TreeSet<Key>(keySet())).iterator();
      }
    
    }
    

    Any time you need one or the other view of the data, you just instantiate the appropriate subclass.

    Of course, whether global data is really a good idea in the first place is another question, but at least Monostate gives you more flexibility in how you use it.

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

Sidebar

Ask A Question

Stats

  • Questions 270k
  • Answers 270k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For an executable program (as opposed to a shell script),… May 13, 2026 at 1:31 pm
  • Editorial Team
    Editorial Team added an answer The fix is to upload/provision a new file, and then… May 13, 2026 at 1:31 pm
  • Editorial Team
    Editorial Team added an answer Have you tried passing HTML as the first argument? Your… May 13, 2026 at 1:31 pm

Related Questions

There are quite a lot of questions on SO regarding View Engines in ASP.Net
I have a somewhat ridiculous question regarding DDD, Repository Patterns and ORM. In this
How would one create a REST web service to write a row into a
I've noticed a lot of talk about asp.net MVC lately, but I haven't come

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.