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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:49:18+00:00 2026-05-29T09:49:18+00:00

Without State pattern public static void main(String[] args) { Context c = new Context();

  • 0

Without State pattern

public static void main(String[] args) {
    Context c = new Context();
    for (int i = 0; i < 10; i++)
        c.someMethod();
}

static class Context {
    public static final int STATE_A = 0;
    public static final int STATE_B = 1;

    private int state = STATE_A;

    public void someMethod() {
        switch (state) {
        case STATE_A:
            System.out.println("StateA.SomeMethod");
            state = STATE_B;
            break;
        case STATE_B:
            System.out.println("StateB.SomeMethod");
            state = STATE_A;
            break;
        default:
            break;
        }
    }
}

State Pattern

public static void main(String[] args) {
        Context context = new Context();
        context.setState(new State1());
        for (int i = 0; i < 10; i++)
            context.someMethod();
    }

    static class Context {
        State state;

        void setState(State state) {
            this.state = state;
        }

        void someMethod() {
            state.someMethod();
            if (state instanceof State1) {
                state = new State2();
            } else {
                state = new State1();
            }
        }
    }

    static interface State {
        void someMethod();
    }

    static class State1 implements State {
        @Override
        public void someMethod() {
            System.out.println("StateA.SomeMethod");
        }
    }

    static class State2 implements State {
        @Override
        public void someMethod() {
            System.out.println("StateB.SomeMethod");
        }
    }

In first case we have only one object but in another we create new object every time when we call someMethod() method.

  1. Is this a correct understanding of the pattern?
  2. How can I solve this problem to not create so many objects?
  3. What else do I need to know about this pattern ?
  • 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-29T09:49:19+00:00Added an answer on May 29, 2026 at 9:49 am

    Well you can do better:

    You must not check the instanceof to advance to new state.
    Each state should be able to go to the next as soon as it runs (Sample:Not even tried to compile).

    Example:

    class Context {  
        State state;                  
    
        public Context(){  
             state = STATE1;//Starting state
        }  
    
        void someMethod() { 
                state.someMethod(this);  //Which is it?State1 or state 2???
        }  
    
    }
    
    public interface States {  
        public static final State STATE1 = new State1();  
        public static final State STATE2 = new State2();   
        //more states here
    
    }  
    
    class State1 implements State {
         @Override         
          public void someMethod(Context ctx) {            
          System.out.println("StateA.SomeMethod");  
          ctx.setState(STATE2); //advance to next state        
      }
    }
    
    class State2 implements State {
        @Override         
        public void someMethod(Context ctx) {            
        System.out.println("StateB.SomeMethod");  
        ctx.setState(STATE1); //advance to next state (back to state 1)          
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code I have often situations like this: public void MyMethod(string data) {
I know that Singleton pattern is bad because it uses global state. But in
I have to do a final project for my computational linguistics class. We've been
I am trying to use the pimpl pattern and define the implementation class in
Google's Webmaster guidelines state Allow search bots to crawl your sites without session IDs
Given a fairly complex object with lots of state, is there a pattern for
I'm implementing a finite-state machine, with each class representing a state. Each state knows
Without the use of any external library, what is the simplest way to fetch
Without getting a degree in information retrieval, I'd like to know if there exists
Without calculating them , I mean?

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.