婚介所
婚介所其实就是找对象的一个代理,请仿照我们的课堂例子“论坛权限控制代理”完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示“对不起,不能早恋!”,并终止业务。
类图
java
package rjsj.no14; public class Client { public static void main(String[] args) { AbstractPermission permission; permission = new MatrimonialAgency(); System.out.println("未成年人..."); permission.toMatch(); System.out.println("**************************************************"); System.out.println("年龄大于18..."); permission.setLevel(1); permission.toMatch(); } }
package rjsj.no14; public class MatrimonialAgency implements AbstractPermission{ private RealPermission permission = new RealPermission(); private int level = 0; @Override public void setLevel(int level) { this.level = level; } @Override public void toMatch() { if(level == 0){ System.out.println("对不起,不能早恋!"); } else if (level == 1){ permission.toMatch(); } } }
package rjsj.no14; public class RealPermission implements AbstractPermission{ @Override public void setLevel(int level) { } @Override public void toMatch() { System.out.println("开始找对象"); } }
package rjsj.no14; public interface AbstractPermission { public void setLevel(int level); public void toMatch(); }
C++
#include <iostream> #include <string> using namespace std; class Person{ public: string name; int age; Person(string n,int a){ name=n; age=a; } }; class Center{ protected: string name; public: Center(string n){ name=n; } virtual void findlove(Person* person)=0; }; class Marry:public Center{ public: Marry(string name):Center(name){} void findlove(Person* person){ if(person->age<18){ cout <<name<<":"<<person->name<<",不能早恋!" << endl; } else if(person->age<100){ cout <<name<<":"<<person->name<<",我们会为您找到合适的伴侣!" << endl; } } }; int main(){ 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); return 0; }
运行截图
标签:name,level,软件设计,void,permission,代理,模式,toMatch,public From: https://www.cnblogs.com/wlqyyds/p/16858095.html