Monday, December 29, 2008

Sample of Java Pattern -- Factory

Customer.java
package factory;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class Customer implements Serializable {

private Long customerId;
private String firstName;
private String lastName;
private String address;
private String city;
private String postalCode;
private String workPhone;
private String homePhone;
private String cellPhone;
private String email;
private Date createdOn;
private Date updatedOn;
private Set<Order> orders = new HashSet<Order>(0);

public Long getCustomerId() {
return customerId;
}
public void setCustomerId(Long customerId) {
this.customerId = customerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
public Set<Order> getOrders() {
return orders;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((email == null) ? 0 : email.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
return true;
}

}

Order.java
package factory;

import java.io.Serializable;
import java.util.Date;

public class Order implements Serializable {

private int orderId;
private Customer customer;
private String sku;
private Date createdOn;
private Date updatedOn;
private int status;

public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public Date getUpdatedOn() {
return updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((sku == null) ? 0 : sku.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Order other = (Order) obj;
if (sku == null) {
if (other.sku != null)
return false;
} else if (!sku.equals(other.sku))
return false;
return true;
}
}

DomainObjMgmt.java
package factory;

import java.util.Date;

public class DomainObjMgmt {
public static Object create(String which){
Object obj = null;
if (which.equalsIgnoreCase("customer")) {
Customer customer = new Customer();
customer.setFirstName("firstName");
customer.setLastName("lastName");
customer.setAddress("address");
customer.setCity("city");
customer.setPostalCode("postalCode");
customer.setWorkPhone("workPhone");
customer.setHomePhone("homePhone");
customer.setCellPhone("cellPhone");
customer.setEmail("email");
customer.setCreatedOn(new Date());
customer.setUpdatedOn(new Date());
obj = customer;
System.out.println("Creating a customer, done.");
}else if(which.equalsIgnoreCase("order")){
Order order = new Order();
order.setSku("sku");
order.setCreatedOn(new Date());
order.setUpdatedOn(new Date());
obj = order;
System.out.println("Creating an order, done.");
}else{
obj = null;
System.out.println("Could not create this object");
}
return obj;
}
}


FactoryTest.java
package factory;

public class FactoryTest {
public static void main(String[] args) {
Customer customer = (Customer)DomainObjMgmt.create("customer");
Order order = (Order)DomainObjMgmt.create("order");
DomainObjMgmt.create("advisor");
}
}


you may download the source code.

No comments:

Post a Comment