background preloader

Pattern

Facebook Twitter

Observer pattern. Related patterns: Publish–subscribe pattern, mediator, singleton.

Observer pattern

Structure[edit] UML class diagram of Observer pattern Example[edit] The file MyApp.java contains a main() method that might be used in order to run the code. /* File Name : EventSource.java */package org.wikipedia.obs; import java.util.Observable; //Observable is hereimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class EventSource extends Observable implements Runnable { @Override public void run() { try { final InputStreamReader isr = new InputStreamReader(System.in); final BufferedReader br = new BufferedReader(isr); while (true) { String response = br.readLine(); setChanged(); notifyObservers(response); } } catch (IOException e) { e.printStackTrace(); } }} See also[edit] References[edit]

Singleton pattern. There is criticism of the use of the singleton pattern, as some consider it an anti-pattern, judging that it is overused, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.[1][2][3][4][5][6] In C++ it also serves to isolate from the unpredictability of the order of dynamic initialization, returning control to the programmer.

Singleton pattern

Common uses[edit] The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.Facade Objects are often Singletons because only one Facade object is required.State objects are often Singletons.Singletons are often preferred to global variables because: They do not pollute the global namespace (or, in languages with namespaces, their containing namespace) with unnecessary variables.[7]They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources. Facade pattern. The facade pattern (or façade pattern) is a software design pattern commonly used with object-oriented programming.

The name is by analogy to an architectural facade. A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can: Usage[edit] Structure[edit] Facade The facade class abstracts Packages 1, 2, and 3 from the rest of the application. Clients The objects are using the Facade Pattern to access resources from the Packages. Example[edit] This is an abstract example of how a client ("you") interacts with a facade (the "computer") to a complex system (internal computer parts, like CPU and HardDrive).

References[edit] Jump up ^ Freeman, Eric; Freeman, Elisabeth; Kathy, Sierra; Bert, Bates (2004). External links[edit] Design Patterns: Elements of Reusable Object-Oriented Software (0785342633610): Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns. Java Singleton « ChiaoCheng.com. After going through a couple job interviews, I quickly realized that there are many differing “opinions” (yes even among seasoned Java programmers) about the best way to initialize a singleton class.

Java Singleton « ChiaoCheng.com

For the record, there are really only two fundamental ways of initializing a singleton properly in java: eager or lazy. Then there is a neat trick to try to get the best of both worlds so that makes a total of three. Depending on the person you talk to, one of these methods may be preferred over the others but all three listed below are thread safe. Eager Initialization In eager initialization, the singleton is instantiated when the programs starts whether or not it gets used. Example 1. Public class Singleton { // Eager initialization happens here. static private Singleton instance = new Singleton(); // Prevent instantiation by public. private Singleton() { } public static Singleton getInstance() { return instance; } } Lazy Initialization Example 2.

Initialize-on-demand holder class idiom. Le pattern Specification pour la gestion de vos règles métier. Souvent lorsque l’on parle de gérer les règles métiers, on pense à moteur de règle, pas forcement … Le design pattern Specification est une solution de gestion de vos règles métiers.

Le pattern Specification pour la gestion de vos règles métier

Ce pattern a été formalisé par Eric Evans, père du DDD, et Martin Fowler que l’on ne présente plus. Ce pattern est simple mais très puissant. Il permet de : marquer et identifier les règles métiers,les centraliser,les réutiliser,communiquer entre développeurs et fonctionnels sur ces règles métiers. Cette solution a récemment été mise en place sur un gros site d’eCommerce en France. Allo Houston ? Prenons un exemple simple mais significatif, un site d’eCommerce de fruits et légumes. Supposons que l’on parte d’un besoin simple, exprimé par Ted, notre expert fonctionnel : « En tant qu’internaute, je veux ajouter des produits (donc des fruits ou/et légumes) à mon panier ». Bob, Le valeureux développeur, n’hésite pas un seul instant et réalise la besogne de la manière suivante : Collection.