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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:23:53+00:00 2026-05-15T16:23:53+00:00

Firstly, can anyone explain how a state object can be shared when the state

  • 0

Firstly, can anyone explain how a state object can be shared when the state object has no instance variables ?

This text is taken from GOF, page 308, item 3 (consequences section):

The state object can be shared.
If state objects have no instance variabkes – that is, the state they
represent is encoded entirely in their
type – then contexts can share a
state object. When states are shared in
this way, they are essentially
flyweight.

Can anyone explain this text ?

Secondly, what are the approaches to the state transition decision? I mean the decision of which next state to propagate?

Please help.
Thanks.

  • 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-15T16:23:53+00:00Added an answer on May 15, 2026 at 4:23 pm

    In the state pattern you have an represent the state of an object by using state-objects. These state-objects represent a certain state, but they do not have any mutable state of their own. This means they never change. Therefore, any number of objects can use the same state-object at the same time (even from different threads). If the state-object had mutable state, other objects would have to worry about their state-object being changed from elsewhere.

    The using of one object instance by many others can be seen as an instance of the flyweight-pattern.

    As for the second part of your question, here is an example:

    class SomeStateMachine;
    
    class AbstractState {
        // abstract baseclass for all state-classes
        void input(const std::string & data, SomeStateMachine & caller) = 0;
    }
    
    class FinalState : public AbstractState {
        FinalState * getInstance(); // always returns same instance
    }
    
    class InitialState : public AbstractState {
    public:
        InitialState * getInstance(); // always returns same instance
        void input(const std::string & data, SomeStateMachine & caller) {
            std::cout << data << std::endl;
            caller.m_State = FinalState::getInstance();
        }
    }
    
    class SomeStateMachine {
    public:
        SomeStateMachine() : m_State(InitialState::getInstance())
        void input(const std::string & data) {
            m_State->input(data, *this);
        }
    private:
        friend class InitialState;
        AbstractState * m_State;
    };
    

    So you basically pass a reference to the calling object to every method of your state-object. This way, the state-object is able to change the state of the caller when needed. This example might not be very beautiful, but I hope you get the idea.

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

Sidebar

Related Questions

How can i tell programm to read firstly from the 1st line, to check
(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone
Firstly this is more of a design / interface type question, so has no
Firstly, I wrote my customized setter for an NSString* like this: - (void)setDateString:(NSString *)newDateString
Firstly sorry if this is a common question but I couldn't find anything on
Firstly, I know this [type of] question is frequently asked, so let me preface
Firstly, this is going to sound like homework, but it ain't. Just a problem
Firstly I asked sorry if this is a stupid question. but I have a
Firstly I want to say I know little english. I have seeking about this
I've received a text file in which the text is like this, ãYAHOO.COM. When

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.