简体   繁体   中英

What do I name this class whose sole purpose is to report failure?

In our system, we have a number of classes whose construction must happen asynchronously. We wrap the construction process in another class that derives from an IConstructor class:

class IConstructor {
    public:
        virtual void Update() = 0;
        virtual Status GetStatus() = 0;
        virtual int GetLastError() = 0;
};

There's an issue with the design of the current system - the functions that create the IConstructor -derived classes are often doing additional work which can also fail. At that point, instead of getting a constructor which can be queried for an error, a NULL pointer is returned.

Restructuring the code to avoid this is possible, but time-consuming. In the meantime, I decided to create a constructor class which we create and return in case of error, instead of a NULL pointer:

class FailedConstructor : public IConstructor
    public:
        virtual void Update() {}
        virtual Status GetStatus() { return STATUS_ERROR; }
        virtual int GetLastError() { return m_errorCode; }
    private: int m_errorCode;
};

All of the above this the setup for a mundane question: what do I name the FailedConstructor class? In our current system, FailedConstructor would indicate "a class which constructs an instance of Failed ", not "a class which represents a failed attempt to construct another class".

I feel like it should be named for one of the design patterns, like Proxy or Adapter , but I'm not sure which.

EDIT: I should make it clear that I'm looking for an answer that adheres to, ideally, one of the GoF design patterns, or some other well-established naming convention for things of this nature.

To answer your literal question, I'd probably go with ConstructorFailure , as it describes the event of failing.

However, I'd probably go one step further and make it an Exception , in which case ConstructorException doesn't sound too shabby. Any reason you want to return this instead of throwing it?

I'd name it NullConstructor in line with the null object pattern, which is the pattern you're using. See http://en.wikipedia.org/wiki/Null_Object_pattern

Throw an exception. That is If I understand your description correctly and the creation of the IConstructor object is not done asynchronously.

Though if you don't have exceptions available to you I would probably call it ConstructorCreationError . Yes it does convey a failure mode but, more accurately, it is communicating the specific error that occurred. Also, having constructor as the last word, to me, seems to give the wrong meaning, but you could put "constructor" at the end as well.

You could also replace the verb "Creation" with something like SpawnConstructorError , ConstructorGenerationError and or if you're a fan of Dr. Chevalier maybe ErroneousConstructor .

我选择DummyConstructor是因为它的唯一目的是模拟一个有效的Constructor实例,但是它没有实现任何实际功能。

FailureResponseConstructor?

You're not creating a failure, you're creating the response to the failure. Thus, I would think any synonym to 'response' or 'respond' would work.

If you willing to spend effort checking returned pointer against the "FailureConstructor", I don't see reason why couldn't you check it against NULL ?

Unless your system is designed to mask component failure from each other, it just doesn't make sense to assume every associated parts are working well.

I'm going to go with something based on Niall C.'s comment -- FailedConstructorProxy . The Proxy pattern seems to fit best with what this class is; though rather than it relaying method calls to an object, it's standing in and generating what we wanted the actual constructor to return.

(If someone has a more correct answer, post it and I'll mark that one as accepted. I'm still not 100% convinced this is the right name!)

Why not create a Failed class to represent a class that failed construction and go with FailedConstructor ? This way the naming is consistent.

我建议调用此类的FailedObjectConstructionHandler来描述该类的功能。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM