language agnostic - changed object state after behavior that used the state -


I want to give a second chance because I think I have chosen a bad example.

The question is how should I deal with those situations where one object can still change when I used it to do something and the new state is relevant to what is being done.

Examples in pseudo code:

  class book method 'addChapter': A chapter class person adds the 'readbook' method: read an object in the class book  

Now when I read a book, I can add a chapter between chapters 3 and 4, at least in PHP, where the object will be passed in context, the book object can still change while The person is already reading chapter 6. How can I deal with these types of situations?

Any of the answers above are good for a good solution based on the demand of your business.

You asked "how will I deal with the circumstances ..." It depends:

Do you already send a book legally to a user, after that a chapter To add? If not, you should throw an exception (I do not know that PHP supports exceptions, but anyway - you should treat it as an inaccurate condition).

Another solution would be to make sure that you expose and object it is already complete and it is not expected to change - this can be a valid solution, especially if this type of ' Your decision to enable streaming is oriented but you do not have the actual proof that this section is one

Do you want to replace existing clients or just new customers?

Now the addition of a new chapter is legal.

If pre-you should implement any type of notification logic (one of the aromatic forms is a publisher / customer pattern, but others are).

If the letter- you should make your book object irreversible, therefore the operating operation will not appear in existing customers, but they completely create a new book which will be provided to new customers (individuals).

I can go further, but I suggest that the next time you are trying to solve the right problem, the more times you can see, the more detail - the same problem Solutions in different domains may vary.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -