Showing posts with label patterns. Show all posts
Showing posts with label patterns. Show all posts

19 April 2016

Java EE Patterns (edit)


  • Numbers on the arrows indicate a possible call sequence in a request.
  • patterns
    • Front Controller: initial point of contact for handling all related requests. The Front Controller centralizes control logic that might otherwise be duplicated, and manages the key request handling activities.
    • Transfer Object: carries multiple data elements across a tier
    • Transfer Object Assembler:  builds an application model as a composite Transfer Object. The Transfer Object Assembler aggregates multiple Transfer Objects from various business components and services, and returns it to the client. 
    • Persistent Domain Object: Rich domain object, ie. having rich behavior/bus.logic and persistent 
    • Web Service Broker: exposes and brokers one or more services using XML and web protocols.
      • The Web Service Broker can be generalised to a Protocol Broker.
    • Service Facade: encapsulates business-tier components and exposes a coarse-grained service to remote clients. Clients access a Service Façade instead of accessing business components directly. 
    • Service: Fine-grained, reusable logic in an EJB with local access only, product of decomposition
    • Data Access Object: abstracts and encapsulates all access to the persistent store. The Data Access Object manages the connection with the data source to obtain and store data. 
    • Asynchronous Resource Integrator: Invocation of a Service from a Message-Driven Bean (invoked by a messaging system via JMS)
    • Payload extractor: factor out the (reusable) type checking and error handling for a MDB message into a reusable interceptor; poison messages moved by the interceptor to a “dead letter queue” via a stateless EJB using the JMS API
    • Resource Binder: put a custom resource into JNDI using a @Singleton with @Startup and the JNDI API (Context.(re)bind()).

23 April 2012

Builder-Style DTO

MyDto is a Data Transfer Object that can be built using a chain of building calls.
You start by calling a factory method and then you add the info for MyDto step by step to the Builder. Each time this returns the builder, so you can chain the calls.
When you are finished, you call the build call, which returns the Data Transfer Object:

MyDto dto  = new MyDto.Builder()
       .name("Jef Blaaskop")
       .address("Antigoon 4")
       .city("Amoras")
       .build(); 
 
The constructor of MyDto is private: you can only make it using the Builder. The Builder is an inner class of MyDto, so it can call the private constructor.
public class MyDto implements Serializable{
    private String address;
    private String name;
    private String city;

    private MyDto (String name,String address,String city) {
        this.city= city;
        this.address = address;
        this.name = name;
    }

    private MyDto() {}

    public static class Builder{
        private MyDto dto;
         Builder(){
            this.dto= new MyDto ();
        }

        public Builder address(String address){
           // some checking
            this.dto.setAddress(address);
            return this;
        }

        public Builder name(String name){
            if(name == null)
                throw new IllegalArgumentException("Anonymous not allowed");
            this.dto.setName(name);
            return this;
        }

      public Builder city(String city){
           // some checking
            this.dto.setCitys(city);
            return this;
        }

        public MyDto build(){
            return this.dto;
        }
     }      // end Builder 
// remaining getters / setters ommitted
} // end MyDto

22 April 2012

DAO Example

public interface AnniversaryDao {
  Anniversary getByYear(int years);
}
public class AnniversaryJdbcDao implements AnniversaryDao{
  private static final String GET_BY_YEAR = 
    "SELECT * from Anniversary WHERE years = ?";
  //...
  public Anniversary getByYear(int years) {
    //…
    PreparedStatement statement = connection.prepareStatement(GET_BY_YEAR);
    statement.setInt(1,years);
    ResultSet rs = statement.executeQuery();
    if (rs.next()){
      result = new Anniversary(
      rs.getInt("years"),
      rs.getString("material"),
      rs.getString("flowers"));
    }
   //  ....
  return result;
  }
}
public class AnniversaryJpaSeDao implements AnniversaryDao{
   EntityManagerFactory emf = Persistence
    .createEntityManagerFactory("JPA-03PU");

  private EntityManager getEntityManager() {
    return emf.createEntityManager();
  }
  //...
  public Anniversary getByYear(int years) {
    return getEntityManager().createNamedQuery(findAnniversayByYear)
      .setParameter("years", years)
      .getSingleResult();
  }
}
@ManagedBean
@RequestScoped
public class AnniversaryJpaEe6Dao implements AnniversaryDao{
  @¨PersistenceContext private EntityManager em;

  //...
  public Anniversary getByYear(int years) {
    return em.createNamedQuery(findAnniversayByYear)
      .setParameter("years", years)
      .getSingleResult();
  }
}

21 June 2010

MVC diagram

Diagram for the InfoTool MVC example in SL-285

25 March 2010

GUI architecture patterns

Model View Controller is the common architecture for graphical user interfaces.
There are however many variations on it, partly depending on the underlying technology.
As a result MVC is often misunderstood, and the same names are used for different beasts.
Here's a clear overview of MVC and its brethern.