SHARE
TWEET

Extension Method on Factory Class

fenterbug Aug 5th, 2016 (edited) 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace Cinema {
  2.     public interface ICinema {
  3.     }
  4.  
  5.     public sealed class CinemaFactory {
  6.         // Singleton pattern to support extension methods. (Can't add extension methods to static classes.)
  7.         private static readonly Lazy<CinemaFactory> LazyInstance = new Lazy<CinemaFactory> (() => new CinemaFactory());
  8.  
  9.         public static CinemaFactory Instance {
  10.         get {
  11.                 return LazyInstance.Value;
  12.             }
  13.         }
  14.  
  15.         private CinemaFactory () {
  16.         }
  17.  
  18.         public ICinema  GetCinema (EndpointAddress10 RemoteCinemaUri) {
  19.             ICinema CinemaEngineInstance;
  20.  
  21.             // Get a reference from the WCF service...
  22.  
  23.             return CinemaEngineInstance;
  24.         }
  25.     }
  26. }
  27.  
  28. namespace Cinema.Engine {
  29.     using Cinema;
  30.  
  31.     public class CinemaEngine : ICinema {
  32.     }
  33.  
  34.     // Add polymorphism to the class defined in a different project.
  35.     public static class CinemaFactoryHelper {
  36.         public static ICinema GetCinema (this CinemaFactory _This) {
  37.             return new CinemaEngine ();
  38.         }
  39.     }
  40. }
  41.  
  42. namespace Cinema.Client1 {
  43.     using Cinema;
  44.  
  45.     public class Consumer1 {
  46.         // Intellisense shows that there are no overloads to the GetCinema() method. Yay!
  47.         private ICinema ref = CinemaFactory.Instance.GetCinema (EndpointAddress10.FromEndpointAddress (new EndpointAddress ("http://localhost"));
  48.     }
  49. }
  50.  
  51. namespace Cinema.Client2 {
  52.     using Cinema;
  53.     using Cinema.Engine; // Add visibility to the extension method that provides a local instance of the engine.
  54.  
  55.     public class Consumer2 {
  56.         // Intellisense shows the method +1 overload. Yay!
  57.         private ICinema ref = CinemaFactory.Instance.GetCinema ();
  58.     }
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top