Java EE Revisits GoF Design Patterns

46
Java EE revisits Design Patterns Reza Rahman @reza_rahman Murat Yener @yenerm

Transcript of Java EE Revisits GoF Design Patterns

Page 1: Java EE Revisits GoF Design Patterns

Java EE revisits Design Patterns

Reza Rahman @reza_rahman Murat Yener @yenerm

Page 2: Java EE Revisits GoF Design Patterns

Who we are?

Reza Rahman, Java Evangelist @reza_rahman

[email protected]

Murat Yener, Mobile Dev, Java EE enthusiast @yenerm

[email protected]

Page 3: Java EE Revisits GoF Design Patterns

40% discount with promo

code

VBK43 when ordering

through wiley.com

valid until end of December 2015

Page 4: Java EE Revisits GoF Design Patterns

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

Page 5: Java EE Revisits GoF Design Patterns

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

Page 6: Java EE Revisits GoF Design Patterns

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!!

Page 7: Java EE Revisits GoF Design Patterns

Singleton in Java EE

public class SingletonBean {

}

Page 8: Java EE Revisits GoF Design Patterns

Singleton in Java EE

@Singleton public class SingletonBean {

}

Page 9: Java EE Revisits GoF Design Patterns

Singleton in Java EE

@Singleton public class SingletonBean {

@PostConstruct void startUpTask() {

// Perform start up tasks }

}

Page 10: Java EE Revisits GoF Design Patterns

Singleton in Java EE

@Startup @Singleton public class SingletonBean {

@PostConstruct void startUpTask() {

// Perform start up tasks }

}

Page 11: Java EE Revisits GoF Design Patterns

Singleton (cont.)

• Simple

• No XML configuration needed

• Simple inject to use beans

@Inject SingletonBean bean;

Page 12: Java EE Revisits GoF Design Patterns

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

Page 13: Java EE Revisits GoF Design Patterns

Abstract Factory

• Creational pattern, to encapsulate creation

• Only the part responsible for creation changes

Page 14: Java EE Revisits GoF Design Patterns

Abstract Factory in Java EEpublic class HelloProducer {

public String getMessage(){ return "Hello World"; }

}

private String message;

Page 15: Java EE Revisits GoF Design Patterns

Abstract Factory in Java EEpublic class HelloProducer {

@Produces public String getMessage(){ return "Hello World"; }

}

@Inject private String message;

Page 16: Java EE Revisits GoF Design Patterns

Abstract Factory in Java EEpublic class HelloProducer { @Produces public String getMessage(){ return "Hello World"; }

@Produces public String getGreeting(){ return "Greetings Earthlings"; } }

@Inject private String message; ?!?

Page 17: Java EE Revisits GoF Design Patterns

Abstract Factory in Java EEpublic 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;

Page 18: Java EE Revisits GoF Design Patterns

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

Page 19: Java EE Revisits GoF Design Patterns

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

Page 20: Java EE Revisits GoF Design Patterns

Facade in Java EEpublic class HelloFacade {

public String executeBusinessLogic(){ //very very complex logic }

}

private HelloFacade service;

Page 21: Java EE Revisits GoF Design Patterns

Facade in Java EE@Stateless public class HelloFacade {

public String executeBusinessLogic(){ //very very complex logic }

}

@Inject private HelloFacade service;

Page 22: Java EE Revisits GoF Design Patterns

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 :)

Page 23: Java EE Revisits GoF Design Patterns

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.

Page 24: Java EE Revisits GoF Design Patterns

Decorator in Java EE public class ConfigDecorator implements Accessory {

@Inject private Accessory product; public Long getPrice() { return getPrice()+1000; } }

Page 25: Java EE Revisits GoF Design Patterns

Decorator in Java EE @Decorator public class ConfigDecorator implements Accessory {

@Inject @Delegate private Accessory product; public Long getPrice() { return getPrice()+1000; } }

Page 26: Java EE Revisits GoF Design Patterns

Decorator in Java EE

<decorators> <class>com.patterns.ConfigDecorator</class>

</decorators>

Page 27: Java EE Revisits GoF Design Patterns

Decorator in Java EE

<decorators> <class>com.patterns.ConfigDecorator</class> <class>com.patterns.AnotherDecorator</class> </decorators>

Page 28: Java EE Revisits GoF Design Patterns

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

Page 29: Java EE Revisits GoF Design Patterns

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

Page 30: Java EE Revisits GoF Design Patterns

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); } }

Page 31: Java EE Revisits GoF Design Patterns

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()); } }

Page 32: Java EE Revisits GoF Design Patterns

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 :)

Page 33: Java EE Revisits GoF Design Patterns

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

Page 34: Java EE Revisits GoF Design Patterns

The Model

• Represents data and related business logic

• Model is usually a CDI bean

Page 35: Java EE Revisits GoF Design Patterns

The View

• Visual representation of Data

• User interacts with View to access data and trigger business logic

Page 36: Java EE Revisits GoF Design Patterns

The Controller

• Links view with model

• Directs application flow

Page 37: Java EE Revisits GoF Design Patterns

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

Page 38: Java EE Revisits GoF Design Patterns

Entity

• Lightweight domain object which is persistable

• Annotated with javax.persistance.Entity

• Can represent relational data object/relational mapping annotations

Page 39: Java EE Revisits GoF Design Patterns

Entity

@Entity public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;

protected String firstName; protected String lastName; }

Page 40: Java EE Revisits GoF Design Patterns

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

Page 41: Java EE Revisits GoF Design Patterns

DAO

@PersistenceContext EntityManager em;

//… SomeObject obj = em.find(SomeObject.class, id);

em.persist(obj);

em.merge(obj);

Page 42: Java EE Revisits GoF Design Patterns

DTO

• Serializable object to transfer data between layers

• simple cases entity is DTO

• often annotated via JAXB and JSON processing Java API.

Page 43: Java EE Revisits GoF Design Patterns

DTO@Entity

public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;

protected String firstName; protected String lastName; }

Page 44: Java EE Revisits GoF Design Patterns

DTO@Entity @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Contact { @Id @GeneratedValue(strategy = GenerationType.AUTO) @XmlTransient private Long id;

protected String firstName; protected String lastName; }

Page 45: Java EE Revisits GoF Design Patterns

Domain Driven Design

Page 46: Java EE Revisits GoF Design Patterns

</slides>Reza Rahman @reza_rahman

[email protected]

Murat Yener @yenerm

[email protected]