Showing posts with label design patterns. Show all posts
Showing posts with label design patterns. Show all posts

Thursday, December 31, 2020

Multiton

 

Multiton
Design Patterns
Creational Design Patterns



In computer programming, the Multiton Pattern is a design pattern which generalizes the singleton pattern in two different ways:
1) On copy of the corresponding Singleton for each data type,
2) On copy of the corresponding Singleton for each value of a parameter.
Where the Singleton Pattern permits only one instance of a class to be instantiated, the Multiton Pattern allows multiple instances of a class to be instantiated.



Saturday, October 10, 2015

List of Creational Design Patterns

List of Creational Design Patterns
Question & Answer

In the following, which five design patterns are the creational patterns?


 1) Abstract Factory
 2) Adapter
 3) Bridge
 4) Builder
 5) Chain of Responsibility
 6) Command
 7) Composite
 8) Decorator
 9) Facade
10) Factory Method
11) Flyweight
12) Interpreter
13) Iterator
14) Mediator
15) Memento
16) Observer
17) Prototype
18) Proxy
19) Singleton
20) State
21) Strategy
22) Template Method
23) Visitor
 

Creational Design Patterns
  1. Abstract Factory
  2. Builder
  3. Factory Method
  4. Prototype
  5. Singleton

Sunday, September 20, 2015

INTERVIEW :: DESIGN/DEVELOPMENT :: DESIGN PATTERNS :: BASIC CONCEPTS



INTERVIEW
  DESIGN/DEVELOPMENT
    DESIGN PATTERNS
      BASIC CONCEPTS
 

In the following, which five design patterns are the creational patterns?

 1) Abstract Factory
 2) Adapter
 3) Bridge
 4) Builder
 5) Chain of Responsibility
 6) Command
 7) Composite
 8) Decorator
 9) Facade
10) Factory Method
11) Flyweight
12) Interpreter
13) Iterator
14) Mediator
15) Memento
16) Observer
17) Prototype
18) Proxy
19) Singleton
20) State
21) Strategy
22) Template Method
23) Visitor
 










Tuesday, March 31, 2009

Design Patterns :: Singleton

Design Patterns :: Singleton

There are many common situations when singleton pattern is used:
 - Accesing resources in shared mode
 - Configuration Classes
 - Logger Classes
 - Serialization
 - Network Port Interface

Singleton Design Pattern
Singleton Design Pattern

Check list
1.Define a private static attribute in the "single instance" class.
2.Define a public static accessor function in the class.
3.Do "lazy initialization" (creation on first use) in the accessor function.
4.Define all constructors to be protected or private.
5.Clients may only use the accessor function to manipulate the Singleton.



  1. using System;

  2. namespace DesignPatternsSingleton
  3. {
  4.   /// <summary>
  5.   /// MainApp startup class for Design Patterns :: Singleton
  6.   /// </summary>
  7.   class MainApp
  8.   {
  9.   /// <summary>
  10.   /// The Singleton class.
  11.   /// </summary>
  12.   public class Singleton
  13.   {
  14.     private static Singleton _instance;

  15.     // Constructor is 'protected'
  16.     protected Singleton()
  17.     {
  18.     }

  19.     public static Singleton Instance()
  20.     {
  21.       // Uses lazy initialization.
  22.       // Note: this is not thread safe.
  23.       if (_instance == null)
  24.       {
  25.         _instance = new Singleton();
  26.       }

  27.       return _instance;
  28.     }
  29.   }
  30. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;

  4. namespace DesignPatternsSingleton
  5. {
  6.   /// <summary>
  7.   /// MainApp startup class for Real-World
  8.   /// Singleton Design Pattern.
  9.   /// </summary>
  10.   class MainApp
  11.   {
  12.     /// <summary>
  13.     /// Entry point into console application.
  14.     /// </summary>
  15.     static void Main()
  16.     {
  17.       LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
  18.       LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
  19.       LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
  20.       LoadBalancer b4 = LoadBalancer.GetLoadBalancer();

  21.       // Same instance?
  22.       if (b1 == b2 && b2 == b3 && b3 == b4)
  23.       {
  24.         Console.WriteLine("Same instance\n");
  25.       }

  26.       // Load balance 15 server requests
  27.       LoadBalancer balancer = LoadBalancer.GetLoadBalancer();
  28.       for (int i = 0; i < 15; i++)
  29.       {
  30.         string server = balancer.Server;
  31.         Console.WriteLine("Dispatch Request to: " + server);
  32.       }

  33.       // Wait for user
  34.       Console.ReadKey();
  35.     }
  36.   }

  37.   /// <summary>
  38.   /// The 'Singleton' class
  39.   /// </summary>
  40.   class LoadBalancer
  41.   {
  42.     private static LoadBalancer _instance;
  43.     private List<string> _servers = new List<string>();
  44.     private Random _random = new Random();

  45.     // Lock synchronization object
  46.     private static object syncLock = new object();

  47.     // Constructor (protected)
  48.     protected LoadBalancer()
  49.     {
  50.       // List of available servers
  51.       _servers.Add("ServerI");
  52.       _servers.Add("ServerII");
  53.       _servers.Add("ServerIII");
  54.       _servers.Add("ServerIV");
  55.       _servers.Add("ServerV");
  56.     }

  57.     public static LoadBalancer GetLoadBalancer()
  58.     {
  59.       // Support multithreaded applications through
  60.       // 'Double checked locking' pattern which (once
  61.       // the instance exists) avoids locking each
  62.       // time the method is invoked
  63.       if (_instance == null)
  64.       {
  65.         lock (syncLock)
  66.         {
  67.           if (_instance == null)
  68.           {
  69.             _instance = new LoadBalancer();
  70.           }
  71.         }
  72.       }

  73.       return _instance;
  74.     }

  75.     // Simple, but effective random load balancer
  76.     public string Server
  77.     {
  78.       get
  79.       {
  80.         int r = _random.Next(_servers.Count);
  81.         return _servers[r].ToString();
  82.       }
  83.     }
  84.   }
  85. }