- interface InternetService{
- public void connect();
- public void disconnect();
- }
- interface MailService{
- public void send();
- public void receive();
- }
- interface VirusService{
- public void begin();
- public void end();
- }
- class InternetServiceImpl implements InternetService{
- public void connect(){
- System.out.println("开始连接网络....");
- }
- public void disconnect(){
- System.out.println("网络断开....");
- }
- }
- class MailServiceTmpl implements MailService{
- public void send(){
- System.out.println("发送邮箱");
- }
- public void receive(){
- System.out.println("接收邮箱");
- }
- }
- class VirusServiceTmpl implements VirusService{
- public void begin(){
- System.out.println("开始检查病毒");
- }
- public void end(){
- System.out.println(杀毒完毕...");
- }
- }
- interface Menu1 extends InternetService,MailService{
- }
- interface Menu2 extends InternetSerice,VirusService{
- }
- class Menu1Impl implements Menu1{
- private InternetService is;
- private MailService ms;
- public MenuImpl(InternetService is,MailService ms){
- this.is=is;
- tihs.ms=ms;
- }
- public void connect(){
- is.connect();
- }
- public void disconnect(){
- is.disconnect();
- }
- public void send(){
- ms.send();
- }
- public void receive(){
- ms.receive)();
- }
- }
- class Menu2Impl implements Menu2{
- private InternetService is;
- private VirusService vs;
- public Menu2Impl(InternetService is,VirusService vs){
- this.is=is;
- this.vs=vs;
- }
- public void connect(){
- is.connect();
- }
- public void disconnect(){
- is.disconnect();
- }
- public void begin(){
- vs.begin();
- }
- public void end(){
- vs.end();
- }
- }
- public class ServiceDemo{
- public static void main(String[] args){
- InternetService is=new InternetServiceImpl();
- MailService ms=new MailServiceImpl();
- Menu1Impl m1=new MenuImpl(is,ms);
- System.out.println("检测menu1的功能中....");
- m1.connect();
- m1.disconnect();
- m1.send();
- m1.receive();
- }
- }
复制代码 在复合接口设计中,如何将两个系统更完好了进行耦合呢 |
|