代理模式
[实验任务一]:婚介所
婚介所其实就是找对象的一个代理,请仿照我们的课堂例子“论坛权限控制代理”完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示“对不起,不能早恋!”,并终止业务。
实验要求:
1. 提交类图;
2. 提交源代码;
Client
public class Client {
public static void main(String[] args) {
RealPerson personz=new RealPerson(18,"张三");
RealPerson personl=new RealPerson(16,"李四");
RealPerson personw=new RealPerson(25,"王五");
Proxy pro=new Proxy();
pro.setPerson(personz);
pro.goxq();
pro.setPerson(personl);
pro.goxq();
pro.setPerson(personw);
pro.goxq();
}
}
Proxy
public class Proxy implements AbstractPerson{
private RealPerson person=new RealPerson();
public RealPerson getPerson() {
return person;
}
public void setPerson(RealPerson person) {
this.person = person;
}
@Override
public void goxq() {
// TODO Auto-generated method stub
if(person.getAge()<18) {
System.out.println(person.getName()+"你还未满十八岁,不能早恋");
}else {
person.goxq();
}
}
AbstractPerson
public interface AbstractPerson {
public void goxq();
}
RealPerson
public class RealPerson implements AbstractPerson {
private int age;
private String name;
@Override
public void goxq() {
// TODO Auto-generated method stub
System.out.println(name+"你都"+age+"了,走! 给你说门好亲事!");
}
public RealPerson(int age, String name) {
super();
this.age = age;
this.name = name;
}
public RealPerson() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}