When should you use the singleton pattern instead of a static class? [closed]

Singletons can implement interfaces and inherit from other classes. Singletons can be lazy loaded. Only when it is actually needed. That’s very handy if the initialisation includes expensive resource loading or database connections. Singletons offer an actual object. Singletons can be extended into a factory. The object management behind the scenes is abstract so it’s … Read more

What is the difference between Strategy design pattern and State design pattern?

Honestly, the two patterns are pretty similar in practice, and the defining difference between them tends to vary depending on who you ask. Some popular choices are: States store a reference to the context object that contains them. Strategies do not. States are allowed to replace themselves (IE: to change the state of the context … Read more

Are there any viable alternatives to the GOF Singleton Pattern?

To understand the proper way to workaround Singletons, you need to understand what is wrong with Singletons (and global state in general): Singletons hide dependencies. Why is that important? Because If you hide the dependencies you tend to lose track of the amount of coupling. You might argue that void purchaseLaptop(String creditCardNumber, int price){ CreditCardProcessor.getInstance().debit(creditCardNumber, … Read more

What’s the difference between design patterns and architectural patterns? [closed]

It requires a detailed explanation but I will try to sketch the differences to best of my knowledge. Patterns are distilled commonality that you find in programs. It allows us to deconstruct a large complex structure and build using simple parts. It provides a general solution for a class of problems. A large complex software … Read more

What is an anti-pattern?

Anti-patterns are certain patterns in software development that are considered bad programming practices. As opposed to design patterns which are common approaches to common problems which have been formalized and are generally considered a good development practice, anti-patterns are the opposite and are undesirable. For example, in object-oriented programming, the idea is to separate the … Read more

Eliminating `switch` statements [closed]

Switch-statements are not an antipattern per se, but if you’re coding object oriented you should consider if the use of a switch is better solved with polymorphism instead of using a switch statement. With polymorphism, this: foreach (var animal in zoo) { switch (typeof(animal)) { case “dog”: echo animal.bark(); break; case “cat”: echo animal.meow(); break; … Read more