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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:33:28+00:00 2026-05-13T09:33:28+00:00

I’m trying to model a certain process and I’m thinking that the State Pattern

  • 0

I’m trying to model a certain process and I’m thinking that the State Pattern might be a good match. I’d like to get your feedback though about whether State will suit my needs and how it should be combined with my persistence mechanism.

I have a CMS that has numerous objects, for example, Pages. These objects (we’ll use the example of Pages, but it’s true of most objects) can be in one of a number of states, 3 examples are:
Unpublished
Published
Reworking

When Unpublished, they are editable. Once Published, they are not editable, but can be moved into the Reworking state. In the Reworking state they are editable again and can be Republished.

Obviously the decision for whether these Pages are editable should be in the models themselves and not the UI. So, the State pattern popped into mind. However, how can I prevent assigning values to the object’s properties? It seems like a bad idea to have a check on each property setter:

if (!CurrentState.ReadOnly)

Any ideas how to work this? Is there a better pattern for this?

  • 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-13T09:33:28+00:00Added an answer on May 13, 2026 at 9:33 am

    Update:

    I actually thought of a method this morning that would work with your specific case and be a lot easier to maintain. I’ll leave the original two points here, but I’m going to recommend the final option instead, so skip to the “better method” section.

    1. Create a ThrowIfReadOnly method, which does what it says on the tin. This is slightly less repetitive and avoids the nesting.

    2. Use an interface. Have an IPage that implements the functionality you want, have every public method return an IPage, then have two implementations, an EditablePage and a ReadOnlyPage. The ReadOnlyPage just throws an exception whenever someone tries to modify it. Also put an IsReadOnly property (or State property) on the IPage interface so consumers can actually check the status without having to catch an exception.

    Option (2) is more or less how IList and ReadOnlyCollection<T> work together. It saves you the trouble of having to do a check at the beginning of every method (thus eliminating the risk of forgetting to validate), but requires you to maintain two classes.

    — Better Method —

    A proper technical spec would help a lot to clarify this problem. What we really have here is:

    • A series of arbitrary “write” actions;
    • Each action has the same outcome, dependent on the state:
    • Either the action is taken (unpublished/reworking), or fails/no-ops (read-only).

    What really needs to be abstracted is not so much the action itself, but the execution of said action. Therefore, a little bit of functional goodness will help us here:

    public enum PublishingState
    {
        Unpublished,
        Published,
        Reworking
    }
    
    public delegate void Action();
    
    public class PublishingStateMachine
    {
        public PublishingState State { get; set; }
    
        public PublishingStateMachine(PublishingState initialState)
        {
            State = initialState;
        }
    
        public void Write(Action action)
        {
            switch (State)
            {
                case PublishingState.Unpublished:
                case PublishingState.Reworking:
                    action();
                    break;
                default:
                    throw new InvalidOperationException("The operation is invalid " +
                        "because the object is in a read-only state.");
            }
        }
    }
    

    Now it becomes almost trivial to write the classes themselves:

    public class Page
    {
        private PublishingStateMachine sm = new
            PublishingStateMachine(PublishingState.Unpublished);
    
        private string title;
        private string category;
    
        // Snip other methods/properties
        // ...
    
        public string Title
        {
            get { return title; }
            set { sm.Write(() => title = value; }
        }
    
        public string Category
        {
            get { return category; }
            set { sm.Write(() => category = value; }
        }
    
        public PublishingState State
        {
            get { return sm.State; }
            set { sm.State = value; }
        }
    }
    

    Not only does this more-or-less implement the State pattern, but you don’t need to maintain separate classes or even separate code paths for the different states. If you want to, for example, turn the InvalidOperationException into a no-op, just remove the throw statement from the Write method. Or, if you want to add an additional state, like Reviewing or something like that, you just need to add one case line.

    This won’t handle state transitions for you or any really complex actions that do different things depending on the state (other than just “succeed” or “fail”), but it doesn’t sound like you need that. So this gives you a drop-in state implementation that requires almost no extra code to use.

    Of course, there’s still the option of dependency injection/AOP, but there’s obviously a lot of overhead associated with that approach, and I probably wouldn’t use it for something so simple.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to set the style for a single… May 13, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer i = C+kV Lets call N the normal to the… May 13, 2026 at 7:26 pm
  • Editorial Team
    Editorial Team added an answer You can get the frame of each view and use… May 13, 2026 at 7:26 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

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.