| Start |
|---|
| Singleton Pattern The Singleton pattern is designed to restrict creation of a class to exactly one single instance. This pattern is used in a scenario where it makes sense to have only one of something, such as:
One of the most common ways to implement the Singleton pattern is via a static factory method such as a The first call to the method or property creates and returns the Singleton instance. Thereafter, the method always returns the same instance. This way, there is only ever one instance of the singleton object. Preventing creation of instances via Here is a typical code example for implementing a Singleton pattern in C#: To illustrate this pattern further, the code below checks whether an identical instance of the Singleton is returned when the Instance property is called more than once. Note: this implementation is not thread safe. To see more examples, including how to make this thread-safe, visit: Singleton Implementation (opens new window) Singletons are conceptually similar to a global value, and cause similar design flaws and concerns. Because of this, the Singleton pattern is widely regarded as an anti-pattern. Visit "What is so bad about Singletons?" (opens new window) for more information on the problems that arise with their use. In C#, you have the ability to make a class For key differences between the two, visit C# Singleton Pattern Versus Static Class (opens new window). |
| Factory Method pattern Factory Method is one of creational design patterns. It is used to deal with the problem of creating objects without specifying exact result type. This document will teach you how to use Factory Method DP properly. Let me explain the idea of it to you on a simple example. Imagine you're working in a factory that produces three types of devices - Ammeter, Voltmeter and resistance meter. You are writing a program for a central computer that will create selected device, but you don't know final decision of your boss on what to produce. Let's create an interface Now, we can create classes that represent our devices. Those classes must implement Now we have to define factory method. Let's create Great! Let's test our code: This is the example output you might see after running this code: AmMeter turns on... 36 6 33 43 24 AmMeter flashes lights saying good bye! VoltMeter turns on... 102 -61 85 138 36 VoltMeter flashes lights saying good bye! OhmMeter turns on... 723828 368536 685412 800266 578595 OhmMeter flashes lights saying good bye! |
| Abstract Factory Pattern Provide an interface for creating families of related or dependent objects without specifying their concrete classes. In this example demonstrates the creation of different animal worlds for a computer game using different factories. Although the animals created by the Continent factories are different, the interactions among the animals remain the same. Output: Lion eats Wildebeest Wolf eats Bison |
| Builder Pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations and and provides a high level of control over the assembly of the objects. In this example demonstrates the Builder pattern in which different vehicles are assembled in a step-by-step fashion. The Shop uses VehicleBuilders to construct a variety of Vehicles in a series of sequential steps. Output Vehicle Type: Scooter Frame : Scooter Frame Vehicle Type: Car Vehicle Type: MotorCycle |
| Prototype Pattern Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype. In this example demonstrates the Prototype pattern in which new Color objects are created by copying pre-existing, user-defined Colors of the same type. Output: Cloning color RGB: 255, 0, 0 Cloning color RGB: 128,211,128 Cloning color RGB: 211, 34, 20 RemarksThe creational patterns aim to separate a system from how its objects are created, composed, and represented. They increase the system's flexibility in terms of the what, who, how, and when of object creation. Creational patterns encapsulate the knowledge about which classes a system uses, but they hide the details of how the instances of these classes are created and put together. Programmers have come to realize that composing systems with inheritance makes those systems too rigid. The creational patterns are designed to break this close coupling. |
Read Article
https://develop.horje.com/learn/1434/reference