首页 > 编程语言 > 详细

Java Web设计模式之依赖倒换原则

时间:2014-07-19 09:24:34      阅读:360      评论:0      收藏:0      [点我收藏+]

1.依赖倒置原则

  A.高层次的模块不应该依赖于低层次的模块,他们都应该依赖于抽象。
  B.抽象不应该依赖于具体,具体应该依赖于抽象。
2.用UML图来说明一下:
bubuko.com,布布扣
 
 
代码说明:
(1)管理员接口
bubuko.com,布布扣
1 package com.alibaba.com.miao;
2 
3 public interface IEmployee {
4 
5     public String code(ICode code);
6 }
View Code

(2)编码接口

bubuko.com,布布扣
1 package com.alibaba.com.miao;
2 
3 public interface ICode {
4 
5     public String getRun(String Run);
6 }
View Code

(3)编码类

bubuko.com,布布扣
 1 package com.alibaba.com.miao;
 2 
 3 public class Code implements IEmployee {
 4 
 5     private static final String Run = "编码活动";
 6 
 7     @Override
 8     public String code(ICode code) {
 9 
10         System.out.println(code + "程序员开始编码...");
11 
12         return code.getRun(Run);
13     }
14 }
View Code

(4)普通程序员

bubuko.com,布布扣
 1 package com.alibaba.com.miao;
 2 
 3 public class CommonProgrammer implements ICode {
 4 
 5     private static final String Run = "JACK";
 6 
 7     @Override
 8     public String getRun(String Run1) {
 9         String Run2 = "工作";
10         System.out.println(Run + " 普通程序员开始工作");
11         return Run2;
12     }
13 
14 }
View Code
(5)高级程序员
bubuko.com,布布扣
 1 package com.alibaba.com.miao;
 2 
 3 public class SeniorProgrammer implements ICode {
 4 
 5     private static final String Run = "李四";
 6 
 7     @Override
 8     public String getRun(String Run1) {
 9         String Run2 = "工作";
10         return Run2;
11     }
12 
13 }
View Code
(6)测试类
bubuko.com,布布扣
 1 <%@page import="com.alibaba.com.miao.ICode"%>
 2 <%@page import="com.alibaba.com.miao.CommonProgrammer"%>
 3 <%@page import="com.alibaba.com.miao.SeniorProgrammer"%>
 4 <%@page import="com.alibaba.com.miao.Code"%>
 5 <%@page import="com.alibaba.com.miao.IEmployee"%>
 6 <%@ page language="java" contentType="text/html; charset=UTF-8"
 7     pageEncoding="UTF-8"%>
 8 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 9 <html>
10 <head>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>单一职责原则</title>
13 </head>
14 <body>
15 
16     <%
17         IEmployee employee = new Code();
18 
19         ICode code = new SeniorProgrammer();
20         ICode code1 = new CommonProgrammer();
21 
22         out.println(" " + employee.code(code));
23         out.println("管理人员派张三高级程序员" + employee.code(code1));
24     %>
25 
26 </body>
27 </html>
View Code

(7)参考目录结构

bubuko.com,布布扣

   

参考:Java Web设计模式之道。

Java Web设计模式之依赖倒换原则,布布扣,bubuko.com

Java Web设计模式之依赖倒换原则

原文:http://www.cnblogs.com/sxmcACM/p/3854285.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!