Closed rain1024 closed 10 years ago
Tags: <creational>
Tags: <creational>
Tags: <creational>
The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotvpe result. In other words, the cell clones itself.
Tags: <creational>
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
Abstract Factory
, Builder
, and Prototype
can use Singleton
in their implementation.Facade
objects are often Singletons
because only one Facade
object is required.State
objects are often Singletons
.Singleton
over global variables is that you are absolutely sure of the number of instances when you use Singleton
, and, you can change your mind and manage any number of instances.Singleton
design pattern is one of the most inappropriately used patterns. Singletons
are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons
in a misguided attempt to replace global variables. A Singleton
is, for intents and purposes, a global variable. The Singleton
does not do away with the global; it merely renames it.Singleton
unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.Singleton
. The next thing I know Singletons
appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton
." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.Tags: <structural>
, <wrapper>
Tags: <structural>
Tags: <structural>
Tags: <structural>
Tags: <structural>
<wrapper>
The Facade defines a unified, higher level interface to a subsystem that makes it easier to use. Consumers encounter a Facade when ordering from a catalog. The consumer calls one number and speaks with a customer service representative. The customer service representative acts as a Facade, providing an interface to the order fulfillment department, the billing department, and the shipping department.
Facade
defines a new interface, whereas Adapter
uses an old interface.
Adapter
makes two existing interfaces work together as opposed to defining an entirely new one.Flyweight
shows how to make lots of little objects, Facade
shows how to make a single object represent an entire subsystem.Mediator
is similar to Facade
in that it abstracts functionality of existing classes.
Mediator
abstracts/centralizes arbitrary communications between colleague objects. It routinely "adds value", and it is known/referenced by the colleague objects. In contrast, Facade
defines a simpler interface to a subsystem, it doesn't add new functionality, and it is not known by the subsystem classes.Abstract Factory
can be used as an alternative to Facade
to hide platform-specific classes.Facade
objects are often Singletons
because only one Facade object is required.Adapter
and Facade
are both wrappers; but they are different kinds of wrappers.
Facade
is to produce a simpler interface, and the intent of Adapter
is to design to an existing interface.Facade
routinely wraps multiple objects and Adapter
wraps a single object; Facade
could front-end a single complex object and Adapter
could wrap several legacy objects.Tags: <structural>
The Flyweight uses sharing to support large numbers of objects efficiently. Modern web browsers use this technique to prevent loading same images twice. When browser loads a web page, it traverse through all images on that page. Browser loads all new images from Internet and places them the internal cache. For already loaded images, a flyweight object is created, which has some unique data like position within the page, but everything else is referenced to the cached one.
Flyweight
shows how to make lots of little objects, Facade
shows how to make a single object represent an entire subsystem.Flyweight
is often combined with Composite
to implement shared leaf nodes.Flyweight
.Flyweight
explains when and how State
objects can be shared.Tags: <structural>
Tags: <behavioral>
, <decouple>
The Chain of Responsibility pattern avoids coupling the sender of a request to the receiver by giving more than one object a chance to handle the request. ATM use the Chain of Responsibility in money giving mechanism.
Chain of Responsibility
, Command
, Mediator
, and Observer
, address how you can decouple senders and receivers, but with different trade-offs. Chain of Responsibility
passes a sender request along a chain of potential receivers.Chain of Responsibility
can use Command
to represent requests as objects.Chain of Responsibility
is often applied in conjunction with Composite
. There, a component's parent can act as its successor.Tags: <behavioral>
carManager.execute( "arrangeViewing", "Ferrari", "14523" );
carManager.execute( "requestInfo", "Ford Mondeo", "54323" );
carManager.execute( "requestInfo", "Ford Escort", "34232" );
carManager.execute( "buyVehicle", "Ford Escort", "34232" );
Tags: <behavioral>
Tags: <behavioral>
Tags: <behavioral>
, <decouple>
Tags: <behavioral>
The Memento captures and externalizes an object's internal state so that the object can later be restored to that state. This pattern is common among do-it-yourself mechanics repairing drum brakes on their cars. The drums are removed from both sides, exposing both the right and left brakes. Only one side is disassembled and the other serves as a Memento of how the brake parts fit together. Only after the job has been completed on one side is the other side disassembled. When the second side is disassembled, the first side acts as the Memento.
Tags: <behavioral>
, <state>
, <decouple>
Subject represents the core (or independent or common or engine) abstraction. Observer represents the variable (or dependent or optional or user interface) abstraction. The Subject prompts the Observer objects to do their thing. Each Observer can call back to the Subject as needed.
Tags: <behavioral>
Tags: <behavioral>
strategy0 = StrategyExample()
strategy1 = StrategyExample(executeReplacement1)
strategy2 = StrategyExample(executeReplacement2)
Tags: <behavioral>
Tags: <behavioral>
Design Paterns
Abstract Factory
Tags:
<creational>
Code
References
http://www.dofactory.com/javascript-abstract-factory-pattern.aspx http://sourcemaking.com/design_patterns/abstract_factory/cpp/1