Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java EE Revisits GoF Design Patterns

14,417 views

Published on

JavaOne'15 talk with Reza Rahman

Published in: Software
  • Hi All, We are planning to start Hadoop online training batch on this week... If any one interested to attend the demo please register in our website... For this batch we are also provide everyday recorded sessions with Materials. For more information feel free to contact us : siva@keylabstraining.com. For Course Content and Recorded Demo Click Here : http://www.keylabstraining.com/hadoop-online-training-hyderabad-bangalore
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Java EE Revisits GoF Design Patterns

  1. 1. Java EE revisits Design Patterns Reza Rahman @reza_rahman Murat Yener @yenerm
  2. 2. Who we are? Reza Rahman, Java Evangelist @reza_rahman reza.rahman@oracle.com Murat Yener, Mobile Dev, Java EE enthusiast @yenerm murat@muratyener.com
  3. 3. 40% discount with promo code VBK43 when ordering through wiley.com valid until end of December 2015
  4. 4. the beginning Dijkstra: Object-oriented programming is an exceptionally bad idea which could only have originated in California Design patterns are "descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context." Gang of Four
  5. 5. Enterprise Java and Design Patterns • JavaOne 2000 talk (TS-1341) Prototyping patterns for the J2EE Platform • Core J2EE Design Patterns (Deepak Alur, John Crupi and Dan Malks) • Out-of-box DP implementations with Java EE 5
  6. 6. Singleton • “Ensure a class has only one instance, and provide a global point of access to it • Classic implementation: private constructor, double locking, static initializer, enums… • must be thread safe!!
  7. 7. Singleton in Java EE public class SingletonBean { }
  8. 8. Singleton in Java EE @Singleton public class SingletonBean { }
  9. 9. Singleton in Java EE @Singleton public class SingletonBean { @PostConstruct void startUpTask() { // Perform start up tasks } }
  10. 10. Singleton in Java EE @Startup @Singleton public class SingletonBean { @PostConstruct void startUpTask() { // Perform start up tasks } }
  11. 11. Singleton (cont.) • Simple • No XML configuration needed • Simple inject to use beans @Inject SingletonBean bean;
  12. 12. pros & cons • The Good: expensive objects, caching, global access… • The Bad: overuse can cause problems, lazy loading can cause delays, not lazy loading may cause memory problems • and the ugly: considered as an anti-pattern
  13. 13. Abstract Factory • Creational pattern, to encapsulate creation • Only the part responsible for creation changes
  14. 14. Abstract Factory in Java EE public class HelloProducer { public String getMessage(){ return "Hello World"; } } private String message;
  15. 15. Abstract Factory in Java EE public class HelloProducer { @Produces public String getMessage(){ return "Hello World"; } } @Inject private String message;
  16. 16. Abstract Factory in Java EE public class HelloProducer { @Produces public String getMessage(){ return "Hello World"; } @Produces public String getGreeting(){ return "Greetings Earthlings"; } } @Inject private String message; ?!?
  17. 17. Abstract Factory in Java EE public class HelloProducer { @Produces @Named(“Message") public String getMessage(){ return "Hello World"; } @Produces @Named(“Greeting") public String getGreeting(){ return "Greetings Earthlings"; } } @Inject @Named(“Message") private String message;
  18. 18. pros & cons • The Good: easy to implement, no boilerplate, works magically • The Bad: named annotation is not type safe so same type of objects better be wrapped or annotated with qualifiers • and the ugly: may introduce hard to understand flow
  19. 19. Facade in Java EE • Hides the complex logic and provides an interface for the clients • Typically used in APIs • Classic implementation: Just create a method to hide complexity
  20. 20. Facade in Java EE public class HelloFacade { public String executeBusinessLogic(){ //very very complex logic } } private HelloFacade service;
  21. 21. Facade in Java EE @Stateless public class HelloFacade { public String executeBusinessLogic(){ //very very complex logic } } @Inject private HelloFacade service;
  22. 22. pros & cons • The Good: stateless or stateful, easy to implement. Can be exposed as rest or web service endpoint. • The Bad: overuse may introduce unnecessary layers • and the ugly: the name of the pattern :)
  23. 23. Decorator in Java EE • Adds behavior to objects in runtime. • More flexible and extensible than inheritance • Classic implementation: Both the decorator and target object shares the same interface. Decorator holds wraps the target object and adds its own behavior.
  24. 24. Decorator in Java EE public class ConfigDecorator implements Accessory {
 @Inject 
 private Accessory product;
 
 public Long getPrice() {
 return getPrice()+1000;
 } 
 }
  25. 25. Decorator in Java EE @Decorator public class ConfigDecorator implements Accessory {
 @Inject @Delegate
 private Accessory product;
 
 public Long getPrice() {
 return getPrice()+1000;
 } 
 }
  26. 26. Decorator in Java EE <decorators> <class>com.patterns.ConfigDecorator</class> </decorators>
  27. 27. Decorator in Java EE <decorators> <class>com.patterns.ConfigDecorator</class> <class>com.patterns.AnotherDecorator</class> </decorators>
  28. 28. pros & cons • The Good: unlike inheritance very easy to change behavior without breaking legacy code. • The Bad: needs xml configuration (<CDI 1.1) • and the ugly: overuse will introduce an execution flow which is hard to follow
  29. 29. Observer in Java EE • Don’t call us, we call you! • Publisher-Subscriber • Classic implementation: Out-of-box implementation! Observable Interface, Listeners via inner classes
  30. 30. Observer in Java EE @Stateless public class HelloWorldObserver { public void traceHelloWorlds( @Observable String message){ //… } } @Stateless public class PublishService { @Inject Event<String> event; public void producer(){ event.fire(“Hey!!” + message); } }
  31. 31. Observer in Java EE @Stateless public class HelloWorldObserver { public void traceHelloWorlds( @Observable SomeObj message){ //… } } @Stateless public class PublishService { @Inject Event<SomeObj> event; public void producer(){ event.fire(new SomeObj()); } }
  32. 32. pros & cons • The Good: very very easy, no boiler plate.. • The Bad: may introduce difficulty to follow execution flow & debug • and the ugly: nothing.. it’s beautiful :)
  33. 33. MVC in Java EE • It is not a pattern but a composite collection of other patterns • Is used and accepted as industry standard in almost all UI frameworks. • Keeps data, logic and UI code separate and easy to change • JSF is a clean and simple implementation of MVC
  34. 34. The Model • Represents data and related business logic • Model is usually a CDI bean
  35. 35. The View • Visual representation of Data • User interacts with View to access data and trigger business logic
  36. 36. The Controller • Links view with model • Directs application flow
  37. 37. MVC in Java EE • Model: Backing beans which are annotated POJOs with @Named and @RequestScope • View: Facelets which are written in XHTML with CSS • Controller: FacesServlet
  38. 38. Entity • Lightweight domain object which is persistable • Annotated with javax.persistance.Entity • Can represent relational data object/relational mapping annotations
  39. 39. Entity @Entity public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; protected String firstName; protected String lastName; }
  40. 40. DAO • Used for accessing the persistent data storage • Usually singleton • Simples cases entity manager is the DAO • Complex cases you create DAOs for entity objects • Usually a CDI managed bean
  41. 41. DAO @PersistenceContext EntityManager em; //… SomeObject obj = em.find(SomeObject.class, id); em.persist(obj); em.merge(obj);
  42. 42. DTO • Serializable object to transfer data between layers • simple cases entity is DTO • often annotated via JAXB and JSON processing Java API.
  43. 43. DTO @Entity public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; protected String firstName; protected String lastName; }
  44. 44. DTO @Entity @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) @XmlTransient private Long id; protected String firstName; protected String lastName; }
  45. 45. Domain Driven Design
  46. 46. </slides> Reza Rahman @reza_rahman reza.rahman@oracle.com Murat Yener @yenerm murat@muratyener.com

×