Skip to main content

Posts

Showing posts from 2019

Service Locator

Service Locator Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Sample Code Problem: We want to get the service based on the interface. Code public interface IServiceLocator { T GetService < T >(); } public class ServiceLocator : IServiceLocator { private IDictionary < object , object > services ; public ServiceLocator () { services = new Dictionary < object , object >(); this . services . Add ( typeof ( IStorageService ), new CloudService ()); this . services . Add ( typeof ( INetworkService ), new WiFiService ()); } public T GetService < T >() { try { return ( T ) services [ typeof ( T )]; } catch

Visitor Pattern

Visitor Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates. Sample Code Problem: We want to correct photo filters based on the camera brand Code public   abstract   class   Element {      public   abstract   void   Accept ( IVisitor   visitor ); } public   class   Filter  :  Element {      public   string   Name  {  get ;  set ; }      public   double   Exposure  {  get ;  set ; }      public   double   Contrast  {  get ;  set ; }      public   double   Saturation  {  get ;  set ; }      public   Filter ( string   name ,  double   exposure ,  double   contrast ,  double   saturation )     {          this . Name  =  name ;          this . Exposure  =  exposure ;          this . Contrast  =  contrast ;          this . Saturation  =  saturation

Strategy Pattern

Strategy Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Sample Code Problem: We want to have a shooting strategies based on the photo conditions. Code public   abstract   class   ShootingStrategy {      public   abstract   void   TakePhoto (); } public   class   LandscapeStrategy  :  ShootingStrategy {      public   override   void   TakePhoto ()     {          Console . WriteLine ( "Taking photo with low ISO and great focal distance." );     } } public   class   SportsStrategy  :  ShootingStrategy {      public   override   void   TakePhoto ()     {          Console . WriteLine ( "Taking photo with high ISO and fast shutter speed." );     } } public   class   ShootingMethod {      private   ShootingStrategy   _shootingStrategy ;   

Template Pattern

Template Pattern  Gamma Categorization: Behavioral Design Pattern Summary:  Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Sample Code Problem: We want to have a digital camera template, which should be used or overriden by it's concrete classes. Code public   abstract   class   DigitalCamera {      public   void   TakePhoto ()     {          System . Console . WriteLine ( "Taking photo." );     }      public   abstract   void   LiveView ();      public   virtual   void   FireFlash ()     {          System . Console . WriteLine ( "Firing flash." );     } } public   class   Canon6D  :  DigitalCamera {      public   override   void   LiveView ()     {          System . Console . WriteLine ( "Using LiveView on a 1040kdots LCD." );     }     

Null Object Pattern

Null Object Pattern  Gamma Categorization: Behavioral Design Pattern Summary:  A no-op object that conforms to the required interface, satisfying a dependency requirement of some other object. Sample Code Problem: We want to be able to apply a null filter. Code public   class   Photo {      public   int   Exposure  {  get ;  set ; } =  0 ;      public   int   Saturation  {  get ;  set ; } =  0 ;      public   int   Contrast  {  get ;  set ; } =  0 ;      public   override   string   ToString ()     {          return   $"Exposure: { Exposure }, Contrast: { Contrast }, Saturation: { Saturation }" ;     } } public   class   PhotoEditor {      public   void   ApplyFilter ( Photo   photo ,  IFilter   filter ) =>  filter . ApplyFilter ( photo ); } public   interface   IFilter {      void   ApplyFilter ( Photo   photo ); } public   class   ImpactFilter  :  IFilter {      public   void   ApplyFilter ( Photo   photo )      {

Observable Pattern

Observable Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.. Sample Code Problem: We want to trigger several events on the digital camera after a photo is taken. Code public   class   DigitalCamera   {      public   event   EventHandler   PictureTaken ;      public   void   TakePicture ()     {          System . Console . WriteLine ( "Taking picture." );          PictureTaken ?. Invoke ( this ,  EventArgs . Empty );     } } public   class   CameraSensorUtilities {      public   void   PictureTakenEventHandler ( object   sender ,  EventArgs   args )     {          System . Console . WriteLine ( "Cleaning digital sensor." );     } } public   class   SystemCounters {      public   void   PictureTakenEventHandler ( object   sender ,  EventArgs   args )  

Mediator Pattern

Mediator Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. Sample Code Problem: We want to apply several commands on the digital camera. Code public   class   Person {      public   string   Name ;      public   ChatRoom   Room ;      private   List < string >  chatLog  =  new   List < string >();      public   Person ( string   name )     {          Name  =  name ;     }      public   void   Receive ( string   sender ,  string   message )     {          string   s  =  $"{ sender }: '{ message }'" ;          System . Console . WriteLine ( $"[{ Name }'s chat session] { s }" );          chatLog . Add ( s );     }      public   void   Say ( string   message )     {    

Memento Pattern

Command Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. Sample Code Problem: We want to apply several commands on the digital camera. Code public   class   DigitalCameraFirmware {      public   string   Name  {  get ;  set ; }      public   byte []  Data  {  get ;  set ; }      public   Memento   SaveMemento ()     {          return   new   Memento ( Name ,  Data );     }      public   void   RestoreMemento ( Memento   memento )     {          this . Name  =  memento . Name ;          this . Data  =  memento . Data ;     } } public   class   Memento {      public   string   Name  {  get ;  private   set ; }      public   byte []  Data  {  get ;  private   set ; }      public   Memento ( string   name ,  byte []  data )     {          this . Name  =  name ;         

Iterator Pattern

Iterator Pattern  Gamma Categorization: Behavioral Design Pattern Summary: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Sample Code Problem: We want to apply several commands on the digital camera. Code public   class   Photo {      public   string   Name  {  get ;  set ; }      public   Photo ( string   name )     {          this . Name  =  name ;     } } public   interface   IPhotoCollection {      IPhotoIterator   CreateIterator (); } public   interface   IPhotoIterator {      Photo   First ();      Photo   Next ();      bool   IsDone  {  get ; }      Photo   CurrentPhoto  {  get ; } } public   class   PhotoCollection  :  IPhotoCollection {      private   ArrayList   _items  =  new   ArrayList ();      public   IPhotoIterator   CreateIterator ()     {          return   new   PhotoIterator ( this );     }      public   int   Count    

Command Pattern

Command Pattern  Gamma Categorization: Behavioral 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 apply several commands on the digital camera. Code public   interface   ICommand {      void   Execute (); } public   enum   CameraCommands  {  ShutterSpeed ,  FocalDistance ,  ISO ,  Shoot  } public   class   ShutterSpeedCommand  :  ICommand {      private   float   shutterSpeed ;      public   ShutterSpeedCommand ( float   shutterSpeed )     {          this . shutterSpeed  =  shutterSpeed ;     }      public   void   Execute ()     {          Console . WriteLine ( $"Setting shutter speed to { shutterSpeed } seconds." );     } } public   class   FocalDistanceCommand  :  ICommand {      private   int   focalDistance ;      public   FocalDistanceCommand ( int   f