实验14:代理模式

实验14:代理模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解代理模式的动机,掌握该模式的结构;

2、能够利用代理模式解决实际问题。

 
   

 


[实验任务一]:婚介所

婚介所其实就是找对象的一个代理,请仿照我们的课堂例子“论坛权限控制代理”完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示“对不起,不能早恋!”,并终止业务。

实验要求:

1. 提交类图;

 

 

 

2. 提交源代码;

JAVA

public abstract class Center

{

    protected String name;

    public Center(String name){this.name=name;}

    public abstract void findlove(Person person);

}

public class Marry extends Center

{

     public Marry(String name){super(name);}

        public void findlove(Person person)

        {

            if(person.getAge()<18)

            {

                System.out.println(name+":"+person.getName()+",对不起!不能早恋!");

            }

            else if(person.getAge()<100)

            {

                System.out.println(name+":"+person.getName()+",我们会为您找到合适的伴侣!");

            }

            else

            {

                System.out.println(name+":"+person.getName()+",对不起!您年龄太大了!");

            }

        }

}

public class Person {

    protected String name;

    protected int age;

    public Person(String name,int age)

    {

        this.name=name;

        this.age=age;

    }

    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;

    }

}

public class Client

{

    public static void main(String args[])

    {

        Center center1,center2;

        center1=new Marry("婚介");

        center2=new Marry("婚介");

        Person b1=new Person("喜羊羊",29);

        Person b2=new Person("美羊羊",16);

        center1.findlove(b1);

        center2.findlove(b2);

    }

}

C++

public abstract class Center

{

    protected String name;

    public Center(String name){this.name=name;}

    public abstract void findlove(Person person);

}

public class Marry extends Center

{

     public Marry(String name){super(name);}

        public void findlove(Person person)

        {

            if(person.getAge()<18)

            {

                System.out.println(name+":"+person.getName()+",对不起!不能早恋!");

            }

            else if(person.getAge()<100)

            {

                System.out.println(name+":"+person.getName()+",我们会为您找到合适的伴侣!");

            }

            else

            {

                System.out.println(name+":"+person.getName()+",对不起!您年龄太大了!");

            }

        }

}

public class Person {

    protected String name;

    protected int age;

    public Person(String name,int age)

    {

        this.name=name;

        this.age=age;

    }

    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;

    }

}

public class Client

{

    public static void main(String args[])

    {

        Center center1,center2;

        center1=new Marry("婚介");

        center2=new Marry("婚介");

        Person b1=new Person("喜羊羊",29);

        Person b2=new Person("美羊羊",16);

        center1.findlove(b1);

        center2.findlove(b2);

    }

}

 

 

 

3. 注意编程规范。

 

posted @ 2022-11-04 11:31  萧贾jzm  阅读(86)  评论(0编辑  收藏  举报
//歌单id