What is a no-op class?
I just learned ab开发者_如何学Pythonout the Template Method pattern in this answer to a question about ensuring that methods in base classes are always called by child classes.
Part of the answer talks about base classes vs no-op classes:
(The decision about whether to make it a no-op or abstract is usually fairly obvious - does the base class make sense on its own, as a concrete class?)
What is a no-op class?
It isn't the class that is no-op, but the implementation of the method.
The answer debates the decision of making the method in the base class abstract (thereby forcing derived classes to implement it, even if they want to have it do absolutely nothing) versus implementing it as a "no-op" (i.e. with an empty method body).
If doing nothing is a sensible default, then the base class might implement this method as a no-op so that derived classes are bothered to override only if they actually want something different to happen.
If doing nothing is not sensible, then it makes sense for the method to be abstract.
no-op
is short for no-operation
, that is, a method that does nothing:
public void MyNoOp()
{
}
Or from the linked question:
public override void Update()
{
//no-op, does nothing
}
精彩评论