Friday, February 27, 2009

Sample of Java Pattern -- Proxy

Order.java
package proxy;

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

public class Order implements Serializable {

private int orderId;
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 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;
}
}

OrderDAO.java
package proxy;

import java.util.List;

public interface OrderDAO {
public List<Order> getAllOrders();
}


OrderDAOImpl.java
package proxy;

import java.util.ArrayList;
import java.util.List;

public class OrderDAOImpl implements OrderDAO{
public List<Order> getAllOrders(){
List<Order> list = new ArrayList<Order>();
for(int i =0; i<10; i++){
Order order = new Order();
order.setSku("sku"+i);
list.add(order);
}
return list;
}
}


OrderDAOProxy.java
package proxy;

import java.util.List;

public class OrderDAOProxy implements OrderDAO{
public List<Order> getAllOrders(){
long stTime = System.currentTimeMillis();
OrderDAO orderDAOImpl = new OrderDAOImpl();
List<Order> list = orderDAOImpl.getAllOrders();
long endTime = System.currentTimeMillis();
System.out.println("took " + (endTime - stTime) + " milliseconds.");
return list;
}
}


ProxyTest.java
package proxy;

public class ProxyTest {

public static void main(String[] args) {
OrderDAO orderDAOProxy = new OrderDAOProxy();
orderDAOProxy.getAllOrders();
}

}


you may download the source code.

Saturday, February 21, 2009

Sample of Java Pattern -- State

OrderState.java
package state;

public interface OrderState {
public void handleOrder(String orderNo);
public void cancelOrder(String orderNo);
public void completeOrder(String orderNo);
}



OrderOpendState.java
package state;

public class OrderOpendState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderOpendState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("handle this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderInProgressState());
}

public void cancelOrder(String orderNo){
System.out.println("cancel this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderCancelledState());
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under opend status, orderNo=" + orderNo);
}
}



OrderInProgressState.java
package state;

public class OrderInProgressState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderInProgressState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under in progress status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("cancel this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderCancelledState());
}

public void completeOrder(String orderNo){
System.out.println("complete this order, orderNo=" + orderNo);
orderStateMgmt.setState(orderStateMgmt.getOrderClosedState());
}
}



OrderCancelledState.java
package state;

public class OrderCancelledState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderCancelledState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under cancelled status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("could not cancel this order under cancelled status, orderNo=" + orderNo);
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under cancelled status, orderNo=" + orderNo);
}
}



OrderClosedState.java
package state;

public class OrderClosedState implements OrderState {

OrderStateMgmt orderStateMgmt;

public OrderClosedState(OrderStateMgmt orderStateMgmt){
this.orderStateMgmt = orderStateMgmt;
}

public void handleOrder(String orderNo){
System.out.println("could not handle this order under closed status, orderNo=" + orderNo);
}

public void cancelOrder(String orderNo){
System.out.println("could not cancel this order under closed status, orderNo=" + orderNo);
}

public void completeOrder(String orderNo){
System.out.println("could not complete this order under closed status, orderNo=" + orderNo);
}
}



OrderStateMgmt.java
package state;

public class OrderStateMgmt {

private OrderOpendState orderOpendState;
private OrderInProgressState orderInProgressState;
private OrderCancelledState orderCancelledState;
private OrderClosedState orderClosedState;

private OrderState orderState;

public OrderStateMgmt(){
this.orderOpendState = new OrderOpendState(this);
this.orderInProgressState = new OrderInProgressState(this);
this.orderCancelledState = new OrderCancelledState(this);
this.orderClosedState = new OrderClosedState(this);
this.orderState = this.orderOpendState;
System.out.println("The order inital status is: opend.");
}

public void setState(OrderState orderState){
this.orderState = orderState;
}

public OrderOpendState getOrderOpendState() {
return orderOpendState;
}

public OrderInProgressState getOrderInProgressState() {
return orderInProgressState;
}

public OrderCancelledState getOrderCancelledState() {
return orderCancelledState;
}

public OrderClosedState getOrderClosedState() {
return orderClosedState;
}

public void handleOrder(String orderNo){
orderState.handleOrder(orderNo);
}

public void cancelOrder(String orderNo){
orderState.cancelOrder(orderNo);
}

public void completeOrder(String orderNo){
orderState.completeOrder(orderNo);
}
}



OrderStateTest.java
package state;

public class OrderStateTest {

public static void main(String[] args) {
OrderStateMgmt orderStateMgmtA = new OrderStateMgmt();

orderStateMgmtA.handleOrder("01");
orderStateMgmtA.completeOrder("01");

OrderStateMgmt orderStateMgmtB = new OrderStateMgmt();

orderStateMgmtB.handleOrder("02");
orderStateMgmtB.cancelOrder("02");

OrderStateMgmt orderStateMgmtC = new OrderStateMgmt();

orderStateMgmtC.completeOrder("03");
}

}



