Skip to main content

Posts

Chain of Responsibility

Chain of Responsibility Pattern  Gamma Categorization: Structural Design Pattern Summary: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Sample Code Problem: We want to create a proxy class to intercept the server connection. Code public   class   Photo {      public   string   Name  {  get ;  set ; }      public   int   Exposure  {  get ;  set ; }      public   int   Saturation  {  get ;  set ; }      public   int   Contrast  {  get ;  set ; }      public   Photo ( string   name )     {          this . Name  =  name ;  ...

Proxy Pattern

Proxy Pattern  Gamma Categorization: Structural Design Pattern Summary:Provide a surrogate or placeholder for another object to control access to it. Sample Code Problem: We want to create a proxy class to intercept the server connection. Code public   class   StorageServer {      private   string   ServerURL ;      public   void   Connect ()      {            System . Console . WriteLine ( "Connecting to server." );      }      public   void   UploadFile ()      {           System . Console . WriteLine ( "Uploading a file." );      }           public   List < string >  Lis...

Flyweight Pattern

Flyweight Pattern  Gamma Categorization: Structural Design Pattern Summary:Use sharing to support large numbers of fine-grained objects efficiently. Sample Code Problem: We want to store in memory only one object per unique filter definition. Code public   class   Filter {      public   Guid   ID  {  get ;  set ; }      public   List < string >  FilterProperties  {  get ;  set ; } =  new   List < string >();      public   bool   Equals ( Filter   filter )     {          if ( this . FilterProperties . Count  !=  filter . FilterProperties . Count )              return   false ;          this . FilterProperties...

Facade Pattern

Facade Pattern  Gamma Categorization: Structural Design Pattern Summary: Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. Sample Code Problem: We want a facade for our digital camera subsystems. public   class   DigitalCameraFacade {      ICamera   camera ;              IStorageManager   storageManager ;      INetworkManager   networkManager ;      public   DigitalCameraFacade ( ICamera   camera ,  IStorageManager   storageManager ,  INetworkManager   networkManager )     {          this . camera  =  camera ;                  ...

Decorator Pattern

Decorator Pattern  Gamma Categorization: Structural Design Pattern Summary: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Sample Code Problem: We want extend our Camera object in order to allow it to use LiveView in addiction to taking photos. Code public   abstract   class   Camera {      public   string   Name  {  get ;  set ; }      public   abstract   void   TakePhoto (); } public   class   DigitalCamera  :  Camera {      public   override   void   TakePhoto () =>  System . Console . WriteLine ( $"Taking a photo with a { Name }." ); } public   abstract   class   AdvancedCamera  :  Camera {      publ...

Bridge Pattern

Bridge Pattern  Gamma Categorization: Structural Design Pattern Summary: A mechanism that decouples an interface (hierarchy) from an implementation (hierarchy). Sample Code Problem: We want to be able to implement the photo storage mechanism. Code public   interface   IStorage {      void   SaveFile ( byte []  bytes ); } public   class   InternalStorage  :  IStorage {      public   void   SaveFile ( byte []  bytes )     {          System . Console . WriteLine ( "File sent to disk." );     } }      public   class   CloudStorage  :  IStorage {      public   void   SaveFile ( byte []  bytes )     {          System . Co...

Composite Pattern

Composite Pattern  Gamma Categorization: Structural Design Pattern Summary: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Sample Code Problem: We want to store a trips photoset hierarchy. Code public   abstract   class   Photo {      protected   string   name ;      public   Photo ( string   name )     {          this . name  =  name ;     }      public   abstract   void   Add ( Photo   p );      public   abstract   void   Remove ( Photo   p );      public   abstract   void   Display ( int   depth ); } public   class   SinglePhoto ...

Adapter Pattern

Adapter Pattern  Gamma Categorization: Structural Design Pattern Summary: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Sample Code Problem: We want to be able to operate an API that will take photos with both Canon or Nikon APIs. Code public   class   CanonAPI {      public   void   TakePhoto () =>  System . Console . WriteLine ( "Taking a photo via Canon API." ); } public   class   NikonAPI {      public   void   PhotoShoot () =>  System . Console . WriteLine ( "Taking a photo via Nikon API." ); } public   interface   ICameraAPIAdapter {      void   Shoot (); } public   class   CanonAdapter  :  ICameraAPIAd...

Singleton Pattern

Singleton Pattern  Gamma Categorization: Creational Design Patten Summary: When it makes sense to only hae one instance of a component in the system, or the construction is expensive, or we want to prevent aditional copies creation. Sample Code Problem: We need to initialise a property only once. Singleton Class public   sealed   class   Singleton {      public   long   Ticks  {  get ;  private   set ; }      public   static   Singleton   Instance  {  get  {  return   lazy . Value ; } }      private   static   readonly   Lazy < Singleton >  lazy  =  new   Lazy < Singleton > (() =>  new   Singleton ());      private   Singleton ()      {      ...

Prototype Pattern

Prototype Pattern  Gamma Categorization: Creational Design Patten Summary: When the object construction reiterate existing designs. Its a partially or fully initialized object that you copy (clone) and make use of. Sample Code Problem: we need to create a Point object, that may be either Cartesian or Polar. Custom Prototype Interface public   interface   IPrototype < T > {      T   DeepCopy (); } public   class   DigitalCamera  :  IPrototype < DigitalCamera > {      public   string   Brand  {  get ;  set ; }      public   string   Model  {  get ;  set ; }      public   DateTime   Year  {  get ;  set ; }      public   override   string   ToString () =>  $"Brand: { Brand }, Mode...