A singleton is a class which only allows a single instance of itself to be created. But why do I need a whole design pattern for this? Why not simply use static fields? Static field object gets created before the application starts If you assign an object to a static field it gets created before the … Continue reading Singleton versus static fields
Category: Creational Design Patterns
Singleton
Singleton is one of the simplest design patterns, yet it is easy to get it wrong. It is also called anti-pattern. Why? Let's start from the beginning. One of a kind Let's create a class Saturn... Hmm but how many instances can such a class have? How many Saturns do you expect in cosmos, dude? … Continue reading Singleton
When NOT to use abstract factory
There are several issues that pile up when we try to use abstract factory pattern that limit its applicability. Example Let's imagine a set of e-books and traditional paper books. All book classes derive from abstract Book class. All e-books will then inherit from ElectronicBook class that specifies a format of the e-book (mobi, epub, … Continue reading When NOT to use abstract factory
Abstract Factory
Abstract factory The abstract factory pattern is a design pattern that allows for the creation of groups of related objects without the requirement of specifying the exact concrete classes that will be used. One of a number of factory classes generates the object sets. Roles Client: - uses interfaces declared by AbstractFactory and AbstractProduct AbstractFactory: … Continue reading Abstract Factory
Factory Method
Factory pattern Another design pattern that separates creation of an object from its representation is Factory. The intent of the Factory is to separate creation from the decision of which object to create. Use it instead of if/else or switch blocks when deciding which class to instantiate. “Define an interface for creating an object, but … Continue reading Factory Method
Builder
Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. Roles The roles (classes and objects) that take part in this pattern are: Director - is called directly by the client - holds instance of Builder - uses the Builder to construct an object … Continue reading Builder