пятница, 19 сентября 2014 г.

When to use Singleton pattern?

Patterns I Hate #1: Singleton

Singleton Pattern

Use your singletons wisely

Difference between Singleton Pattern vs Static Class in Java

10 Singleton Pattern Interview questions in Java - Answered

Why Enum Singleton are better in Java

Singleton Pattern – Positive and Negative Aspects

This entry inaugurates a new series on patterns that I hate. Hate is a strong word, perhaps a better name would be “patterns that I’ve frequently seen lead to ruin”. But that seemed too long.
Anyhow, the singleton pattern is the hands-down #1 pattern that can lead me to froth at the mouth. When a programmer first stumbles on GoF, Singleton seems to be the pattern that they first latch on to because it’s so easy to understand (just one class). Novice programmers doing their first design will often break out subsystems and access them via singletons.
Why is Singleton evil?

When to use Object pool pattern?

Various pools - Thread pools, connection pools, EJB object pools - Flyweight is really about management of shared resources


You need to frequently create and destroy objects.

Objects are similar in size.

Allocating objects on the heap is slow or could lead to memory fragmentation.

Each object encapsulates a resource such as a database or network connection that is expensive to acquire and could be reused.

Basically, we'll use an object pool whenever there are several clients who needs the same stateless resource which is expensive to create.

When there are several clients who need the same resource at different times.

It is deprecated as a general technique, because - as you noticed - creation and destruction of short lived objects per se (i.e. memory allocation and GC) is extremely cheap in modern JVMs. So using a hand-written object pool for your run-of-the-mill objects is most likely slower, more complicated and more error-prone than plain new.*

It still has its uses though, for special objects whose creation is relatively costly, like DB / network connections, threads etc.

Examples of GoF Design Patterns in jdk

Pattern Proxy (Surrogate)

Proxy Design Pattern Tutorial (video)

Proxy Design Pattern Tutorial

The proxy design pattern allows you to provide an interface to other objects by creating a wrapper class as the proxy. The wrapper class, which is the proxy, can add additional functionality to the object of interest without changing the object's code. Below are some of the common examples in which the proxy pattern is used,

Adding security access to an existing object. The proxy will determine if the client can access the object of interest.
Simplifying the API of complex objects. The proxy can provide a simple API so that the client code does not have to deal with the complexity of the object of interest.
Providing interface for remote resources, such as web service or REST resources.
Coordinating expensive operations on remote resources by asking the remote resources to start the operation as soon as possible before accessing the resources.
Adding a thread-safe feature to an existing class without changing the existing class's code.
In short, the proxy is the object that is being called by the client to access the real object behind the scene.

When to use proxy pattern?

Remote Proxy – Represents an object locally which belongs to a different address space. Think of an ATM implementation, it will hold proxy objects for bank information that exists in the remote server.
( RMI, CORBA and Jini)

Virtual Proxy (Lazy Load Proxy) – In place of a complex or heavy object, use a skeleton representation. When an underlying image is huge in size, just represent it using a virtual proxy object and on demand load the real object. You feel that the real object is expensive in terms of instantiation and so without the real need we are not going to use the real object. Until the need arises we will use the virtual proxy.

Protection Proxy – Are you working on a MNC? If so, we might be well aware of the proxy server that provides us internet by restricting access to some sort of websites like public e-mail, social networking, data storage etc. The management feels that, it is better to block some content and provide only work related web pages. Proxy server does that job. This is a type of proxy design pattern.

A smart reference - a replacement for a bare pointer that performs additional actions when an object is accessed.
Provides additional actions whenever a target
object is referenced such as counting the number of references to the object

Adding a thread-safe feature to an existing class without changing the existing class's code.

Cache Proxy - Provides temporary storage of the results of expensive target
operations so that multiple clients can share the results

Firewall Proxy - Protects targets from bad clients (or vice versa)

java.rmi.*, the whole API actually.

Java Remote Method Invocation (RMI)

In java RMI an object on one machine (executing in one JVM) called a client can invoke methods on an object in another machine (another JVM) the second object is called a remote object. The proxy (also called a stub) resides on the client machine and the client invokes the proxy in as if it is invoking the object itself (remember that the proxy implements the same interface that RealSubject implements). The proxy itself will handle communication to the remote object, invoke the method on that remote object, and would return the result if any to the client. The proxy in this case is a Remote proxy.

Adapter vs Proxy Design Pattern

Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object.

Decorator vs Proxy Design Patter

Decorator design pattern adds behaviour at runtime to the real object. But, Proxy does not change the behaviour instead it controls the behaviour.