原型模式实例订单拆分

l现在有一个订单处理系统,里面有一个保存订单的业务功能,需求:每当订单的预定产品数量超过1000的时候,就需要把订单拆成两份订单来保存。如果拆成两份后还是超过1000,则继续拆分,直到每份产品预订数量不超过1000.

根据业务,目前的订单系统分成两种,一种是个人订单、一种是公司订单。

客户名称、产品对象(IDName),订购产品数量。

公司名称、产品对象(IDName),订购产品数量。

 

 订单接口

package B;

public interface Order {
	int getNum();

	void setNum(int num);

	void display();

	Object Clone() throws CloneNotSupportedException;
}

产品类 

package B;

public class Product implements Cloneable {
	private int Id;
	private String name;

	public Product() {

	}

	public int getId() {
		return Id;
	}

	public void setId(int id) {
		Id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Object Clone() throws CloneNotSupportedException {

		return super.clone();
	}
}

 客户订单

package B;

public class CustomerOrder implements Cloneable, Order {
	private String Uname;
	private int num;
	private Product product;

	public CustomerOrder(String Uname) {
		this.Uname = Uname;
		product = new Product();
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}

	private CustomerOrder(Product product) throws CloneNotSupportedException {
		this.product = (Product) product.Clone();
	}

	public void setProduct(int Pid, String Pname) {
		product.setId(Pid);
		product.setName(Pname);
	}

	public void display() {
		System.out.print("客户名:" + Uname + " ");
		System.out.println("订购数目:" + num);
		System.out
				.println("产品号" + product.getId() + " 产品名" + product.getName());
	}

	public Object Clone() throws CloneNotSupportedException {
		CustomerOrder obj = new CustomerOrder(this.product);
		obj.Uname = this.Uname;
		obj.num = this.num;
		return obj;
	}
}

 

 企业订单

package B;

public class CompanyOrder implements Cloneable, Order {
	private String Uname;
	private int num;
	private Product product;

	public CompanyOrder(String Uname) {
		this.Uname = Uname;
		product = new Product();
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getNum() {
		return num;
	}

	private CompanyOrder(Product product) throws CloneNotSupportedException {
		this.product = (Product) product.Clone();
	}

	public void setProduct(int Pid, String Pname) {
		product.setId(Pid);
		product.setName(Pname);
	}

	public void display() {
		System.out.print("客户名:" + Uname + " ");
		System.out.println("订购数目:" + num);
		System.out
				.println("产品号" + product.getId() + " 产品名" + product.getName());
	}

	public Object Clone() throws CloneNotSupportedException {
		CompanyOrder obj = new CompanyOrder(this.product);
		obj.Uname = this.Uname;
		obj.num = this.num;
		return obj;
	}
}

 拆分订单操作类

package B;

public class Chaifen {
	private Order co;

	public Chaifen() {

	}

	public Chaifen(Order co) {

		this.co = co;
	}

	public void Manage() throws CloneNotSupportedException {
		while (co.getNum() > 1000) {
			Order c2 = (Order) co.Clone();
			c2.setNum(1000);
			c2.display();
			co.setNum(co.getNum() - 1000);
		}
		co.display();
	}
}

 客户端

package B;

public class Test {

	public static void main(String args[]) throws CloneNotSupportedException {
		CustomerOrder co = new CustomerOrder("王浩");
		co.setNum(3500);
		co.setProduct(1, "蒙牛");
		Chaifen cf = new Chaifen(co);
		cf.Manage();
		CompanyOrder cp = new CompanyOrder("山东农大");
		cp.setNum(2200);
		cp.setProduct(2, "光明");
		Chaifen cf2 = new Chaifen(cp);
		cf2.Manage();
	}
}

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值