原型模式实例订单处理系统

订单处理系统
现在有一个订单处理系统,里面有一个保存订单的业务功能,需求:每当订单的预定产品数量超过1000的时候,就需要把订单拆成两份订单来保存。如果拆成两份后还是超过1000,则继续拆分,直到每份产品预订数量不超过1000.
根据业务,目前的订单系统分成两种,一种是个人订单、一种是公司订单。
客户名称、产品对象(ID,Name),订购产品数量。
公司名称、产品对象(ID,Name),订购产品数量。

在这里插入图片描述

/**
代码具体实现

*/

package proto;

/**
 产品类,用于原型模式中的深复制
有ID 和NAME两个参数

*/


public class Product implements Cloneable {
private int id;
private String name;
public Product(int id, String name) {
    super();
    this.id = id;
    this.name = name;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Product clone()
{
    Product product=null;
    try {
        product=(Product)super.clone();
    } catch (CloneNotSupportedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return product;
}
}
package proto;
/**

订单类,含有产品的对象并进行深复制

*/


public class order implements Cloneable {
private  String orderName;
private  Product product;
private  int number;
public order()
{}

public order(String orderName, Product product, int number) {
    super();
    this.orderName = orderName;
    this.product = product;
    this.number = number;
}
public String getOrderName() {
    return orderName;
}
public void setOrderName(String orderName) {
    this.orderName = orderName;
}
public Product getProduct() {
    return product;
}
public void setProduct(Product product) {
    this.product = product;
}
public int getNumber() {
    return number;
}
public void setNumber(int number) {
    this.number = number;
}
public order clone()
{
    order order=null;
    try {

        order=(order)super.clone();
        order.product=(Product)this.product.clone();
    } catch (CloneNotSupportedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return order;

}
public void show()
{
    System.out.println("订单数量: "+this.getNumber());
    System.out.println("产品id: "+this.getProduct().getId());
    System.out.println("产品名称: "+this.getProduct().getName());
    System.out.println("用户名称: "+this.getOrderName());
}
}
package proto;
/**

订单处理类
 运用迭代的思想对订单进行处理
 两次clone

*/


public class dealOrder {
private order order1;

public dealOrder(order order)
{
    this.order1=order;
}
public  order deal()
{


    order halforder=null;
    if(order1.getNumber()<1000)
        halforder=this.order1.clone();
    else
    {
    while(order1.getNumber()>=1000)
    {

        halforder=this.order1.clone();
        halforder.setNumber(this.order1.getNumber()/2);
        this.order1=halforder.clone();

    }
    }
    return halforder;

}
}
package proto;

public class Clinte {

/**
 * 主界面类
 */
public static void main(String[] args) {
    Product p1=new Product(1,"高数课本");
    order o1=new order("张三",p1,3400);
    dealOrder d1=new dealOrder(o1);
    int i=0;
    i=o1.getNumber()/d1.deal().getNumber();


    System.out.println("一共有"+i+"份订单");
    System.out.println("订单样例如下");
    d1.deal().show();
}
}
发布了109 篇原创文章 · 获赞 40 · 访问量 6157

猜你喜欢

转载自blog.csdn.net/qq_15719613/article/details/105410933
今日推荐