設計模式10--原型模式(Prototype)

原型模式的本質:克隆生成對象

模式的定義:用原型實例指定創建對象的種類,並通過拷貝這些原型創建新的對象。

package com;

public interface OrderApi {
	
	int getOrderProductNum();
	
	void setOrderProductNum(int num);
	
	/*
	 * 克隆方法,返回原型的實例
	 */
	public OrderApi cloneOrder();
}
package com.impl;

import com.OrderApi;

public class EnterpriseOrder implements OrderApi {
	
	private String companyName;
	private String productId;
	private int orderProductNum;

	
	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	@Override
	public OrderApi cloneOrder() {
		EnterpriseOrder order = new EnterpriseOrder();
		order.setCompanyName(companyName);
		order.setProductId(productId);
		order.setOrderProductNum(orderProductNum);
		return order;
	}
         //@Override使用java Object對象的clone方法,被克隆的對象要implements Cloneable
         //public Object clone()  {
                //Object newOrder = null;
                //try {
                     //newOrder = super.clone();
                //} catch (CloneNotSupportedException e) {
                     //e.printStackTrace();
                //}
                //return newOrder;
        //} 
     	@Override
	public int getOrderProductNum() {
		return orderProductNum;
	}

	@Override
	public void setOrderProductNum(int num) {
		this.orderProductNum = num;

	}

	public String toString() {
		return new StringBuffer().append("productId=" + productId).append(
				",companyName=" + companyName).append(
				",orderProductNum=" + orderProductNum).toString();
	}
}
package com.impl;

import com.OrderApi;

public class PersonalOrder implements OrderApi {

	private String customerName;
	private String productId;
	private int orderProductNum;

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	@Override
	public OrderApi cloneOrder() {
		PersonalOrder order = new PersonalOrder();
		order.setCustomerName(customerName);
		order.setProductId(productId);
		order.setOrderProductNum(orderProductNum);
		return order;
	}
         //@Override使用java Object對象的clone方法,被克隆的對象要implements Cloneable
         //public Object clone()  {
                //Object newOrder = null;
                //try {
                    //newOrder = super.clone();
                //} catch (CloneNotSupportedException e) {
                    //e.printStackTrace();
                //}
               //return newOrder;
         //}       
	@Override
	public int getOrderProductNum() {
		return orderProductNum;
	}

	@Override
	public void setOrderProductNum(int num) {
		this.orderProductNum = num;
	}

	public String toString() {
		return new StringBuffer().append("productId=" + productId).append(
				",customerName=" + customerName).append(
				",orderProductNum=" + orderProductNum).toString();
	}
}
package com;

public class OrderBusiness {
	
	public void saveOrder(OrderApi order){
		while(order.getOrderProductNum()>1000){
			OrderApi newOrder = order.cloneOrder();
			newOrder.setOrderProductNum(1000);
			System.out.println("拆分生成訂單:" + order);
			order.setOrderProductNum(order.getOrderProductNum() - 1000);
		}
		System.out.println("訂單:" + order);
	}
}
import com.OrderBusiness;
import com.impl.EnterpriseOrder;

public class Client {
	public static void main(String[] args) {
		EnterpriseOrder enterpriseOrder = new EnterpriseOrder();
		enterpriseOrder.setCompanyName("SHIXIN747.COMPANY");
		enterpriseOrder.setProductId("010");
		enterpriseOrder.setOrderProductNum(2500);
		OrderBusiness orderBusiness = new OrderBusiness();
		orderBusiness.saveOrder(enterpriseOrder);
	}
}






 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章