You are on page 1of 5

29/10/13

Design Pattern – Strategy | uaiHebert

(:

Português

English

Studied books

About Me

Contact Me

[Curriculum Vitae]

Design Pattern – Strategy
December 9, 2010 2:15 pm | Leave a Comment | uaihebert

SEARCH
Search

Hello, how are you? I will post here some design patterns of the study I am doing about this subject. The base book to this study will be Head First – Design Patterns. I will describe in here the same patterns that we can find in the book, but I will be using different samples and adding more material about this subject as soon it is possible. Let us use this user case: “An internet site that sells toys cars is receiving a lot of customer visitors every day and becoming more famous. Its investors want more; they want that the site provides to the customers the simulations of the toy car actions.” Check below our class model (there are others sub classes of ToyCar that are not described below):

Enter your email address:

Subscribe Delivered by FeedBurner (Google)

Subscribe via RSS

Meu livro JSF (Portuguese Only)

CATEGORIES
Business English (25) Business Português (25) Certificação Português (1) The investors want that the car perform the action of starting the engine, to the customers may hear the engine sound. To accomplish this, the solution that it is “ the most useful, fastest and quickest ” may be to apply some changing in the super class. What of bad could come of using this solution that the Java gives us? Any change in the super class ToyCar will be reflected to all sub classes, and all sub classes will have the same behavior! We just have to create a method to start the engine (“startEngine()”) and our problem will be solved: Certification English (1) Clean Code [English] (5) Código Limpo [Português] (5) Design Pattern – English (3) » Design Pattern - Observer (Part 01) » Design P at t ern - St rat egy » Design Pattern – Observer (Part 02) Design Pattern – Português (3) English (58) OO Paterns [English] (2) OO Paterns [Português] (2) Português (58) » Aplicação Web Completa JSF EJB JPA JAAS The team was very happy with the result of this new functionality that was delivered on time. Now the customers can hear the engine sound. BUT, after a phone call it was possible to see that something bad happened. One of the investors saw something in our application that should never happen, a wood car starting its engine. Why this happened? Let us see our model: » Aplicação Web Completa Tomcat JSF Primefaces JPA Hibernate » Autenticação de Usuários (Filter/Servlet) » Auto Create Schema Script com: Ant, Hibernate 3 e JPA 2 » Cobrindo seus testes com Cobertura, JUnit, HSQLDB, JPA » Código Limpo - Parte 04

uaihebert.com/?p=26

1/5

our class will not care with which behavior the interface method has.Novas funcionalidades » EasyCriteria no Maven » JPA @ManyToMany Unidirecional e Bidirecional » JPA @OneToMany e @ManyToOne Unidirecional e Bidirecional » JPA @OneToOne Unidirecional e Bidirecional » JPA Consultas e Dicas » JPA Mapeando Datas (Date) e Enum » JPA Mapeando Duas Tabelas em uma Classe » JPA Mini Livro . Let us create an interface to handle this engine behavior: uaihebert. So.Primeiros passos e conceitos detalhados » JPA SequenceGenerator » JPA TableGenerator . Instead of letting our class ToyCar handling this behavior. which solution would be the best one here? Let us apply the first pattern principle of this study: “ I dent ify ev ery t hing t hat changes and isolat e it from what does not change. Imagine if we have an interface with a visible method.0 . não pergunte! » EasyCriteria .Parte 05 » Código Limpo – Parte 01 » Código Limpo – Parte 02 » Código Limpo – Parte 03 » Como agendar sua certificação Java Oracle » Como testar sua JPQL / HQL sem realizar Deploy » Criando um WebServer » Design Pattern .0 » Lançado meu primeiro livro: JSF Eficaz » Lei de Demeter » O “Pai do Java” usa Jelastic » Obrigado DZone Would the above code be the best solution? Let us suppose that we have a sub class of ToyCar. that also has ToyCar as super class.29/10/13 Design Pattern – Strategy | uaiHebert (: » Código Limpo . And each time. conceitos e boas práticas » JSF Parametros por Get Request RESTFull » JSF: Converter e Bean Auto Complete » JUDCon 2013 Brasil » JUnit com HSQLDB. JPA e Hibernate » Lançado EasyCriteria 1.0 .com/?p=26 2/5 .Chave Primária Simples » JPA Uma Tabela por Classe » JPA Uma tabela por herança » JPA: Ordernando listas e utilizando Map como atributo mapeado » JSF .Strategy » Diga. a metal car (“MetalCar”) where we would have to override the method too. What could we do to solve this problem? We could just override the method: 1 2 3 4 @ O v e r r i d e < f o n t > < / f o n t > p u b l i cb o o l e a ns t a r t E n g i n e ( ){ < f o n t > < / f o n t > / /D on o t h i n g < f o n t > < / f o n t > } » EasyCriteria . By doing this method overriding in all classes methods we are increasing our project complexity.Observer (Parte 01) » Design Pattern . we would have to override this method also.Novas funcionalidades Our wood car. we could isolate this behavior from our ToyCar class.Utilizando a Criteria do JPA de um modo simples » EasyCriteria 2.Observer (Parte 02) » Design Pattern . that a new ToyCar sub class is created and this sub class should not have the method startEngine() implemented. received the method to start the engine when a wood car does not have an engine. why do not delegate it? Here it come our second pattern principle: “ P rogram t o an int erface” This principle is nothing more than: use all benefits from super class.Hello World. AutoComplete » JSF Exemplos Simples com Ajax » JSF Exibindo Objeto e Mensagens após Redirect » JSF Mini Livro .” What is the meaning of this? Think about an engine behavior that will be present in one class and other different engine behavior that should be present in another class.Dicas.JPA Criteria tem que ser simples » EasyCriteria 3.

< f o n t > < / f o n t > p r i v a t ei n tm a x S p e e d . p r i n t l n ( " N oe n g i n et ot u r no n " ) . s t a r t E n g i n e ( ) .Primeiros passos » TDD Cobertura de testes com JUnit Ant e Emma » Tratando Exceções em uma Aplicação Web » Tutorial Hibernate 3 com JPA 2 » Tutorial JPA Chave Primária Composta » uaiHebert no JUDCON Brasil 2013 » Validação de Login de Usuário com JSF e JAAS TDD [English] (4) TDD [Português] (4) Web App [English] (14) Web App [Português] (14) ARCHIVES Bellow you can check how our classes will be implemented. < f o n t > < / f o n t > } < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > / /O u rC l a s s e sI m p l m e n t a t i o i n s < f o n t > < / f o n t > p u b l i cc l a s sT o y C a r{ < f o n t > < / f o n t > p r i v a t ei n td o o r T o t a l . o u t .com/?p=26 3/5 . First the class ToyCar.Minhas Palestras » TDD . < f o n t > < / f o n t > / /O t h e r sa t t r i b u t e s < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cT o y C a r ( ) { < f o n t > < / f o n t > e n g i n e B e h a v i o r=n e wR e g u l a r E n g i n e ( ) . then Mercedes and WoodCar: 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 4 7 4 8 4 9 5 0 5 1 5 2 / /I n t e r f a c ea n dE n g i n eB e h a v i o ri m p l e m e n t a t i o n s < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i ci n t e r f a c eE n g i n e B e h a v i o r{ < f o n t > < / f o n t > p u b l i cb o o l e a ns t a r t E n g i n e ( ) . < f o n t > < / f o n t > r e t u r nt r u e .29/10/13 Design Pattern – Strategy | uaiHebert (: » Paginação de Datatable Lazy com JSF (Primefaces) » Quatro soluções para LazyInitializationException » Review do Livro: Instant PrimeFaces Starter » TDC 2013 . o u t . < f o n t > < / f o n t > p r o t e c t e dE n g i n e B e h a v i o re n g i n e B e h a v i o r . o u t . < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cv o i ds t a r t E n g i n e ( ) { < f o n t > < / f o n t > e n g i n e B e h a v i o r . < f o n t > < / f o n t > r e t u r nt r u e . p r i n t l n ( " P o w e r f u l lE n g i n eO n " ) . < f o n t > < / f o n t > } < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cc l a s sP o w e r f u l E n g i n e B e h a v i o ri m p l e m e n t sE n g i n e B e h a v i o r{ < f o n t > < / f o n t > < f o n t > < / f o n t > @ O v e r r i d e < f o n t > < / f o n t > p u b l i cb o o l e a ns t a r t E n g i n e ( ){ < f o n t > < / f o n t > S y s t e m . < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > / /O t h e r sm e t h o d s < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cc l a s sM e r c e d e se x t e n d sT o y C a r{ < f o n t > < / f o n t > [+] 2013 (20) [+] 2012 (24) [+] 2011 (46) [+] 2010 (26) UNIQUE VISITORS uaihebert. < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cc l a s sN o E n g i n e B e h a v i o ri m p l e m e n t sE n g i n e B e h a v i o r{ < f o n t > < / f o n t > < f o n t > < / f o n t > @ O v e r r i d e < f o n t > < / f o n t > p u b l i cb o o l e a ns t a r t E n g i n e ( ){ < f o n t > < / f o n t > S y s t e m . < f o n t > < / f o n t > r e t u r nt r u e . < f o n t > < / f o n t > } < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cc l a s sR e g u l a r E n g i n e B e h a v i o ri m p l e m e n t sE n g i n e B e h a v i o r{ < f o n t > < / f o n t > < f o n t > < / f o n t > @ O v e r r i d e < f o n t > < / f o n t > p u b l i cb o o l e a ns t a r t E n g i n e ( ){ < f o n t > < / f o n t > S y s t e m . p r i n t l n ( " E n g i n eO n " ) .

You must decide the best type for your system and implement it. All classes that do not have an engine behavior would be with a correct functioning. In the book Head First – Design Patterns the authors make this explanation very clear. If you notice how our classes are implemented now (WoodCar. even if I do comment the method startEngine() or put the code engineBehavior = new NoEngineBehavior(). Using composition in our system will make our job easier. You should always program to a super type like a regular Class (POJO). See you soon. In the user case that we simulated today this behavior is like our noun. we would have to change only the method startEngine() in our class NoEngineBehavior. < f o n t > < / f o n t > } < f o n t > < / f o n t > } (: I know that we could change the way that our interface is receiving its implementation (“ engineBehavior = new NoEngineBehavior(). Posted in: Design Pattern . Give priority to composition rather than inheritance. I still have “ duplicated code” in my system!” Think with me about this next investor requirement: “When we call the method startEngine() in a type of ToyCar that does not have an engine. s t a r t E n g i n e ( ) . we would have to register this action in a log”.com/?p=26 4/5 . If you have any questions or ideas to share just leave your comment. we used an interface to create the composition. but this refactoring is subject to another posts. but it means always program to a super type. Things like: “Car”. “Computer”. Final considerations: The class name should indicate a noun. Design Pattern: Strategy Studied Principles: Identify everything that changes and isolate it from what does not change. Program to an interface – this principle does not necessarily means that we have to create an Interface.English. s t a r t E n g i n e ( ) . Program to an interface. < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > @ O v e r r i d e < f o n t > < / f o n t > p u b l i cv o i ds t a r t E n g i n e ( ) { < f o n t > < / f o n t > e n g i n e B e h a v i o r . etc. Abstract Class and Interface. It is a class that handles a behavior pattern. You may think: “Wait a minute. Do not mistake this principle that says: “always code for a super type” with “always create an interface”. As we saw in our user case today. English uaihebert. We just studied another project Principle: “Give priority to composition rather than inheritance”. “House”.29/10/13 5 3 5 4 5 5 5 6 5 7 5 8 5 9 6 0 6 1 6 2 6 3 6 4 6 5 6 6 6 7 6 8 6 9 7 0 7 1 7 2 7 3 7 4 Design Pattern – Strategy | uaiHebert < f o n t > < / f o n t > p u b l i cM e r c e d e s ( ) { < f o n t > < / f o n t > e n g i n e B e h a v i o r=n e wP o w e r f u l E n g i n e B e h a v i o r ( ) . A super type will be the best solution to your design problems in like 99% of the situations. I hope this post might be able to help you.”) and another “upgrades” about patterns. < f o n t > < / f o n t > } < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cc l a s sW o o d C a re x t e n d sT o y C a r{ < f o n t > < / f o n t > < f o n t > < / f o n t > p u b l i cW o o d C a r ( ) { < f o n t > < / f o n t > e n g i n e B e h a v i o r=n e wN o E n g i n e B e h a v i o r ( ) . and all others MetalCar. < f o n t > < / f o n t > } < f o n t > < / f o n t > < f o n t > < / f o n t > @ O v e r r i d e < f o n t > < / f o n t > p u b l i cv o i ds t a r t E n g i n e ( ) { < f o n t > < / f o n t > e n g i n e B e h a v i o r . BambooCar).

uaiHebert (: Chip Life Theme by TutorialChip ⋅ WordPress Sh a r e: uaihebert. Required fields are marked * Name * Email * Website CAPTCHA Code * Post Comment Notify me of follow-up comments by email. ← Previous Post Next Post → © Copyright 2013 .29/10/13 Design Pattern – Strategy | uaiHebert (: Leave a Reply Your email address will not be published.com/?p=26 5/5 . Notify me of new posts by email.