1) Understanding Design Patterns
MVC,
MVP
and
MVVM
There are three three most popular MV-* MV-* design patterns: MVC, MVP and MVVM. These These are widel used ! the "arious te#hnologies. $n this arti#le, $ will pro"ide m opinion on these three.
MVC Pattern MVC stan stands ds %or Mod Modelel-Vi Viewew-Con Contr trolle ollerr. $t is a so% so%twar tware e des design ign pat patter tern n whi# whi#h h was introdu intr odu#ed #ed in 1&' 1&'(s. (s. lso lso,, MVC pattern pattern %or %or#es #es a sep separat aration ion o% #on #on#er #erns, ns, it mea means ns domain model and #ontroller logi# are de#oupled %rom user inter%a#e "iew). s a result maintenan#e and testing o% the appli#ation !e#ome simpler and easier. MVC design pat patter tern n spli splits ts an app appli# li#atio ation n into three main asp aspe#t e#ts: s: Mode Model, l, Vi View ew and Controller
1.
Model
The Model represents a set o% #lasses that des#ri!e the !usiness logi# i.e. !usiness model as well as data a##ess operations i.e. data model. $t also de+nes !usiness rules %or data means how the data #an !e #hanged and manipulated.
.
View
The View represents the U$ #omponents lie C, /0uer, h tml et#. $t is onl responsi!le %or displaing the data that is re#ei"ed %rom the #ontroller as the result. This also trans%orms the models) into U$.
.
Controller
The Controller is responsi!le to pro#ess p ro#ess in#oming re2uests. $t re#ei"es input %rom users "ia the View, then pro#ess the user3s data with the help o% Model and passing the results !a# to the View. Tpi#all, it a#ts as the #oordinator !etween the View and the Model. Toda, T oda, this pattern is used ! man popular %ramewor lie as 4u! on 4ails, pring 5ramewor, pple i6 De"elopment and P.78T P.78T MVC.
MVP pattern This pattern is similar to MVC pattern in whi#h #ontroller has !een repla#ed ! the presenter. This design pattern splits an appli#ation into three main aspe#ts: Model, View and Presenter.
1.
Model
The Model represents a set o% #lasses #lass es that des#ri!es the !usiness logi# and data. d ata. $t also de+nes !usiness rules %or data means how the data #an !e #hanged and manipulated.
.
View
The View represents the U$ #omponents lie C, /0uer, h tml et#. $t is onl responsi!le %or displaing the data that is re#ei"ed %rom the presenter as the result. This also trans%orms the models) into U$.
.
Presenter
The Presenter is responsi!le %or handling all U$ e"ents on !ehal% o% the "iew. "iew. This re#ei"e input %rom users "ia the View View,, then pro#ess the user3s data with the help o% Model and passing the results !a# to the View. Unlie "iew and #ontroller, "iew and presenter are #omple #om pletel tel de#ouple de#oupled d %r %rom om ea# ea#h h othe other9s r9s and #om #ommuni muni#at #ate e to ea# ea#h h oth other9 er9s s ! an inter%a#e. lso, presenter does not manage mana ge the in#oming re2uest tra# tra # as #ontroller. This pattern is #ommonl used with P P.78T .78T ;e! 5orms appli#ations whi#h re2uire to #reate automated unit tests %or their #ode-!ehind pages. This is also used with windows %orms.
User intera#ts with the View.
.
The Th ere is is oneone-to to-o -one ne rel elat atio ions nshi hip p !et !etwe ween en Vie iew w and and Pres ese ent nter er me mea ans one one Vie iew w is is mapped to onl one Presenter. Presenter.
.
Vie iew w ha has a re% e%er ere en# n#e e to to Pr Pres esen ente terr !ut !ut Vie iew w ha has not not re% e%er eren en# #e to to Mod Mode el.
=.
Pro"i "id des tw two wa wa #o #ommun unii#atio ion n !e !etween Vie View w an and Pr Prese sen nter.
MVVM pattern MVVM stands %or Model-View-View Model. This pattern supports two-wa data !inding !etween "iew and View model. This ena!les automati# propagation o% #hanges, within the state o% "iew model to t o the View. Tpi#all, Tpi#all, the "iew model uses the o!ser"er pattern to noti% #hanges in the "iew model to model.
1.
Model
The Model represents a set o% #lasses #lass es that des#ri!es the !usiness logi# and data. d ata. $t also de+nes !usiness rules %or data means how the data #an !e #hanged and manipulated.
.
View
The View represents the U$ #omponents lie C, /0uer, h tml et#. $t is onl responsi!le %or displaing the data that is re#ei"ed %rom the #ontroller as the result. This also trans%orms the models) into U$..
.
View Model
The View Model is responsi!le %or % or e>posing methods, #ommands, and other properties that helps to maintain the state o% the "iew, manipulate the model as the result o% a#tions on the "iew, and trigger e"ents in the "iew itsel%. This pattern is #ommonl used ! the ;P5, il"erlight, Cali!urn, n4oute n4oute et#.
User intera#ts with the View.
.
The herre is ma man-to to-o -one ne re rela lattio ion nshi hip p !et !etwe wee en Vie View w and and Vie View wModel mean means s man man View #an !e mapped to one ViewModel.
.
Vie iew w has has a re% re%er ere en# n#e e to to Vie ViewM wMod odel el !u !utt Vie View w Mod Mode el has has no in in%o %orrma mati tion on a! a!ou outt the the View.
=.
uppo porrts two wo--wa da datta !i !in ndi ding ng !etween Vie iew w and ViewModel.
)
ingleton Design Pattern - C?
ingleton pattern is one o% the simplest design patterns. This pattern ensures that a #lass has onl one instan#e and pro"ides a glo!al point o% a##ess to it.
;hat is ingleton Pattern@ ingleton pattern is one o% the simplest design patterns. This pattern ensures that a #lass has onl one instan#e and pro"ides a glo!al point o% a##ess to it.
ingleton Pattern Pattern - UMA Diagram B $mplementation The UMA #lass diagram %or the implementation implementat ion o% the ingleton design pattern p attern is gi"en !elow:
The #lasses, and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
ingleton
This is a #lass whi#h is responsi!le %or #reating and maintaining its own uni2ue instan#e.
C? - $mplementation Code 1. //eager initialization of singleton 2. public class Singleton 3. { 4.
private static Singleton Singleton instance instance = new Singleton Singleton(); ();
5.
private Singleton Singleton() () { }
6. .
public static Singleton !et"nstance
#. { $.
get
1%. { 1%. { 11. return return instance; instance; 12. } 12. } 13. } 13. } 14.} 14. } 15. 16. ////lazy ////lazy initialization of singleton 1.public 1. public class Singleton 1#.{ 1#. { 1$. private static Singleton Singleton instance instance = null null;; 2%. private Singleton Singleton() () { } 21.
22. public static Singleton !et"nstance 23. { 23. { 24. get 25. { 25. { 26. i& (instance (instance == null null)) 2. instance 2. instance = new Singleton Singleton(); (); 2#. 2$. return return instance; instance; 3%. } 3%. } 31. } 31. } 32.} 32. } 33. 34. ////Thread-safe ////Thread-safe (Double-checked (Double-checked Locking) initialization initialization of singleton 35.public 35. public class Singleton 36.{ 36. { 3. private static Singleton Singleton instance instance = null null;; 3#. private Singleton Singleton() () { } 3$. private static ob'ect ob'ect loc*is loc*is = new ob'ect ob'ect(); (); 4%. 41. public static Singleton !et"nstance 42. { 42. { 43. get 44. { 44. { 45. loc (loc*is) (loc*is) 46. { 46. {
4. i& (instance (instance == null null)) 4#. instance 4#. instance = new Singleton Singleton(); (); 4$. 5%. return return instance; instance; 51. } 51. } 52. } 52. } 53. } 53. } 54.} 54. }
ingleton Pattern - 8>ample
;ho is what@ The #lasses and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows: 1.
Singleton - ingleton #lass
C? - ample Code 1. ///
2. /// The 'ingleton' class
3. /// 4. public class Singleton 5. { 6. // !"#T guarantees thread safety for static static initialization .
private static Singleton Singleton instance instance = null null;;
#.
private string +a,e +a,e{ {get get;;set set;} ;}
$.
private string string "-{ "-{get get;;set set;} ;}
1%. private Singleton Singleton() () 11. { 11. { 12. //T //To o D$% &emoe belo line 13. onsole onsole../rite0ine /rite0ine((Singleton "ntance); "ntance); 14. 15. +a,e +a,e = = Server1 Server1;; 16. "16. "- = 1$2.16#.1.23 ; 1. } 1. } 1#. // Lock synchronization obect 1$. private static ob'ect ob'ect snc0oc snc0oc = new ob'ect ob'ect(); (); 2%. 21. public static Singleton "nstance 22. { 22. { 23. get 24. { 24. { 25. // u**ort multithreaded a**lications through 26. // 'Double checked checked locking' *attern hich (once (once 2. // the instance e+ists) aoids aoids locking each
2#. // time the method is inoked inoked 2$. loc (snc0oc) (snc0oc) 3%. { 3%. { 31. i& (Singleton (Singleton.instance .instance == null null)) 32. Singleton Singleton.instance .instance = new Singleton Singleton(); (); 33. 34. return Singleton Singleton.instance; .instance; 35. } 35. } 36. } 36. } 3. } 3. } 3#. 3$. public voi S*ow S*ow() () 4%. { 4%. { 41. onsole. onsole./rite0ine /rite0ine((S Ser erve verr "n "n&o &or, r,at atio ion n is +a +a,e ,e={ ={%} %} ""-={ ={1} 1} "- +a,e); +a,e ); 42. } 42. } 43. 44.} 44. } 45. 46. /// ///
4. /// /// ingleton ,attern ,attern Demo 4#. /// /// 4$. /// /// 5%.class 5%. class -rogra, 51.{ 51. { 52. static voi 7ain 7ain((string string89 89 args)
53. { 53. { 54. Singleton Singleton.."nstance "nstance..S*ow S*ow(); (); 55. Singleton Singleton.."nstance "nstance..S*ow S*ow(); (); 56. 5. onsole onsole..:eae :eae(); (); 5#. } 5#. } 5$.} 5$. }
ingleton Pattern Demo - 6utput
;hen to use it@ 1.
8>a#tl one instan#e o% a #lass is re2uired.
.
Controlled a# a##ess to to a single o!/e#t is is ne#essar.
)
Prototpe Design Pattern - C?
Prototpe pattern is used to #reate a dupli#ate o!/e#t or #lone o% the #urrent o!/e#t to enhan#e per%orman#e. This pattern is used when #reation o% o!/e#t is #ostl or #omple>.
;hat is Prototpe Pattern@ Prototpe pattern is used to #reate a dupli#ate o!/e#t or #lone o% the #urrent o!/e#t to enhan#e per%orman#e. This pattern is used when #reation o% o!/e#t is #ostl or #omple>.
a,ple n o!/e#t is to !e #reated a%ter a #ostl data!ase operation. ;e #an #a#he the o!/e#t, returns its #lone on ne>t re2uest and update the data!ase as and when needed thus redu#ing data!ase #alls.
Prototpe Pattern - UMA Diagram B $mplementation The UMA #lass diagram %or the th e implementation implementat ion o% the Prototpe design pattern is gi"en gi "en !elow:
The #lasses, inter%a#es and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
Prototpe
This is an inter%a#e whi#h is used %or the tpes o% o!/e#t that #an !e #loned itsel%. itsel%.
.
Con#retePrototpe
This is a #lass whi#h implements the Prototpe inter%a#e %or #loning itsel%. itsel%.
C? - $mplementation Code 1. public inter&ace -rototpe 2. { 3.
-rototpe lone lone(); ();
4. } 5.
6. public class oncrete-rototpe? oncrete-rototpe? -rototpe . { #.
public -rototpe lone lone() ()
$. { 1%. // hallo o*y% only to*-leel obects obects are du*licated 11. return return ( (-rototpe -rototpe))7e,berwiselone 7e,berwiselone(); (); 12. 13. // Dee* o*y% all obects are du*licated du*licated 14. //return (,rototy*e)this!lone(). (,rototy*e)this!lone(). 15. } 15. } 16.} 16. } 1. 1#.public 1#. public class oncrete-rototpe@ oncrete-rototpe@ -rototpe 1$.{ 1$. { 2%. public -rototpe lone lone() () 21. { 21. { 22. // hallo o*y% only to*-leel obects obects are du*licated 23. return return ( (-rototpe -rototpe))7e,berwiselone 7e,berwiselone(); (); 24. 25. // Dee* o*y% all obects are du*licated du*licated 26. //return (,rototy*e)this!lone(). (,rototy*e)this!lone(). 2. } 2. } 2#.} 2#. }
Prototpe Pattern - 8>ample
;ho is what@ The #lasses, inter%a#es and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows: 1.
",ploee - Prototpe inter%a#e
.
Aeveloper pist- Con#rete Prototpe
C? - ample Code 1. /// 2. /// The ',rototy*e' interface interface 3. /// 4. public inter&ace ",ploee 5. {
6.
",ploee lone lone(); ();
.
string !etAetails !etAetails(); ();
#. } $. 1%. /// /// 11. /// /// 'oncrete,rototy*e' class 12. /// /// 13.public 13. public class Aeveloper Aeveloper ",ploee 14.{ 14. { 15. public int /ors-er7inute /ors-er7inute { { get get;; set set;; } 16. public string +a,e +a,e { { get get;; set set;; } 1. public string :ole :ole { { get get;; set set;; } 1#. public string -re&erre0anguage -re&erre0anguage { { get get;; set set;; } 1$. 2%. public ",ploee lone lone() () 21. { 21. { 22. // hallo o*y% only to*-leel obects obects are du*licated 23. return return ( (",ploee ",ploee))7e,berwiselone 7e,berwiselone(); (); 24. 25. // Dee* o*y% all obects are du*licated du*licated 26. //return (0#m*loyee)this!lone(). 2. } 2. } 2#. 2$. public string !etAetails !etAetails() () 3%. { 3%. {
31. return string string.. 36. /// /// 'oncrete,rototy*e' class 3. /// /// 3#.public 3#. public class pist pist ",ploee 3$.{ 3$. { 4%. public int /ors-er7inute /ors-er7inute { { get get;; set set;; } 41. public string +a,e +a,e { { get get;; set set;; } 42. public string :ole :ole { { get get;; set set;; } 43. 44. public ",ploee lone lone() () 45. { 45. { 46. // hallo o*y% only to*-leel obects obects are du*licated 4. return return ( (",ploee ",ploee))7e,berwiselone 7e,berwiselone(); (); 4#. 4$. // Dee* o*y% all obects are du*licated du*licated 5%. //return (0#m*loyee)this!lone(). 51. } 51. } 52. 53. public string !etAetails !etAetails() () 54. { 54. { 55. return string string..
56. } 56. } 5.} 5. } 5#. 5$. /// /// 6%. /// /// ,rototy*e ,attern ,attern Demo 61. /// /// 62. 63.class 63. class -rogra, 64.{ 64. { 65. static voi 7ain 7ain((string string89 89 args) 66. { 66. { 6. Aeveloper Aeveloper ev ev = new Aeveloper Aeveloper(); (); 6#. ev. 6#. ev.+a,e +a,e = = :a*ul :a*ul;; 6$. ev. 6$. ev.:ole :ole = = ea, ea, 0eaer 0eaer;; %. ev. %. ev.-re&erre0anguage -re&erre0anguage = = C C;; 1. 2. Aeveloper Aeveloper evop evop = (Aeveloper (Aeveloper)ev. )ev.lone lone(); (); 3. evop. 3. evop.+a,e +a,e = ?ri&; ?ri&; //"ot mention &ole and ,referredLanguage1 it ill co*y aboe 4. 5. onsole onsole../rite0ine /rite0ine(ev. (ev.!etAetails !etAetails()); ()); 6. onsole onsole../rite0ine /rite0ine(evop. (evop.!etAetails !etAetails()); ()); . #. pist pist tpist tpist = new pist pist(); (); $. tpist. $. tpist.+a,e +a,e = = 7onu 7onu;; #%. tpist. #%. tpist.:ole :ole = = pist pist;;
#1. tpist. #1. tpist./ors-er7inute /ors-er7inute = = 12% 12%;; #2. #3. pist pist tpistop tpistop = (pist (pist)tpist. )tpist.lone lone(); (); #4. tpistop. #4. tpistop.+a,e +a,e = = Sa*il Sa*il;; #5. tpistop. #5. tpistop./ors-er7inute /ors-er7inute = = 115 115;; //"ot //"ot mention &ole1 &ole1 it ill co*y aboe #6. #. onsole onsole../rite0ine /rite0ine(tpist. (tpist.!etAetails !etAetails()); ()); ##. onsole onsole../rite0ine /rite0ine(tpistop. (tpistop.!etAetails !etAetails()); ()); #$. $%. onsole onsole..:eae :eae(); (); $1. $2. } $2. } $3.} $3. }
Prototpe Pattern Demo - 6utput
;hen to use it@ 1.
The #reation o% ea#h o!/e#t is #ostl or #omple>.
.
lim imiited num!er o% st sta ate #om! m!iina nattions e>ist in an o!/ !/e e#t.
=)
uilder Design Pattern - C?
uilder pattern !uilds a #omple> o!/e#t ! using simple o!/e#ts and a step ! step approa#h. uilder #lass !uilds the +nal o!/e#t step ! step. This !uilder is independent o% other o!/e#ts. The !uilder pattern des#ri!es a wa to separate an o!/e#t %rom its
#onstru#tion. The same #onstru#tion method #an #reate dierent representation o% the o!/e#t.
;hat is uilder Pattern@ uilder uil der pattern pattern !uilds a #om #omple ple> > o!/e o!/e#t #t ! using a ste step p ! ste step p appr approa# oa#h. h. ui uilder lder inter%a#e de+nes the steps to !uild the +nal o!/e#t. This !uilder is independent %rom the o!/e#ts #reation pro#ess. #lass that is nown as Dire#tor, #ontrols the o!/e#t #reation pro#ess. Moreo"er, !uilder pattern des#ri!es a wa to separate an o!/e#t %rom its #onstru#tion. The same #onstru#tion method #an #reate dierent dierent representation o% the o!/e#t.
uilder Pattern - UMA Diagram B $mplementation The UMA #lass diagram %or the implementation o% the !uilder design pattern is gi"en !elow:
The #lasses, inter%a#es and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
uilder
This is an inter%a#e whi#h is used to de+ne all the steps to #reate a produ#t produ#t
.
Con#reteuilder
This is a #lass whi#h implements the uilder inter%a#e to #reate #omple> produ#t. produ#t.
.
Produ#t
This is a #lass whi#h de+nes the t he parts o% the #omple> o!/e#t whi#h are to !e generated ! the !uilder pattern.
=.
Dire#tor
This is a #lass whi#h is used to #onstru#t an o!/e#t using the uilder inter%a#e.
C? - $mplementation Code 1. public inter&ace "@uiler 2. { 3.
voi @uil-art1 @uil-art1(); ();
4.
voi @uil-art2 @uil-art2(); ();
5.
voi @uil-art3 @uil-art3(); ();
6.
-rouct !et-rouct !et-rouct(); ();
. } #. $. public class oncrete@uiler oncrete@uiler "@uiler 1%.{ 1%. { 11. private -rouct -rouct Dprouct Dprouct = new -rouct -rouct(); (); 12. 13. public voi @uil-art1 @uil-art1() () 14. { 14. { 15. Dprouct. 15. Dprouct.-art1 -art1 = = -art 1; 1; 16. } 16. } 1.
1#. public voi @uil-art2 @uil-art2() () 1$. { 1$. { 2%. Dprouct. 2%. Dprouct.-art2 -art2 = = -art 2; 2; 21. } 21. } 22. 23. public voi @uil-art3 @uil-art3() () 24. { 24. { 25. Dprouct. 25. Dprouct.-art3 -art3 = = -art 3; 3; 26. } 26. } 2. 2#. public -rouct !et-rouct !et-rouct() () 2$. { 2$. { 3%. return return Dprouct; Dprouct; 31. } 31. } 32.} 32. } 33. 34.public 34. public class -rouct 35.{ 35. { 36. public string -art1 -art1 { { get get;; set set;; } 3. public string -art2 -art2 { { get get;; set set;; } 3#. public string -art3 -art3 { { get get;; set set;; } 3$.} 3$. } 4%. 41.public 41. public class Airector 42.{ 42. {
43. public voi onstruct onstruct(("@uiler "@uiler "@uiler)) 44. { 44. { 45. "@uiler "@uiler..@uil-art1 @uil-art1(); (); 46. "@uiler "@uiler..@uil-art2 @uil-art2(); (); 4. "@uiler "@uiler..@uil-art3 @uil-art3(); (); 4#. } 4#. } 4$.} 4$. }
uilder Pattern - 8>ample
;ho is what@ The #lasses, inter%a#es and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows:
1.
"Ee*icle@uiler - uilder inter%a#e
.
Fero@uiler Fona@uiler - Con#rete uilder
.
Ee*icle- Produ#t
=.
Ee*icle reator - Dire#tor
C? - ample Code 1. /// 2. /// The '2uilder' interface 3. /// 4. public inter&ace "Ee*icle@uiler 5. { 6.
voi Set7oel Set7oel(); ();
.
voi Setngine Setngine(); ();
#.
voi Setrans,ission Setrans,ission(); ();
$.
voi Set@o Set@o(); ();
1%. voi Set?ccessories Set?ccessories(); (); 11. 12. Ee*icle !etEe*icle !etEe*icle(); (); 13.} 13. } 14. 15. /// /// 16. /// /// The 'oncrete2uilder3' class 1. /// /// 1#.public 1#. public class Fero@uiler Fero@uiler "Ee*icle@uiler 1$.{ 1$. {
2%. Ee*icle Ee*icle ob'Ee*icle ob'Ee*icle = new Ee*icle Ee*icle(); (); 21. public voi Set7oel Set7oel() () 22. { 22. { 23. ob'Ee*icle. 23. ob'Ee*icle.7oel 7oel = = Fero Fero;; 24. } 24. } 25. 26. public voi Setngine Setngine() () 2. { 2. { 2#. ob'Ee*icle. 2#. ob'Ee*icle.ngine ngine = = 4 Stroe; Stroe; 2$. } 2$. } 3%. 31. public voi Setrans,ission Setrans,ission() () 32. { 32. { 33. ob'Ee*icle. 33. ob'Ee*icle.rans,ission rans,ission = = 12% ,G*r; ,G*r; 34. } 34. } 35. 36. public voi Set@o Set@o() () 3. { 3. { 3#. ob'Ee*icle. 3#. ob'Ee*icle.@o @o = = -lastic -lastic;; 3$. } 3$. } 4%. 41. public voi Set?ccessories Set?ccessories() () 42. { 42. { 43. ob'Ee*icle. 43. ob'Ee*icle.?ccessories ?ccessories..? ?((Seat over); over); 44. ob'Ee*icle. 44. ob'Ee*icle.?ccessories ?ccessories..? ?((:ear 7irror); 7irror);
45. } 45. } 46. 4. public Ee*icle !etEe*icle !etEe*icle() () 4#. { 4#. { 4$. return return ob'Ee*icle; ob'Ee*icle; 5%. } 5%. } 51.} 51. } 52. 53. /// /// 54. /// /// The 'oncrete2uilder4' class 55. /// /// 56.public 56. public class Fona@uiler Fona@uiler "Ee*icle@uiler 5.{ 5. { 5#. Ee*icle Ee*icle ob'Ee*icle ob'Ee*icle = new Ee*icle Ee*icle(); (); 5$. public voi Set7oel Set7oel() () 6%. { 6%. { 61. ob'Ee*icle. 61. ob'Ee*icle.7oel 7oel = = Fona Fona;; 62. } 62. } 63. 64. public voi Setngine Setngine() () 65. { 65. { 66. ob'Ee*icle. 66. ob'Ee*icle.ngine ngine = = 4 Stroe; Stroe; 6. } 6. } 6#. 6$. public voi Setrans,ission Setrans,ission() ()
%. { %. { 1. ob'Ee*icle. 1. ob'Ee*icle.rans,ission rans,ission = = 125 ,G*r; ,G*r; 2. } 2. } 3. 4. public voi Set@o Set@o() () 5. { 5. { 6. ob'Ee*icle. 6. ob'Ee*icle.@o @o = = -lastic -lastic;; . } . } #. $. public voi Set?ccessories Set?ccessories() () #%. { #%. { #1. ob'Ee*icle. #1. ob'Ee*icle.?ccessories ?ccessories..? ?((Seat over); over); #2. ob'Ee*icle. #2. ob'Ee*icle.?ccessories ?ccessories..? ?((:ear 7irror); 7irror); #3. ob'Ee*icle. #3. ob'Ee*icle.?ccessories ?ccessories..? ?((Fel,et Fel,et); ); #4. } #4. } #5. #6. public Ee*icle !etEe*icle !etEe*icle() () #. { #. { ##. return return ob'Ee*icle; ob'Ee*icle; #$. } #$. } $%.} $%. } $1. $2. /// /// $3. /// /// The ',roduct' ',roduct' class $4. /// ///
$5.public $5. public class Ee*icle $6.{ $6. { $. public string 7oel 7oel { { get get;; set set;; } $#. public string ngine ngine { { get get;; set set;; } $$. public string rans,ission rans,ission { { get get;; set set;; } 1%%. public string @o @o { { get get;; set set;; } 1%1. public 0ist 0istHstringI HstringI ?ccessories ?ccessories { { get get;; set set;; } 1%2. 1%3. public Ee*icle Ee*icle() () 1%4. { 1%5. ?ccessories ?ccessories = = new 0ist 0istHstringI HstringI(); (); 1%6. } 1%. 1%#. public voi S*ow"n&o S*ow"n&o() () 1%$. { 11%. onsole onsole../rite0ine /rite0ine((7oel {%} {%} 7oel 7oel); ); 111. onsole onsole../rite0ine /rite0ine((ngine {%} {%} ngine ngine); ); 112. onsole onsole../rite0ine /rite0ine((@o {%} {%} @o @o); ); 113. onsole onsole../rite0ine /rite0ine((rans,ission {%} {%} rans,ission rans,ission); ); 114. onsole onsole../rite0ine /rite0ine((?ccessories ?ccessories); ); 115. &oreac* &oreac* ( (var var accessor accessor in ?ccessories ?ccessories)) 116. { 11. onsole onsole../rite0ine /rite0ine((Jt{%} Jt{%} accessor); 11#. } 11$. }
12%. } 121. 122. /// 123. /// The 'Director' class 124. /// 125. public class Ee*iclereator 126. { 12. private reaonl "Ee*icle@uiler "Ee*icle@uiler ob'@uiler; ob'@uiler; 12#. 12$. public Ee*iclereator Ee*iclereator(("Ee*icle@uiler "Ee*icle@uiler builer) builer) 13%. { 131. ob'@uiler = builer; 132. } 133. 134. public voi reateEe*icle reateEe*icle() () 135. { 136. ob'@uiler. ob'@uiler.Set7oel Set7oel(); (); 13. ob'@uiler. ob'@uiler.Setngine Setngine(); (); 13#. ob'@uiler. ob'@uiler.Set@o Set@o(); (); 13$. ob'@uiler. ob'@uiler.Setrans,ission Setrans,ission(); (); 14%. ob'@uiler. ob'@uiler.Set?ccessories Set?ccessories(); (); 141. } 142. 143. public Ee*icle !etEe*icle !etEe*icle() () 144. {
145. return return ob'@uiler. ob'@uiler.!etEe*icle !etEe*icle(); (); 146. } 14. } 14#. 14$. /// 15%. /// 2uilder Design ,attern ,attern Demo 151. /// 152. class -rogra, 153. { 154. static voi 7ain 7ain((string string89 89 args) 155. { 156. var var ve*iclereator ve*iclereator = new Ee*iclereator Ee*iclereator((new Fero@uiler Fero@uiler()); ()); 15. ve*iclereator. ve*iclereator.reateEe*icle reateEe*icle(); (); 15#. var var ve*icle ve*icle = ve*iclereator.!etEe*icle ve*iclereator.!etEe*icle(); (); 15$. ve*icle. ve*icle.S*ow"n&o S*ow"n&o(); (); 16%. 161. onsole onsole../rite0ine /rite0ine((BBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBB BBBBBBB ); 162. 163. ve*iclereator = new Ee*iclereator Ee*iclereator((new Fona@uiler Fona@uiler()); ()); 164. ve*iclereator. ve*iclereator.reateEe*icle reateEe*icle(); (); 165. ve*icle = ve*iclereator.!etEe*icle ve*iclereator.!etEe*icle(); (); 166. ve*icle. ve*icle.S*ow"n&o S*ow"n&o(); (); 16. 16#. onsole onsole..:eae :eae(); (); 16$. }
1%. }
uilder Pattern Demo - 6utput
;hen to use it@ 1.
7eed to to #r #reate an an o!/ o!/e e#t in in se se"eral st steps a a st step ! ! st step app apprroa# a#h h).
.
The #r #reat atio ion n o% o! o!/e#ts shou shoulld !e ind inde epe pen ndent %r %rom th the wa wa the the o!/ o!/e#t3 t3s s par parts are assem!led.
.
4un unttime #ont ntrrol o"er th the e #reatio ion n pro#ess is re2ui uirred.
E)
!stra#t 5a#tor Design Pattern - C?
!stra#t 5a#tor 5a#tor method pattern %alls %a lls under Creational Pattern o% Fang o% 5our F65) Design Patterns in .7et. $t is used to #reate a set o% related o!/e#ts, or dependent o!/e#ts. $nternall, !stra#t 5a#tor use 5a#tor 5a#tor design pattern %or #reating o!/e#ts. $t ma also use uilder design pattern and prototpe design pattern %or #reating o!/e#ts.
;hat is !stra#t 5a#tor Pattern@ !stra#t 5a#tor 5a#tor patterns a#ts a super-%a#tor whi#h #reates other %a#tories. This pattern is al also so #a #all lled ed as 5a# a#tor tor o% %a %a#t #tor orie ies. s. $n ! !str stra# a#tt 5a# a#tor tor pa patt tter ern n an int inter er%a %a#e #e is responsi!le %or #reating a set o% related o!/e#ts, or dependent o!/e#ts without spe#i%ing their #on#rete #lasses.
!stra#t 5a#tor Pattern - UMA Diagram B $mplementation The UMA #lass diagram di agram %or the implementation implementat ion o% the a!stra#t %a#tor design pattern is gi"en !elow:
The #lasses, inter%a#es and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
!stra#t5a#tor
This is an inter%a#e whi#h is used to #reate a!stra#t produ#t
.
Con#rete5a#tor
This is a #lass whi#h implements the !stra#t5 !stra#t5a#tor a#tor inter%a#e to #reate #on#rete produ#ts.
.
!stra#tProdu#t
This is an inter%a#e whi#h de#lares a tpe o% produ#t.
=.
Con#reteProdu#t
This is a #lass whi#h implements the !stra#tProdu#t inter%a#e to #reate #reate produ#t.
E.
Client
This is a #lass whi#h use !stra#t5 !stra#t5a#tor a#tor and !stra#tProdu#t inter%a#es to #reate a %amil o% related o!/e#ts.
C? - $mplementation Code 1. public inter&ace ?bstract
?bstract-rouct? reate-rouct? reate-rouct?(); ();
4. 5.
?bstract-rouct@ reate-rouct@ reate-rouct@(); ();
6. } . #. public class oncrete
1#. } 1#. } 1$.} 1$. } 2%. 21.public 21. public class oncrete
43. 44.public 44. public class -rouct@2 -rouct@2 ?bstract-rouct@ ?bstract-rouct@ { { } 45. 46.public 46. public class lient 4.{ 4. { 4#. private ?bstract-rouct? ?bstract-rouct? Dprouct?; Dprouct?; 4$. private ?bstract-rouct@ ?bstract-rouct@ Dprouct@; Dprouct@; 5%. 51. public lient lient((?bstract
!stra#t 5a#tor Pattern - 8>ample
;ho is what@ The #lasses, inter%a#es and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows: 1.
!stra#t 5a#tor inter%a#e Ee*icle
.
Fona
.
@ie Scooter - !stra#tProdu#t inter%a#e
=.
:egular @ie Sports @ie :egular Scooter Scoot - Con#reate Produ#ts
E.
Ee*iclelient - Client
C? - ample Code 1. /// 2. /// The 'bstract5actory' 'bstract5actory' interface! 3. /// 4. inter&ace Ee*icle
@ie !et@ie !et@ie((string @ie @ie); );
.
Scooter !etScooter !etScooter((string Scooter Scooter); );
#. } $. 1%. /// /// 11. /// /// The 'oncrete5actory3' 'oncrete5actory3' class! 12. /// /// 13.class 13. class Fona
22. return new :egular@ie :egular@ie(); (); 23. e&ault e&ault 24. t*row new ?pplication>ception ?pplication>ception((string string..ception ?pplication>ception((string string.. 45. /// /// The 'oncrete5actory4' 'oncrete5actory4' class!
46. /// /// 4.class 4. class Feroception ?pplication>ception((string string..
1. e&ault e&ault 2. t*row new ?pplication>ception ?pplication>ception((string string.. $. /// /// The 'bstract,roduct' 'bstract,roduct' interface #%. /// /// #1.inter&ace #1. inter&ace @ie #2.{ #2. { #3. string +a,e +a,e(); (); #4.} #4. } #5. #6. /// /// #. /// /// The 'bstract,roduct2' 'bstract,roduct2' interface ##. /// /// #$.inter&ace #$. inter&ace Scooter $%.{ $%. { $1. string +a,e +a,e(); (); $2.} $2. } $3. $4. /// /// $5. /// /// The ',roduct3' ',roduct3' class
$6. /// /// $.class $. class :egular@ie :egular@ie @ie $#.{ $#. { $$. public string +a,e +a,e() () 1%%. { 1%1. return :egular @ieB +a,e; +a,e; 1%2. } 1%3. } 1%4. 1%5. /// 1%6. /// The ',roduct4' ',roduct4' class 1%. /// 1%#. class Sports@ie Sports@ie @ie 1%$. { 11%. public string +a,e +a,e() () 111. { 112. return Sports @ieB +a,e; +a,e; 113. } 114. } 115. 116. /// 11. /// The ',roduct23' ',roduct23' class 11#. /// 11$. class :egularScooter :egularScooter Scooter 12%. {
121. public string +a,e +a,e() () 122. { 123. return :egular ScooterB +a,e; +a,e; 124. } 125. } 126. 12. /// 12#. /// The ',roduct24' ',roduct24' class 12$. /// 13%. class Scoot Scoot Scooter 131. { 132. public string +a,e +a,e() () 133. { 134. return ScootB +a,e; +a,e; 135. } 136. } 13. 13#. /// 13$. /// The 'lient' class 14%. /// 141. class Ee*iclelient 142. { 143. @ie @ie bie; bie; 144. Scooter Scooter scooter; scooter; 145.
146. public Ee*iclelient Ee*iclelient((Ee*icle 165. /// bstract 5actory 5actory ,attern ,attern Demo 166. /// 16. class -rogra, 16#. { 16$. static voi 7ain 7ain((string string89 89 args) 1%. {
11. Ee*icle
!stra#t 5a#tor Pattern Demo - 6utput
;hen to use it@ 1.
Create a set o% related o!/e#ts, or dependent o!/e#ts whi#h must !e used together.
.
st stem em sh sho oul uld d !e !e #on on+ +gu gurred to to wor wor wi with th mu mult ltip iple le %a %ami mili lies es o% pr prod odu# u#ts ts..
.
The Th e #r #rea eati tion on o% o! o!//e# e#ts ts sh shou ould ld !e in inde depe pend nde ent %r %rom om th the e uti utili liG Gin ing g ss sste tem. m.
=.
Con#rete #lasses should !e !e de#oupled %rom #l #lients.
7ote 1.
$nte $n terrna nall ll, , ! !st stra ra#t #t 5a# a#to tor r us use e 5a# 5a#to tor r de desi sign gn pa patt tter ern n %or %or #r #rea eati ting ng o! o!/e /e#t #ts. s. u utt it it #an also use uilder design pattern and prototpe design pattern %or #reating o!/e#ts. $t #ompletel depends upon our implementation %or #reating o!/e#ts.
.
!st ! stra ra#t #t 5a# a#to tor r #a #an n !e !e use used d as as an an alt alter erna nati ti"e "e to 5a# a#ad ade e to to hid hide e pla plat% t%or ormm-sp spe# e#i+ i+# # #lasses.
.
;hen ;h en ! !st strra# a#tt 5a# 5a#to tor r,, ui uild lder er,, and and Prot otot otp pe e de+ de+ne ne a %a#t %a#tor or %or %or #r #rea eati ting ng th the e o!/e#ts, we should #onsider the %ollowing points : 1.
!st ! stra ra#t #t 5a# a#to tor r us use e the the %a %a# #to tor r %o %orr #r #rea eati ting ng o! o!/e /e#t #ts s o% o% se" se"er eral al #la lass sse es.
.
uilder use use th the %a# %a#tor %or %or #re #reating a #om #omple> o!/ o!/e#t ! us using sim simple o!/e#ts and a step ! step approa#h.
.
Prototpe use use the the %a# %a#tor %or %or !ui !uilding a o!/e#t ! #o #oping an e> e>isting o!/e#t.
H)
5a#tor Method Design Pattern - C?
$n 5a#tor 5a#tor pattern, we #reate o!/e#t without e>posing the #reation logi#. $n this pattern, an inter%a#e is used %or #reating an o!/e#t, !ut let su!#lass de#ide whi#h #lass to instantiate. The #reation o% o % o!/e#t is done when it is re2uired. The 5a#tor 5a#tor method allows a #lass later instantiation to su!#lasses.
;hat is 5a#tor Method Pattern@ $n 5a#tor pattern, we #reate o!/e#t without e>posing the #reation logi#. $n this pattern, an int inter% er%a#e a#e is use used d %or #reatin #reating g an o!/e o!/e#t, #t, !ut let su! su!#las #lass s de# de#ide ide whi#h #la #lass ss to instantiate. The #reation o% o!/e#t is done when it is re2uired. The 5a#tor method allows a #lass later instantiation to su!#lasses.
5a#tor Method Pattern - UMA Diagram B $mplementation The UMA #lass diagram %or the implementation o% the %a#tor method design pattern is gi"en !elow:
The #lasses, inter%a#es and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
Produ#t
This is an inter%a#e %or #reating the o!/e#ts.
.
Con#reteProdu#t
This is a #lass whi#h implements the Produ#t inter%a#e.
.
Creator
This is an a!stra#t #lass and de#lares the %a#tor method, whi#h returns an o!/e#t o% tpe Produ#t.
=.
Con#reteCreator
This is a #lass whi#h implements the Creator #lass #la ss and o"errides the %a#tor %a #tor method to return an instan#e o% a Con#retePr Con#reteProdu#t. odu#t.
C? - $mplementation Code 1. inter&ace -rouct 2. { 3. 4. } 5. 6. class oncrete-rouct? oncrete-rouct? -rouct . { #. } $. 1%.class 1%. class oncrete-rouct@ oncrete-rouct@ -rouct 11.{ 11. { 12.} 12. } 13.
14.abstract 14. abstract class reator 15.{ 15. { 16. public abstract -rouct ception ?rgu,ent>ception(("nvali tpe tpe tpe tpe); ); 2#. } 2#. } 2$. } 2$. } 3%.} 3%. }
5a#tor Method Pattern - 8>ample
;ho is what@ The #lasses, inter%a#es and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows: 1.
"
.
Scooter @ie - Con#reate Produ#t #lasses
.
Ee*icle
=.
oncreteEe*icle
C? - ample Code 1. using Sste, Sste,;; 2. na,espace
5. /// The ',roduct' ',roduct' interface 6. /// .
public inter&ace "
#. { $.
voi Arive Arive((int int ,iles); ,iles);
1%. } 1%. } 11. 12. /// 13. /// 'oncrete,roduct' 'oncrete,roduct' class 14. /// 15. public class Scooter Scooter " 24. /// 'oncrete,roduct' 'oncrete,roduct' class 25. /// 26. public class @ie @ie "
3%. onsole onsole../rite0ine /rite0ine((Arive t*e @ie M M ,iles.oString ,iles.oString() () M , ,); ); 31. } 31. } 32. } 32. } 33. 34. /// 35. /// The reator bstract lass 36. /// 3. public abstract class Ee*icle 44. /// 'oncretereator' class 45. /// 46. public class oncreteEe*icle
55. return new @ie @ie(); (); 56. e&ault e&ault 5. t*row new ?pplication>ception ?pplication>ception((string string.. 64. /// 5actory 5actory ,attern ,attern Demo 65. /// 66. class -rogra, 6. { 6. { 6#. static voi 7ain 7ain((string string89 89 args) 6$. { 6$. { %. Ee*icle
#%. } #%. } #1. } #1. } #2.} #2. }
5a#tor Pattern Demo - 6utput
;hen to use it@ 1.
u!#lasses +gure ou out wh what o! o!/e#ts should !e !e #reated.
.
Par aren entt #las #lass s allo allows ws lat later er ins insta tant ntia iati tion on to to su!# su!#la lass sses es mea means ns th the #r #rea eati tion on o% o% o!/e o!/e#t #t is done when it is re2uired.
.
The Th e pr pro# o#es ess s o% o! o!/e /e#t #ts s #r #rea eati tion on is re2 e2ui uirred to #e #ent ntra rali liGe Ge wi with thin in th the e ap appl pli# i#at atio ion. n.
=.
#la #lass ss #r #rea eato tor) r) wi will ll no nott no now w wha whatt #la #las sse ses s it it wil willl !e !e re re2u 2uir ired ed to #rea eate te..
')
ridge Design Pattern - C?
ridge pattern %alls under tru#tural Pattern o% Fang o% 5our F65) Design Patterns in .7et. ll we now, $nheritan#e is a wa to spe#i% dierent implementations o% an a!stra#tion. ut in this wa, implementations are tightl !ound to the a!stra#tion and #an not !e modi+ed independentl ind ependentl..
;hat is ridge Pattern ridge pattern pattern is used to separ separate ate an a!stra# a!stra#tion tion %rom its impleme implementation ntation so that !oth #an !e modi+ed independentl. independentl. This pattern p attern in"ol"es an inter%a#e i nter%a#e whi#h a#ts as a !ridge !etween the a!stra#tion #lass and an d imp imple leme mente nterr #l #las asse ses s an and d als also o ma maes es the %u %un# n#tio tional nalit it o% im imple pleme ment nter er #l #las ass s
independent %rom the a!stra#tion #lass. oth tpes o% #lasses #an !e modi+ed without ae#ting to ea#h other.
ridge Pattern - UMA Diagram B $mplementation The UMA #lass diagram %or the implementation o% the !ridge design pattern is gi"en !elow:
The #lasses, inter%a#es and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
!stra#tion
This is an a!stra#t #lass and #ontaining mem!ers that de+ne an a!stra#t !usiness o!/e#t and its %un#tionalit. $t #ontains a re%eren#e to an o!/e#t o% tpe ridge. $t #an also a#ts as the !ase #lass %or other a!stra#tions.
.
4ede+ned !stra#tion
This is a #lass whi#h inherits %rom the !stra#tion #lass. $t e>tends the inter%a#e de+ned ! !stra#tion #lass.
.
ridge
This is an inter%a#e whi#h a#ts as a !ridge !etween the a!stra#tion #lass and impl im plem emen ente terr
#las #l asse ses s
and an d
also al so
ma ma es
the th e
%un# %u n#ti tion onal alit it
o%
impl im plem emen ente terr
#las #l ass s
independent %rom the a!stra#tion #lass.
=.
$mplementation B $mplementation
These are #lasses whi#h implement the ridge inter%a#e and also pro"ide the implementation details %or the asso#iated !stra#tion #lass.
C? - $mplementation Code 1. public abstract class ?bstraction 2. { 3.
public @rige ",ple,enter ",ple,enter { { get get;; set set;; }
4. 5.
public virtual voi Nperation Nperation() ()
6. { .
onsole../rite0ine onsole /rite0ine((",ple,entation@aseNperation() ",ple,entation@aseNperation()); );
#.
",ple,enter..Nperation",ple,entation ",ple,enter Nperation",ple,entation(); ();
$. } 1%.} 1%. } 11. 12.public 12. public class :eOne?bstraction :eOne?bstraction ?bstraction 13.{ 13. { 14. public overrie voi Nperation Nperation() () 15. { 15. { 16. onsole onsole../rite0ine /rite0ine((:eOne?bstractionNperation() :eOne?bstractionNperation()); ); 1. ",ple,enter ",ple,enter..Nperation",ple,entation Nperation",ple,entation(); (); 1#. } 1#. }
1$.} 1$. } 2%. 21.public 21. public inter&ace @rige 22.{ 22. { 23. voi Nperation",ple,entation Nperation",ple,entation(); (); 24.} 24. } 25. 26.public 26. public class ",ple,entation? ",ple,entation? @rige 2.{ 2. { 2#. public voi Nperation",ple,entation Nperation",ple,entation() () 2$. { 2$. { 3%. onsole onsole../rite0ine /rite0ine((",ple,entation?Nperation",ple,entation() ",ple,entation?Nperation",ple,entation()); ); 31. } 31. } 32.} 32. } 33. 34.public 34. public class ",ple,entation@ ",ple,entation@ @rige 35.{ 35. { 36. public voi Nperation",ple,entation Nperation",ple,entation() () 3. { 3. { 3#. onsole onsole../rite0ine /rite0ine((",ple,entation@Nperation",ple,entation() ",ple,entation@Nperation",ple,entation()); ); 3$. } 3$. } 4%.} 4%. }
ridge Pattern - 8>ample
;ho is what@ The #lasses, inter%a#es and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows: 1.
7essage - !stra#tion Class.
.
Sste,7essage Pser7essage- 4ede+ned !stra#tion Classes.
.
"7essageSener - ridge $nter%a#e.
=.
,ailSener /ebServiceSener 7S7Q Sener - Con#rete$mplementation #lass whi#h implements the $Messageender inter%a#e.
C? - ample Code 1. /// 2. /// The 'bstraction' class
3. /// 4. public abstract class 7essage 5. { 6.
public "7essageSener 7essageSener 7essageSener { { get get;; set set;; }
.
public string Sub'ect Sub'ect { { get get;; set set;; }
#.
public string @o @o { { get get;; set set;; }
$.
public abstract voi Sen Sen(); ();
1%.} 1%. } 11. 12. /// /// 13. /// /// The '&e6nedbstraction' '&e6nedbstraction' class 14. /// /// 15.public 15. public class Sste,7essage Sste,7essage 7essage 16.{ 16. { 1. public overrie voi Sen Sen() () 1#. { 1#. { 1$. 7essageSener 7essageSener..Sen7essage Sen7essage((Sub'ect Sub'ect @o @o); ); 2%. } 2%. } 21.} 21. } 22. 23. /// /// 24. /// /// The '&e6nedbstraction' '&e6nedbstraction' class 25. /// /// 26.public 26. public class Pser7essage Pser7essage 7essage 2.{ 2. {
2#. public string Psero,,ents Psero,,ents { { get get;; set set;; } 2$. 3%. public overrie voi Sen Sen() () 31. { 31. { 32.
string &u &ull ll@ @o = Psero,,ents); Psero,,ents );
string..
33. 7essageSener 7essageSener..Sen7essage Sen7essage((Sub'ect Sub'ect &ull@o); 34. } 34. } 35.} 35. } 36. 3. /// /// 3#. /// /// The '2ridge/0m*lementor' interface 3$. /// /// 4%.public 4%. public inter&ace "7essageSener 41.{ 41. { 42. voi Sen7essage Sen7essage((string string sub'ect sub'ect string string bo); bo); 43.} 43. } 44. 45. /// /// 46. /// /// The 'oncrete0m*lementor' class 4. /// /// 4#.public 4#. public class ,ailSener ,ailSener "7essageSener 4$.{ 4$. { 5%. public voi Sen7essage Sen7essage((string string sub'ect sub'ect string string bo) bo) 51. { 51. { 52. onsole onsole../rite0ine /rite0ine((,ailJn{%}Jn{1}Jn ,ailJn{%}Jn{1}Jn sub'ect bo);
@o @o
53. } 53. } 54.} 54. } 55. 56. /// /// 5. /// /// The 'oncrete0m*lementor' class 5#. /// /// 5$.public 5$. public class 7S7QSener 7S7QSener "7essageSener 6%.{ 6%. { 61. public voi Sen7essage Sen7essage((string string sub'ect sub'ect string string bo) bo) 62. { 62. { 63. onsole onsole../rite0ine /rite0ine((7S7QJn{%}Jn{1}Jn 7S7QJn{%}Jn{1}Jn sub'ect bo); 64. } 64. } 65.} 65. } 66. 6. /// /// 6#. /// /// The 'oncrete0m*lementor' class 6$. /// /// %.public %. public class /ebServiceSener /ebServiceSener "7essageSener 1.{ 1. { 2. public voi Sen7essage Sen7essage((string string sub'ect sub'ect string string bo) bo) 3. { 3. { 4. onsole onsole../rite0ine /rite0ine((/eb ServiceJn{%}Jn{1} ServiceJn{%}Jn{1}Jn Jn sub'ect bo); 5. } 5. } 6.} 6. } .
#. /// /// $. /// /// 2ridge Design ,attern ,attern Demo #%. /// /// #1.class #1. class -rogra, #2.{ #2. { #3. static voi 7ain 7ain((string string89 89 args) #4. { #4. { #5. "7essageSener "7essageSener e,ail e,ail = new ,ailSener ,ailSener(); (); #6. "7essageSener "7essageSener Rueue Rueue = new 7S7QSener 7S7QSener(); (); #. "7essageSener "7essageSener web web = new /ebServiceSener /ebServiceSener(); (); ##. #$. 7essage 7essage ,essage ,essage = new Sste,7essage Sste,7essage(); (); $%. ,essage. $%. ,essage.Sub'ect Sub'ect = = est 7essage; 7essage; $1. ,essage. $1. ,essage.@o @o = = Fi *is is a est 7essage; 7essage; $2. $3. ,essage. $3. ,essage.7essageSener 7essageSener = = e,ail; $4. ,essage. $4. ,essage.Sen Sen(); (); $5. $6. ,essage. $6. ,essage.7essageSener 7essageSener = = Rueue; $. ,essage. $. ,essage.Sen Sen(); (); $#. $$. ,essage. $$. ,essage.7essageSener 7essageSener = = web; 1%%. ,essage. ,essage.Sen Sen(); (); 1%1. 1%2. Pser7essage Pser7essage user,sg user,sg = new Pser7essage Pser7essage(); ();
1%3. user,sg. user,sg.Sub'ect Sub'ect = = est 7essage; 7essage; 1%4. user,sg. user,sg.@o @o = = Fi *is is a est 7essage; 7essage; 1%5. user,sg. user,sg.Psero,,ents Psero,,ents = = " *ope ou are well; well; 1%6. 1%. user,sg. user,sg.7essageSener 7essageSener = = e,ail; 1%#. user,sg. user,sg.Sen Sen(); (); 1%$. 11%. onsole onsole..:eae :eae(); (); 111. } 112. }
ridge Pattern Demo - 6utput
;hen to use it@ 1.
!st ! stra ra#t #tio ions ns an and d im impl ple eme ment ntat atio ions ns sh sho oul uld d !e mo modi di+ +ed in inde depe pend nden entl tl .
.
Changes in th the imp implementation o% an an a!s a!stra#tion sho should ha" ha"e no im impa#t on #lients.
.
The Th e r rid idge ge pa patt tter ern n is is use used d whe when n a ne new w "er "ersi sion on o% a so% so%tw twar are e or or s sst stem em is !r !rou ough ghtt out, !ut the older "ersion o% the so%tware still running %or its e>isting #lient. There is no need to #hange the #lient #ode, !ut the #lient need to #hoose whi#h "ersion he wants to use.
7ote ridge pattern has nearl the same stru#ture as the dapter Pattern. ut it is used when designing new sstems instead o% the dapter pattern whi#h is used with alread e>isting sstems.
I)
dapter Design Pattern - C?
dapter pattern %alls under tru#tural tru#tural Pattern o% Fang o% 5our F65) Design Patterns in .7et. The dapter pattern allows a sstem to use #lasses o% another sstem that is in#ompati!le with it. $t is espe#iall used %or toolits and li!raries. $n this arti#le, $ would lie share what is adapter pattern and how is it wor@
;hat is dapter Pattern dapter pattern a#ts as a !ridge !etween two in#ompati!le inter%a#es. This pattern in"ol"es a single #lass #alled adapter whi#h is responsi!le %or #ommuni#ation !etween two independent or in#ompati!le inter%a#es.
a,ple #ard reader a#ts as an adapter !etween memor #ard and a laptop. Jou J ou plugins the memor #ard into i nto #ard reader and #ard reader into t he laptop so that memor #ard #an !e read "ia laptop.
dapter Pattern - UMA Diagram B $mplementation The UMA #lass diagram %or the implementation o% the dapter design pattern is gi"en !elow:
The #lasses, inter%a#es and o!/e#ts in the a!o"e UMA #lass diagram are as %ollows:
1.
$Target
This is an inter%a#e whi#h is used ! the #lient to a#hie"e its %un#tionalitKre2uest.
.
dapter
This is a #lass whi#h implements the $Target $Target inter%a#e and an d inherits the daptee #lass. $t is responsi!le %or #ommuni#ation !etween Client and daptee.
.
daptee
This is a #lass whi#h ha"e the %un#tionalit, re2uired ! the #lient. Lowe"er, its inter%a#e is not #ompati!le with the #lient.
=.
Client
This is a #lass whi#h intera#t with a tpe that implements the $T $Target arget inter%a#e. Lowe"er, the #ommuni#ation #lass #alled adaptee, is not #ompati!le with the #lient
C? - $mplementation Code 1. public class lient 2. {
3.
private "arget "arget target; target;
4. 5.
public lient lient(("arget "arget target) target)
6. { .
t*is.target t*is .target = target;
#. } $. 1%. public voi 7ae:eRuest 7ae:eRuest() () 11. { 11. { 12. target. 12. target.7et*o? 7et*o?(); (); 13. } 13. } 14.} 14. } 15. 16.public 16. public inter&ace "arget 1.{ 1. { 1#. voi 7et*o? 7et*o?(); (); 1$.} 1$. } 2%. 21.public 21. public class ?apter ?apter ?aptee ?aptee "arget 22.{ 22. { 23. public voi 7et*o? 7et*o?() () 24. { 24. { 25. 7et*o@ 7et*o@(); (); 26. } 26. } 2.} 2. }
2#. 2$.public 2$. public class ?aptee 3%.{ 3%. { 31. public voi 7et*o@ 7et*o@() () 32. { 32. { 33. onsole onsole../rite0ine /rite0ine((7et*o@() is calle); calle); 34. } 34. } 35.} 35. }
dapter Pattern - 8>ample
;ho is what@ The #lasses, inter%a#es and o!/e#ts in the a!o"e #lass diagram #an !e identi+ed as %ollows: 1.
"raget - Target inter%a#e
.
,ploee ?apter- dapter Class
.
F: Sste,- daptee Class
=.
*ir-art@illingSste, - Client
C? - ample Code 1. /// 2. /// The 'lient' class 3. /// 4. public class *ir-art@illingSste, 5. { 6.
private "arget "arget e,ploeeSource; e,ploeeSource;
. #.
public *ir-art@illingSste, *ir-art@illingSste,(("arget "arget e,ploeeSource) e,ploeeSource)
$. { 1%. t*is t*is.e,ploeeSource .e,ploeeSource = e,ploeeSource; 11. } 11. } 12. 13. public voi S*ow,ploee0ist S*ow,ploee0ist() () 14. { 14. { 15. 0ist 0istHstringI HstringI e,ploee e,ploee = e,ploeeSource.!et,ploee0ist e,ploeeSource.!et,ploee0ist(); (); 16. //T //To o D$% 0m*lement you business logic logic 1. 1#. onsole onsole../rite0ine /rite0ine((CCCCCCCCC ,ploee 0ist CCCCCCCCCC CCCCCCCCCC); ); 1$. &oreac* &oreac* ( (var var ite, ite, in in e,ploee) e,ploee) 2%. { 2%. { 21. onsole onsole../rite /rite(ite,); (ite,); 22. } 22. } 23. 24. } 24. }
25.} 25. } 26. 2. /// /// 2#. /// /// The '0Target' '0Target' interface 2$. /// /// 3%.public 3%. public inter&ace "arget 31.{ 31. { 32. 0ist 0istHstringI HstringI !et,ploee0ist !et,ploee0ist(); (); 33.} 33. } 34. 35. /// /// 36. /// /// The 'da*tee' class 3. /// /// 3#.public 3#. public class F:Sste, 3$.{ 3$. { 4%. public string string8989 8989 !et,ploees !et,ploees() () 41. { 41. { 42. string string8989 8989 e,ploees = new string string884989; 43. 44. e,ploees8 44. e,ploees8% %9 = new string string89 89 { 1%% 1%% Aeepa Aeepa ea, ea, 0eaer 0e aer }; }; 45. e,ploees8 45. e,ploees81 19 = new string string89 89 { 1%1 1%1 :o*it :o*it Aeveloper Aeveloper }; }; 46. e,ploees8 46. e,ploees82 29 = new string string89 89 { 1%2 1%2 !auta, !auta, Aeveloper Aeveloper }; }; 4. e,ploees8 4. e,ploees83 39 = new string string89 89 { 1%3 1%3 Aev Aev ester ester }; }; 4#. 4$. return return e,ploees; e,ploees;
5%. } 5%. } 51.} 51. } 52. 53. /// /// 54. /// /// The 'da*ter' class class 55. /// /// 56.public 56. public class ,ploee?apter ,ploee?apter F:Sste, F:Sste, "arget 5.{ 5. { 5#. public 0ist 0istHstringI HstringI !et,ploee0ist !et,ploee0ist() () 5$. { 5$. { 6%. 0ist 0istHstringI HstringI e,ploee0ist e,ploee0ist = new 0ist 0istHstringI HstringI(); (); 61. string string8989 8989 e,ploees = !et,ploees !et,ploees(); (); 62. &oreac* &oreac* ( (string string89 89 e,ploee in in e,ploees) e,ploees) 63. { 63. { 64. e,ploee0ist. 64. e,ploee0ist.? ?(e,ploee8 (e,ploee8% %9); 65. e,ploee0ist. 65. e,ploee0ist.? ?(( ); ); 66. e,ploee0ist. 66. e,ploee0ist.? ?(e,ploee8 (e,ploee81 19); 6. e,ploee0ist. 6. e,ploee0ist.? ?(( ); ); 6#. e,ploee0ist. 6#. e,ploee0ist.? ?(e,ploee8 (e,ploee82 29); 6$. e,ploee0ist. 6$. e,ploee0ist.? ?((Jn Jn); ); %. } %. } 1. 2. return return e,ploee0ist; e,ploee0ist; 3. } 3. } 4.} 4. }
5. 6. /// /// . /// /// da*ter Design ,attern ,attern Demo #. /// /// $.class $. class -rogra, #%.{ #%. { #1. static voi 7ain 7ain((string string89 89 args) #2. { #2. { #3. "arget "target "target = = new ,ploee?apter ,ploee?apter(); (); #4. *ir-art@illingSste, *ir-art@illingSste, client client = new *ir-art@illingSste, *ir-art@illingSste,(("target "target); ); #5. client. #5. client.S*ow,ploee0ist S*ow,ploee0ist(); (); #6. #. onsole onsole..:eae :eae(); (); ##. #$. } #$. } $%.} $%. }
dapter Pattern Demo - 6utput
;hen to use it@ 1.
llo l low w a s sst ste em to to use use #l #las asse ses s o% o% ano anoth ther er s sst stem em tha hatt is is in# in#om ompa pati ti!l !le e wit with h it. it.
.
llow #ommuni#ation !etween new and alread e>isting sstem whi#h are independent to ea#h other
.
do do..7e 7ett 2l 2lda dapt pter er,, 6ra# 6ra#le le da dapt pter er,, M2 M2l lda dapt pter er ar are !est !est e>a e>amp mple le o% o% da dapt pte er Pattern.
7ote 1.
$nte $n terrna nall ll, , da dapt pter er us use e 5a# 5a#to tor r de desi sign gn pa patt tter ern n %or %or #r #rea eati ting ng o! o!/e /e#t #ts. s. u utt it it #an #an al also so use ui uilder lder design patt patter ern n and pr protot ototpe pe des design ign pat patter tern n %or #r #reat eating ing pr produ# odu#t. t. $t #ompletel depends upon our implementation %or #reating produ#ts.
.
dap d apte terr #a #an n !e !e us used ed as an al alte terrna nati ti"e "e to 5a# a#ad ade e to hi hide de pl plat at%o %orrmm-sp spe# e#i+ i+# # #la #lass sses es..
.
;hen ;h en da dapt pter er,, ui uild lder er,, and and Pr Prot otot ot pe de+ de+n ne a %a# %a#to tor r %or %or #r #rea eati ting ng the the pr prod odu# u#tts, we should #onsider the %ollowing points : 1.
dap aptter us use th the %a %a#tor %o %or #r #reat atin ing g o! o!/e#ts o% o% se se"eral #lass sse es.
.
uilder use use the the %a# %a#tor %or %or #re #reating a #omple> pro produ#t ! us using sim simple o!/e#ts and a step ! step approa#h.
.
Prototpe use use the the %a# %a#tor %or %or !ui !uilding a produ#t ! #o #oping an e>i e>isting produ#t.