安全代理模式实例订单系统
MyEclipse自动生成的UML图,好像有一点丑。。。
实现的相同接口
package Dingdan;
public interface IOrder {
String GetproductName();
void SetproductName(String productName, String user);
int GetOrderNum();
void SetOrderNum(int OrderNum, String user);
String GetOrderUser();
void SetOrderUser(String Orderuser,String user);
}
真实订单
package Dingdan;
public class Order implements IOrder {
private String Pname;
private int Onum;
private String Ouser;
public Order(String pname, int onum, String ouser) {
Pname = pname;
Onum = onum;
Ouser = ouser;
}
@Override
public String GetproductName() {
// TODO 自动生成的方法存根
return Pname;
}
@Override
public void SetproductName(String productName, String user) {
this.Pname=productName;
}
@Override
public int GetOrderNum() {
return Onum;
}
@Override
public void SetOrderNum(int OrderNum, String user) {
this.Onum=OrderNum;
}
@Override
public String GetOrderUser() {
return Ouser;
}
@Override
public void SetOrderUser(String Orderuser, String user) {
this.Ouser=Orderuser;
}
}
订单代理
package Dingdan;
public class ProxyOrder implements IOrder {
private Order ord=null;
public ProxyOrder(Order ord) {
this.ord = ord;
}
public String GetproductName() {
return this.ord.GetproductName();
}
@Override
public void SetproductName(String productName, String user) {
if(user!=null&&user.equals(this.ord.GetOrderUser()))
ord.SetproductName(productName,user);
else
System.out.println("对不起您没有权限");
}
@Override
public int GetOrderNum() {
return this.ord.GetOrderNum();
}
@Override
public void SetOrderNum(int OrderNum, String user) {
if(user!=null&&user.equals(this.ord.GetOrderUser()))
ord.SetOrderNum(OrderNum,user);
else
System.out.println("对不起,您没有权限");
}
@Override
public String GetOrderUser() {
return this.ord.GetOrderUser();
}
@Override
public void SetOrderUser(String Orderuser, String user) {
if(user!=null&&user.equals(this.ord.GetOrderUser()))
ord.SetOrderUser(Orderuser,user);
else
System.out.println("对不起,您没有权限");
}
}
客户端
package Dingdan;
public class Test {
public static void main(String args[])
{Order o1=new Order("设计模式",100,"张三");
ProxyOrder po=new ProxyOrder(o1);
po.SetOrderNum(123,"李四");
po.SetOrderUser("王五", "李四");
System.out.println(po.GetOrderNum());
po.SetproductName("五年高考", "张三");
System.out.println(po.GetproductName());
po.SetOrderUser("李四", "张三");
po.SetOrderNum(150, "李四");
System.out.println(po.GetOrderNum());
}
}