design-patterns
should a db connection be a singleton?
A DB connection should not normally be a Singleton. Two reasons: many DB drivers are not thread safe. Using a singleton means that if you have many threads, they will all share the same connection. The singleton pattern does not give you thread saftey. It merely allows many threads to easily share a “global” instance. … Read more
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
Fluent Interfaces – Method Chaining
The core idea behind building a fluent interface is one of readability – someone reading the code should be able to understand what is being achieved without having to dig into the implementation to clarify details. In modern OO languages such as C#, VB.NET and Java, method chaining is one way that this is achieved, … Read more
When to use promise.all()?
I’m not sure anyone has really given the most general purpose explanation for when to use Promise.all() (and when not to use it): What is(are) the correct scenario(s) to use promise.all() Promise.all() is useful anytime you have more than one promise and your code wants to know when all the operations that those promises represent … 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
clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)
There is a very simple pattern, which has retro-actively been dubbed PassKey, and which is very easy in C++11: template <typename T> class Key { friend T; Key() {} Key(Key const&) {} }; And with that: class Foo; class Bar { public: void special(int a, Key<Foo>); }; And the call site, in any Foo method, … Read more
Parcelable and inheritance in Android
Here is my best solution, I would be happy to hear from somebody that had a thought about it. public abstract class A implements Parcelable { private int a; protected A(int a) { this.a = a; } public void writeToParcel(Parcel out, int flags) { out.writeInt(a); } protected A(Parcel in) { a = in.readInt(); } } … 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: http://msdn.microsoft.com/en-us/library/ms182128(v=VS.100).aspx 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