Strategy Pattern

Strategy pattern is one of the behavioral design patterns. This group of patterns regard communication between objects. They describe the way objects and classes interact and divide responsibilities among themselves. Intent The intent of the Strategy Pattern is to encapsulate related algorithms and make them callable through a common interface. This pattern decouples algorithm implementation into independent classes so they might … Continue reading Strategy Pattern

Value holder

Value holder The roles in this pattern are: ValueHolder- uses the IValueLoader through a Strategy Pattern to load the value when it is first accessed IValueLoader Factory Good news. The ValueHolder, and the IValueLoader are reusable, so you don't have to create these for every different object that you're going to load lazily. Value holder vs Lazy<T> type … Continue reading Value holder

Virtual proxy

Virtual proxy Proxy looks like the real object but does not have fully loaded state. When a request to one of its properties is made, it fetches it from the underlying object or data store. Client is the code that is using your object. It refers to an interface that both the real object and the proxy … Continue reading Virtual proxy

Lazy initialization and Lazy class

Lazy initialization Lazy Initialization is the simplest approach to lazy loading. The property check if its backing field was already initialized. If not, it initializes it and if it was- returns it. Typically the property just checks for null value. If null s a legal value - that can be for example obtained from the database … Continue reading Lazy initialization and Lazy class

Lazy loading

The intent of lazy loading If you're lazy about doing things you'll win when it turns out you don't need to do them at all. Martin Fowler The intent of this approach is to increase application performance by reducing the amount of work it has to do in situations when this work is not necessary. … Continue reading Lazy loading

Singleton versus static fields

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

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