close

image

LongFormArtistResource: (A, 1)

LongFormBookResource: (A, 2)

ShortFormArtistResource: (B, 1)

ShortFormBookResource: (B, 2)

There are four combinations and it is composed of View(A & B) and Resource(1 & 2).

Obviously, some classes metioned above are duplicate, which are highlighted with blue and red color.

image

Once there are four sub-classes of Resouce, the number of all possible classes increases to eight (2 by 4).

The total number keeps small if we build a has_a bridge between the two classes.

In this case the total number of sub-classes is six(2 plus4) instead of eight.

image

   1: namespace BridgePattern
   2: {
   3:     interface IResource
   4:     {
   5:         string snippet();
   6:         string title();
   7:         string images();
   8:         string url();
   9:     }
  10: }

image
   1: namespace BridgePattern
   2: {
   3:     class Artist
   4:     {
   5:         private string biography;
   6:         private string firstName;
   7:         private string lastName;
   8:         private string imagePath;
   9:         private string urlPath;
  10:         public Artist(string firstName, string lastName, string biography, string imagePath, string urlPath)
  11:         {
  12:             this.biography = biography;
  13:             this.firstName = firstName;
  14:             this.lastName = lastName;
  15:             this.imagePath = imagePath;
  16:             this.urlPath = urlPath;
  17:         }
  18:         public string bio()
  19:         {
  20:             return biography;
  21:         }
  22:         public string fName() { return this.firstName; }
  23:         public string lName() { return this.lastName; }
  24:         public string image() { return this.imagePath; }
  25:         public string url() { return this.urlPath; }
  26:     }
  27:     class ArtistResouce : IResource
  28:     {
  29:         Artist artist;
  30:         public ArtistResouce(Artist artist)
  31:         {
  32:             this.artist = artist;
  33:         }
  34:         public string images()
  35:         {
  36:             return this.artist.image();
  37:         }
  38:  
  39:         public string snippet()
  40:         {
  41:             return this.artist.bio();
  42:         }
  43:  
  44:         public string title()
  45:         {
  46:             return this.artist.fName() + " " + this.artist.lName();
  47:         }
  48:  
  49:         public string url()
  50:         {
  51:             return this.artist.url();
  52:         }
  53:     }
  54: }

image
   1: namespace BridgePattern
   2: {
   3:     abstract class View
   4:     {
   5:         protected IResource resource;
   6:         public View(IResource resource)
   7:         {
   8:             this.resource = resource;
   9:         }
  10:         public abstract string Show();
  11:     }
  12:     class LongForm : View
  13:     {
  14:         public LongForm(IResource resource) : base(resource)
  15:         {
  16:  
  17:         }
  18:         public override string Show()
  19:         {
  20:             string html = string.Format("{0}\n{1}\n{2}", resource.title(), resource.images(), resource.snippet());
  21:             return this.resource.snippet();
  22:         }
  23:     }
  24: }
arrow
arrow
    全站熱搜

    me1237guy 發表在 痞客邦 留言(0) 人氣()