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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:23:23+00:00 2026-05-24T06:23:23+00:00

I am learning design pattern by reading Head First Design Patterns , and I

  • 0

I am learning design pattern by reading Head First Design Patterns, and I have just finished the chapter on the State Pattern. However, there is one thing I don’t get:

In the book, the class which has a state is called the Context, while the actual states implement the State interface. When a request is given the book use the method here to change the state:

  1. The Context receives the request;
  2. The Context call the handle() method on the State it has, hand the request to the State;
  3. The State process the request, and set the State in the Context if it should change. To do this, the States have the Context as a field.

However, this seems to me give a somewhat “recursive” coupling between the two class, and is unintuitive. I would prefer the following design if I haven’t read their solution:

  1. The Context receives the request;
  2. The Context call the handle() method on the State it has, hand the request to the State;
  3. The State process the request, and return a State; whether it is another State depends on how the request is processed.
  4. The Context set its own State to the State returned.

The Pros and Cons I could think of:

  1. If we put all that can vary into the State classes, the Context and State and become more decoupled, since they won’t need to peek into each others’ fields;
  2. The State classes might become bigger;
  3. The State might not be easily reused among different Contexts. However, we could implement States as abstract classes that implement the State interface, and have concrete States that are used by Contexts subclassing the abstract States.

Is there any specific reason that the first one (which is used by Head First Design Patterns) should be favored, or the second choice is also valid and used in practice, or maybe it has some serious flaws that I did not see?

Thanks for all the inputs and for your time reading and replying!

  • 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-24T06:23:23+00:00Added an answer on May 24, 2026 at 6:23 am

    The issue with the approach you mention is that any other objects which have a reference to the State object fail to get updates in the scenario you describe. As you describe it, the State can return another State which has the updated State, and the Context can replace its reference to a State with the updated State, but any other objects that have reference to the State will now have a dangling reference that’s not referred to by Context.

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

Sidebar

Related Questions

I have recently been learning more about design patterns and thought I'd take a
I am learning STL and design patterns . i wanted to know is there
Duplicate: Learning implementing design patterns for newbies I have been a developer for years
Hi I am learning Design patterns these days. I want to read design pattern
I'm learning about Design Patterns and found the Builder design pattern. What are the
I'm learning about design patterns and one thing I noticed in pretty much all
I am learning about the different design patterns and I have a strong feeling
I am learning the State Design Pattern. I am using C# ASP.Net (not sure
I have just finished learning application development on android platform, as In matter of
I am learning java. I am trying to use composite design pattern. I am

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.