Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Esunpatrndeconductaqueseutilizaparamanejaralgoritmosyresponsabilidadesentre objetosmediantelaconstruccindeunlenguajeparainterpretarcosassobreotrolenguajede programacin.SegnGangofFour: Dadounlenguaje,definirlarepresentacindesugramticajuntoconsuintrpretequeutilizar lamismarepresentacinpara,valgalaredundancia,interpretarenunciados. Lautilidaddeestepatrn,aunquenomuyconocido,sedacuandoexisteunrboldesintaxis quepuederepresentarunagramtica.Seutilizaentraductores,buscadoreselegantes,anlisis depatronesentextosyenelusodecontenidosemnticoenInternet. Contexto:Informacinglobalparatodoelintrprete. Expresinabstracta:Interfazdeinterpretacin. Expresinconcreta:Implementalainterfaz,pudiendoserterminalono.
Ejemplo:
1 3 . { 1 4 . A r r a y L i s t < B o o k >b o o k s=c o n t e x t . g e t A l l B o o k s ( ) ; 1 5 . S t r i n g B u f f e rr e s u l t=n e wS t r i n g B u f f e r ( ) ; 1 6 . f o r ( B o o kb o o k :b o o k s ) 1 7 . { 1 8 . i f ( b o o k . g e t A u t h o r ( ) . e q u a l s I g n o r e C a s e ( s e a r c h S t r i n g ) ) 1 9 . { 2 0 . r e s u l t . a p p e n d ( b o o k . t o S t r i n g ( ) ) ; 2 1 . } 2 2 . } 2 3 . r e t u r nr e s u l t ; 2 4 . 2 5 . } 2 6 . 2 7 . } 0 1 . / / c l i e n t 0 2 . p u b l i cc l a s sA m a z o n C l i e n t 0 3 . { 0 4 . 0 5 . p r i v a t eI n t e r p r e t e r C o n t e x tc o n t e x t ; 0 6 . 0 7 . p u b l i cA m a z o n C l i e n t ( I n t e r p r e t e r C o n t e x tc o n t e x t ) 0 8 . { 0 9 . t h i s . c o n t e x t=c o n t e x t ; 1 0 . } 1 1 . 1 2 . / * * 1 3 . *I n t e r p r e t sas t r i n gi n p u to ft h ef o r m 1 4 . * m o v i e s|b o o k sb yt i t l e|y e a r|n a m e' < s t r i n g > ' 1 5 . * / 1 6 . p u b l i cS t r i n gi n t e r p r e t ( S t r i n ge x p r e s s i o n ) 1 7 . { 1 8 . / / w en e e dt op a r s et h es t r i n gt od e t e r m i n ew h i c he x p r e s s i o nt ou s e 1 9 . 2 0 . A b s t r a c t E x p r e s s i o ne x p=n u l l ; 2 1 . 2 2 . S t r i n g [ ]s t r i n g P a r t s=e x p r e s s i o n . s p l i t ( "" ) ; 2 3 . 2 4 . S t r i n gm a i n=s t r i n g P a r t s [ 0 ] ; 2 5 . S t r i n gs u b=s t r i n g P a r t s [ 2 ] ; 2 6 . 2 7 . / / g e tt h eq u e r yp a r t
2 8 . S t r i n gq u e r y=e x p r e s s i o n . s u b s t r i n g ( e x p r e s s i o n . f i r s t I n d e x O f ( " ' " ) , e x p r e s s i o n . l a s t I n d e x O f ( " ' " ) ) ; 2 9 . 3 0 . 3 1 . i f ( m a i n . e q u a l s ( " b o o k s " ) ) 3 2 . { 3 3 . i f ( s u b . e q u a l s ( " t i t l e " ) 3 4 . { 3 5 . e x p=n e wB o o k T i t l e E x p r e s s i o n ( q u e r y ) ; 3 6 . } 3 7 . i f ( s u b . e q u a l s ( " y e a r " ) 3 8 . { 3 9 . e x p=n e wB o o k Y e a r E x p r e s s i o n ( q u e r y ) ; 4 0 . } 4 1 . } 4 2 . e l s e i f ( m a i n . e q u a l s ( " m o v i e " ) ) 4 3 . { 4 4 . / / s i m i l a rs t a t e m e n t st oc r e a t em o v i ee x p r e s s i o n s 4 5 . } 4 6 . 4 7 . 4 8 . i f ( e x p! =n u l l ) 4 9 . { 5 0 . e x p . i n t e r p r e t ( c o n t e x t ) ; 5 1 . } 5 2 . 5 3 . 5 4 . } 5 5 . 5 6 . 5 7 . p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) 5 8 . { 5 9 . I n t e r p r e t e r C o n t e x tc o n t e x t = n e w I n t e r p r e t e r C o n t e x t ( " h t t p : / / a w s . a m a z o n . c o m / " ) ; 6 0 . A m a z o n C l i e n tc l i e n t=n e wA m a z o n C l i e n t ( ) ; 6 1 . 6 2 . / / r u naq u e r y 6 3 . S t r i n gr e s u l t=c l i e n t . i n t e r p r e t ( " b o o k sb ya u t h o r' J o h nC o n n o l l y ' " ) ; 6 4 . 6 5 . S y s t e m . o u t . p r i n t l n ( r e s u l t ) ; 6 6 . } 6 7 .
Patrn:Comando
Estepatrnpermitedesacoplarunaaccindelobjetoquelaejecuta,efectivamente encapsulandoalcomando.As,pues,elpatrnpermiteelenvodelacpsuladelaaccinaun mduloespecficoqueconocesupropiafuncindeaccin.SegnGangofFour: Encapsularunapeticincomoobjeto,permitiendoasparametrizaralosclientescon peticionesdistintasydandosoporteparaaccionesdeshacer. ElpatrnComandoestilcuandosetieneunacoladepeticiones,cuandolaspeticionesdeben manejarseenordenytiemposvariantes,cuandoserequierepoderdeshaceroperaciones mltiples(enunacola).Unejemplosonloscomandosdeinterfacesgrficasyloscomandosde men.
propondrunretoparaelmantenimiento,sobretododeuncontroladorcentral.
Ejemplo: