Is This Use of the “instanceof” Operator Considered Bad Design?

The Visitor pattern is typically used in such cases. Although the code is a bit more complicated, but after adding a new RecordType subclass you have to implement the logic everywhere, as it won’t compile otherwise. With instanceof all over the place it is very easy to miss one or two places. Example: public abstract … Read more

Raise Events in .NET on the main UI thread

Your library could check the Target of each delegate in the event’s invocation list, and marshal the call to the target thread if that target is ISynchronizeInvoke: private void RaiseEventOnUIThread(Delegate theEvent, object[] args) { foreach (Delegate d in theEvent.GetInvocationList()) { ISynchronizeInvoke syncer = d.Target as ISynchronizeInvoke; if (syncer == null) { d.DynamicInvoke(args); } else { … Read more

Are empty interfaces code smell? [closed]

Although it seems there exists a design pattern (a lot have mentioned “marker interface” now) for that use case, i believe that the usage of such a practice is an indication of a code smell (most of the time at least). As @V4Vendetta posted, there is a static analysis rule that targets this: If … Read more

Factory pattern in C#: How to ensure an object instance can only be created by a factory class?

You can make the constructor private, and the factory a nested type: public class BusinessObject { private BusinessObject(string property) { } public class Factory { public static BusinessObject CreateBusinessObject(string property) { return new BusinessObject(property); } } } This works because nested types have access to the private members of their enclosing types. I know it’s … Read more