黑马程序员技术交流社区

标题: 定制服务设计模式的问题 [打印本页]

作者: jonn    时间: 2013-1-15 19:08
标题: 定制服务设计模式的问题
  1. interface InternetService{
  2. public void connect();
  3. public void disconnect();
  4. }
  5. interface MailService{
  6. public void send();
  7. public void receive();
  8. }
  9. interface VirusService{
  10. public void begin();
  11. public void end();
  12. }
  13. class InternetServiceImpl implements InternetService{
  14. public void connect(){
  15. System.out.println("开始连接网络....");
  16. }
  17. public void disconnect(){
  18. System.out.println("网络断开....");
  19. }
  20. }
  21. class MailServiceTmpl implements MailService{
  22. public void send(){
  23. System.out.println("发送邮箱");
  24. }
  25. public void receive(){
  26. System.out.println("接收邮箱");
  27. }
  28. }
  29. class VirusServiceTmpl implements VirusService{
  30. public void begin(){
  31. System.out.println("开始检查病毒");
  32. }
  33. public void end(){
  34. System.out.println(杀毒完毕...");
  35. }
  36. }
  37. interface Menu1 extends InternetService,MailService{

  38. }
  39. interface Menu2 extends InternetSerice,VirusService{
  40. }
  41. class Menu1Impl implements Menu1{
  42. private InternetService is;
  43. private MailService ms;
  44. public MenuImpl(InternetService is,MailService ms){
  45. this.is=is;
  46. tihs.ms=ms;
  47. }
  48. public void connect(){
  49. is.connect();
  50. }
  51. public void disconnect(){
  52. is.disconnect();
  53. }
  54. public void send(){
  55. ms.send();
  56. }
  57. public void receive(){
  58. ms.receive)();
  59. }
  60. }
  61. class Menu2Impl implements Menu2{
  62. private InternetService is;
  63. private VirusService vs;
  64. public Menu2Impl(InternetService is,VirusService vs){
  65. this.is=is;
  66. this.vs=vs;
  67. }
  68. public void connect(){
  69. is.connect();
  70. }
  71. public void disconnect(){
  72. is.disconnect();
  73. }
  74. public void begin(){
  75. vs.begin();
  76. }
  77. public void end(){
  78. vs.end();
  79. }
  80. }
  81. public class ServiceDemo{
  82. public static void main(String[] args){
  83. InternetService is=new InternetServiceImpl();
  84. MailService ms=new MailServiceImpl();
  85. Menu1Impl m1=new MenuImpl(is,ms);
  86. System.out.println("检测menu1的功能中....");
  87. m1.connect();
  88. m1.disconnect();
  89. m1.send();
  90. m1.receive();
  91. }
  92. }
复制代码
在复合接口设计中,如何将两个系统更完好了进行耦合呢




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2