you may download the source code.

Friday, February 13, 2009

Sample of Java Pattern -- Composite

Department.java
package composite;

import java.util.List;

public interface Department {
public Department getDepartment();
public String getDepartmentname();
public void add(Department department);
public void remove(Department department);
public boolean hasSubDepartments();
public List<Department> getSubDepartments();
}


CompositeDepartment.java
package composite;

import java.util.ArrayList;
import java.util.List;

public class CompositeDepartment implements Department {
private String name;
private List<Department> departments = new ArrayList<Department>();

public CompositeDepartment(String name) {
super();
System.out.println("Department " + name + " is created.");
this.name = name;
}

public Department getDepartment() {
return this;
}

public String getDepartmentname() {
return this.name;
}

public void add(Department department){
System.out.println("Department " + department.getDepartmentname() + " is added to Department " + this.name + ".");
departments.add(department);
}

public void remove(Department department){
System.out.println("Department " + department.getDepartmentname() + " is removed from Department " + this.name + ".");
departments.remove(department);
}

public boolean hasSubDepartments(){
if(departments == null || departments.isEmpty())
return false;
return true;
}

public List<Department> getSubDepartments(){
if(hasSubDepartments())
return departments;
return null;
}
}


SingleDepartment.java
package composite;

import java.util.List;

public class SingleDepartment implements Department {
private String name;

public SingleDepartment(String name) {
super();
System.out.println("Department " + name + " is created.");
this.name = name;
}

public Department getDepartment() {
return this;
}

public String getDepartmentname() {
return this.name;
}

public void add(Department department){
System.out.println("Could not add a child department in Department " + this.name + ".");
}

public void remove(Department department){
System.out.println("Department " + department.getDepartmentname() + " doesn't belong to Department " + this.name + ".");
}

public boolean hasSubDepartments(){
return false;
}

public List<Department> getSubDepartments(){
return null;
}
}


CompositeTest.java
package composite;

public class CompositeTest {
public static void main(String[] args) {
Department hq = new CompositeDepartment("HQ");
Department financing = new SingleDepartment("Financing");
Department marketing = new CompositeDepartment("Marketing");
Department preSales = new SingleDepartment("PreSales");
Department sales = new SingleDepartment("Sales");
Department postSales = new SingleDepartment("PostSales");
Department techSupport = new SingleDepartment("TechSupport");
hq.add(financing);
hq.add(marketing);
hq.add(techSupport);
marketing.add(preSales);
marketing.add(sales);
marketing.add(postSales);
System.out.println("hq has sub departments: " + hq.hasSubDepartments());
System.out.println("marketing has sub departments: " + marketing.hasSubDepartments());
System.out.println("techSupport has sub departments: " + techSupport.hasSubDepartments());
marketing.remove(preSales);
marketing.remove(sales);
marketing.remove(postSales);
sales.remove(preSales);
hq.remove(financing);
hq.remove(marketing);
hq.remove(techSupport);
}
}


you may download the source code.

Saturday, February 7, 2009

Sample of Java Pattern -- Iterator

Aggregate.java
package iterator;

public interface Aggregate {
public abstract Iterator iterator();
}


Iterator.java
package iterator;

public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}


Order.java
package iterator;

import java.util.Date;

public class Order {
private String name;
private Date createdOn = new Date();
private Date updatedOn = new Date();

public Order(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}


Orders.java
package iterator;

import java.util.Date;

public class Orders implements Aggregate{
private Order[] orders;
private int last = 0;

public Orders(int max){
this.orders = new Order[max];
}

public Order getOrderAt(int index){
return this.orders[index];
}

public void appendOrder(Order order){
this.orders[last] = order;
this.last ++ ;
}

public int getLength(){
return orders.length;
}

public Iterator iterator() {
return new OrdersIterator(this);
}
}


OrdersIterator.java
package iterator;

public class OrdersIterator implements Iterator{
private Orders orders;
private int index;

public OrdersIterator(Orders orders) {
this.orders = orders;
this.index = 0;
}

public boolean hasNext(){
if(index < orders.getLength()){
return true;
}else{
return false;
}
}

public Order next(){
Order order = orders.getOrderAt(this.index);
this.index ++ ;
return order;
}
}


IteratorTest.java
package iterator;

public class IteratorTest {

public static void main(String[] args) {
Orders orders = new Orders(3);
orders.appendOrder(new Order("order1"));
orders.appendOrder(new Order("order2"));
orders.appendOrder(new Order("order3"));
Iterator iterator = orders.iterator();
while(iterator.hasNext()) {
Order order = (Order)iterator.next();
System.out.println(order.getName());
}
}

}


you may download the source code.