Suggested Pages

Wednesday, May 20, 2015

Design Pattern Builder - Example

Notification.java


import java.util.Date;

/**
 * The Class Notification.
 */
public class Notification {

 /** The system. */
 int SYSTEM = 1;

 /** The application. */
 int APPLICATION = 2;

 /** The builder. */
 private Builder builder;

 /**
  * Instantiates a new notification.
  * 
  * @param builder
  *            the builder
  */
 private Notification(Builder builder) {
  this.builder = builder;
 }

 /**
  * Gets the code.
  * 
  * @return the code
  */
 public int getCode() {
  return builder.code;
 }

 /**
  * Gets the priority.
  * 
  * @return the priority
  */
 public int getPriority() {
  return builder.priority;
 }

 /**
  * Gets the info.
  * 
  * @return the info
  */
 public String getInfo() {
  return builder.info;
 }

 /**
  * Gets the short description.
  * 
  * @return the short description
  */
 public String getShortDescription() {
  return builder.shortDescription;
 }

 /**
  * Gets the long description.
  * 
  * @return the long description
  */
 public String getLongDescription() {
  return builder.longDescription;
 }

 /**
  * Gets the date.
  * 
  * @return the date
  */
 public Date getDate() {
  return builder.date;
 }

 /**
  * The Class Builder.
  */
 static class Builder {

  /** The date. */
  private Date date = new Date();

  /** The long description. */
  private String longDescription;

  /** The short description. */
  private String shortDescription;

  /** The info. */
  private String info;

  /** The priority. */
  private int priority = 0;

  /** The code. */
  private final int code;

  /**
   * Instantiates a new builder.
   * 
   * @param code
   *            the code
   */
  public Builder(int code) {
   this.code = code;
  }

  /**
   * Sets the short description.
   * 
   * @param shortDescription
   *            the short description
   * @return the builder
   */
  public Builder setShortDescription(String shortDescription) {
   this.shortDescription = shortDescription;
   return this;
  }

  /**
   * Sets the date.
   * 
   * @param date
   *            the date
   * @return the builder
   */
  public Builder setDate(Date date) {
   this.date = date;
   return this;
  }

  /**
   * Sets the info.
   * 
   * @param info
   *            the info
   * @return the builder
   */
  public Builder setInfo(String info) {
   this.info = info;
   return this;
  }

  /**
   * Sets the long description.
   * 
   * @param longDescription
   *            the long description
   * @return the builder
   */
  public Builder setLongDescription(String longDescription) {
   this.longDescription = longDescription;
   return this;
  }

  /**
   * Sets the priority.
   * 
   * @param priority
   *            the priority
   * @return the builder
   */
  public Builder setPriority(int priority) {
   this.priority = priority;
   return this;
  }
  
   /**
   * Builds the.
   *
   * @return the notification
   */
  public Notification build() {
   Notification notification = new Notification(this);
   return notification;
  }

 }
}

No comments :

Post a Comment

Suggested Pages