Disposable item: yes or no?

I am thinking of creating some classes using a one-time use pattern defined by the following functions:

  • Instances are used to perform some tasks.
  • The instance will execute the task only once. Attempting to call the execute method twice will throw an exception.
  • Properties can be changed before calling the execute method. Calling them subsequently will also raise an exception.

A minimalist implementation might look like this:

 public class Worker { private bool _executed = false; private object _someProperty; public object SomeProperty { get { return _someProperty; } set { ThrowIfExecuted(); _someProperty = value; } } public void Execute() { ThrowIfExecuted(); _executed = true; // do work. . . } private void CheckNotExcecuted() { if(_executed) throw new InvalidOperationException(); } } 

Questions:

  • Is there a name for this?
  • Pattern or anti pattern?
+4
source share
2 answers

It looks like a form prohibiting template .

If it’s logical that your particular object behaves in this way, I don’t see a problem with it.

+3
source

Threads behave somewhat similarly (also using Dispose / Close blocks them for most operations). So this is not quite an amazing picture.

0
source

Source: https://habr.com/ru/post/1435367/


All Articles