Spring及UML

深入浅出UML:http://blog.csdn.net/lovelion/article/details/7843437 

//Component

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by  on 16/10/28.
 5  */
 6 // 所有部件类的父类:所有部件均可实现自检并恢复到初始状态
 7 public abstract class Component {
 8 
 9     String message;
10 
11     abstract void init();
12 
13     abstract void doSelfTest();
14 
15     public Component(String message) {
16         this.message = message;
17     }
18 
19     public Component() {
20     }
21 }
View Code

//Keyboard

1 package umltest.ticketmachine;
2 
3 /**
4  * Created by on 16/10/28.
5  */
6 // 键盘抽象类
7 public abstract class Keyboard extends Component{
8     abstract int getSelectedKey();
9 }
View Code

 //ActionKeyboard

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by  on 16/10/28.
 5  */
 6 // 继续、取消键盘类
 7 public class ActionKeyboard extends Keyboard {
 8 
 9     int inputKey;
10 
11     public ActionKeyboard(int inputKey) {
12         this.inputKey = inputKey;
13     }
14 
15     @Override
16     int getSelectedKey() {
17         return inputKey;
18     }
19 
20     @Override
21     void init() {
22 
23     }
24 
25     @Override
26     void doSelfTest() {
27 
28     }
29 
30     // 继续/取消键盘事件处理
31     void getAction() {
32 
33     }
34 
35 }
View Code

// TicketKindKeyboard

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by on 16/10/28.
 5  */
 6 // 车票种类键盘类
 7 public class TicketKindKeyboard extends Keyboard{
 8     int inputKey;
 9 
10     public TicketKindKeyboard(int inputKey) {
11         this.inputKey = inputKey;
12     }
13 
14     public TicketKindKeyboard() {
15     }
16 
17 
18     @Override
19     int getSelectedKey() {
20         return 0;
21     }
22 
23     @Override
24     void init() {
25 
26     }
27 
28     @Override
29     void doSelfTest() {
30 
31     }
32 
33     // 车票种类键盘事件处理
34     String getTicketKind() {
35         // TODO
36         return String.valueOf(inputKey);
37     }
38 }
View Code

// DestinationKeyboard

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by on 16/10/28.
 5  */
 6 // 目的地键盘类
 7 public class DestinationKeyboard extends Keyboard {
 8 
 9     int inputKey;
10 
11     public DestinationKeyboard(int inputKey) {
12         this.inputKey = inputKey;
13     }
14 
15     public DestinationKeyboard() {
16     }
17 
18     @Override
19     int getSelectedKey() {
20         return inputKey;
21     }
22 
23     @Override
24     void init() {
25         inputKey = 0;
26     }
27 
28     @Override
29     void doSelfTest() {
30 
31     }
32 
33     // 目的地键盘事件处理
34     String getDestinationCode() {
35         return DestinationEnum.getCodeByKey(String.valueOf(getSelectedKey()));
36     }
37 }
View Code

// DestinationEnum

 1 package umltest.ticketmachine;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * Created by on 16/10/28.
 7  */
 8 public enum DestinationEnum {
 9 
10     LISHUI("578", "0578"),
11     WENZHOU("577", "0577"),
12     NINGBO("574", "0574");
13 
14 
15     private String key;
16     private String code;
17 
18     DestinationEnum(String key, String code) {
19         this.key = key;
20         this.code = code;
21     }
22 
23     DestinationEnum() {
24     }
25 
26     public String getKey() {
27         return key;
28     }
29 
30     public void setKey(String key) {
31         this.key = key;
32     }
33 
34     public String getCode() {
35         return code;
36     }
37 
38     public void setCode(String code) {
39         this.code = code;
40     }
41 
42     // 不考虑判空校验
43     public static String getCodeByKey(String key) {
44         for (DestinationEnum destinationEnum : DestinationEnum.values()) {
45             if (destinationEnum.getKey().equalsIgnoreCase(key)) {
46                 return destinationEnum.getCode();
47             }
48         }
49         return null;
50     }
51 
52     public static void main(String[] args) {
53 
54        System.out.println(MockDb.priceMap);
55 
56     }
57 }
View Code

// MockDb

 1 package umltest.ticketmachine;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 /**
 7  * Created by on 16/11/1.
 8  * 定义票价:目的地以及坐席等级,比如目的地是0578,1等座则票价159
 9  */
10 public class MockDb {
11     public static Map<String, Map<String, Integer>> priceMap = new HashMap<>();
12 
13     static {
14         Map<String, Integer> map1 = new HashMap<>();
15         map1.put("1", 159);
16         map1.put("2", 117);
17         priceMap.put("0578", map1);
18 
19         Map<String, Integer> map2 = new HashMap<>();
20         map2.put("1", 72);
21         map2.put("2", 50);
22         priceMap.put("0577", map2);
23 
24         Map<String, Integer> map3 = new HashMap<>();
25         map3.put("1", 232);
26         map3.put("2", 158);
27         priceMap.put("0574", map3);
28 
29     }
30 }
View Code

// Screen  如何通过AOP在不同的处理阶段,显示不同的信息。传递参数?

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by on 16/10/28.
 5  */
 6 // 显示屏类
 7 public class Screen extends Component {
 8     @Override
 9     void init() {
10 
11     }
12 
13     @Override
14     void doSelfTest() {
15 
16     }
17 
18     public Screen(String message) {
19         super(message);
20     }
21 
22     public Screen() {
23     }
24 
25     // 显示信息
26     public void showTextBefore() {
27         // TODO
28         System.out.println("之前");
29     }
30 
31 
32     // 显示信息
33     public void showTextAfter() {
34         // TODO
35         System.out.println("之后");
36     }
37 }
View Code

//Money

1 package umltest.ticketmachine;
2 
3 /**
4  * Created by on 16/11/1.
5  */
6 public interface Money {
7     int getMoney();
8 }
View Code

// CardDriver

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by on 16/10/28.
 5  */
 6 // 卡驱动器类
 7 public class CardDriver extends Component implements Money{
 8     @Override
 9     void init() {
10 
11     }
12 
13     @Override
14     void doSelfTest() {
15 
16     }
17 
18 
19     // 获取卡金额
20     String getCredit() {
21         // TODO
22         return null;
23     }
24 
25 
26 
27     // 更新卡余额
28     double debitFare() {
29         // TODO
30         return 0;
31     }
32 
33     // 吐卡
34     void ejectMCard() {
35 
36     }
37 
38     @Override
39     public int getMoney() {
40         return 0;
41     }
42 }
View Code

// CashSlot

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by on 16/10/28.
 5  */
 6 // 现金(硬币/纸币)槽类
 7 public class CashSlot extends Component implements Money{
 8 
 9     int money;
10 
11     public CashSlot(int money) {
12         this.money = money;
13     }
14 
15     public CashSlot() {
16     }
17 
18     @Override
19     void init() {
20 
21     }
22 
23     @Override
24     void doSelfTest() {
25 
26     }
27 
28     // 获取金额
29     void getCredit() {
30 
31     }
32 
33     @Override
34     public int getMoney() {
35         return money;
36     }
37 }
View Code

// Printer

 1 package umltest.ticketmachine;
 2 
 3 /**
 4  * Created by on 16/10/28.
 5  */
 6 public class Printer extends Component {
 7     @Override
 8     void init() {
 9 
10     }
11 
12     @Override
13     void doSelfTest() {
14 
15     }
16 
17     public Printer(String message) {
18         super(message);
19     }
20 
21     public Printer() {
22     }
23 
24     // 打印车票
25     void printTicket() {
26         System.out.println("正在打印车票...");
27     }
28 
29     //出票
30     void ejectTicket() {
31         System.out.println("出票中,请稍等");
32     }
33 
34 }
View Code

// TicketSoldSystem

 1 package umltest.ticketmachine;
 2 
 3 import java.util.Map;
 4 
 5 /**
 6  * Created by on 16/10/28.
 7  */
 8 // 售票系统类
 9 public class TicketSoldSystem {
10 
11     // 目的地
12     DestinationKeyboard destinationKeyboard;
13     // 座位类别:一等座、二等座等
14     TicketKindKeyboard ticketKindKeyboard;
15     // 增加票数  或者 取消
16     ActionKeyboard actionKeyboard;
17 
18     //
19     Money money;
20 
21     // 打印 局部变量
22     // Printer printer;
23 
24 
25     // 屏幕信息,通过AOP在每一个操作前后显示
26     // Screen screen;
27 
28 
29     public TicketSoldSystem(DestinationKeyboard destinationKeyboard, TicketKindKeyboard ticketKindKeyboard,
30                             ActionKeyboard actionKeyboard, Money money) {
31         this.destinationKeyboard = destinationKeyboard;
32         this.ticketKindKeyboard = ticketKindKeyboard;
33         this.actionKeyboard = actionKeyboard;
34         this.money = money;
35     }
36 
37     // 验证金额
38     boolean verifyCredit() {
39         Map<String, Map<String, Integer>> priceMap = MockDb.priceMap;
40         int singlePrice = priceMap.get(destinationKeyboard.getDestinationCode()).get(ticketKindKeyboard.getTicketKind());
41         int totalPrice = singlePrice * actionKeyboard.getSelectedKey();
42 
43 
44         if (totalPrice == money.getMoney()) {
45             return true;
46         } else {
47             return false;
48         }
49     }
50 
51     // 计算费用
52     void calculateFare() {
53         // 验证金额
54         if (verifyCredit()) {
55             // 打印车票
56             Printer printer = new Printer();
57             printer.printTicket();
58             printer.ejectTicket();
59         }
60 
61     }
62 
63     public static void main(String[] args) {
64 
65     }
66 }

// MainStart

 1 package umltest.ticketmachine;
 2 
 3 import org.springframework.context.support.ClassPathXmlApplicationContext;
 4 
 5 /**
 6  * Created by on 16/11/1.
 7  */
 8 public class MainStart {
 9     public static void main(String[] args) {
10         // 从应用的classpath目录载入Spring配置文件
11         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean/ticketsystem.xml");
12         TicketSoldSystem ticketSoldSystem = context.getBean(TicketSoldSystem.class);
13 
14         ticketSoldSystem.calculateFare();
15     }
16 }

// ticketsystem.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 5 
 6        <bean id="destinationKeyboard" class="umltest.ticketmachine.DestinationKeyboard">
 7               <constructor-arg value="578"/>
 8        </bean>
 9 
10        <bean id="ticketKindKeyboard" class="umltest.ticketmachine.TicketKindKeyboard">
11               <constructor-arg value="1"/>
12        </bean>
13 
14        <bean id="actionKeyboard" class="umltest.ticketmachine.ActionKeyboard">
15               <constructor-arg value="2"/>
16        </bean>
17 
18        <bean id="money" class="umltest.ticketmachine.CashSlot">
19               <constructor-arg value="318"/>
20        </bean>
21 
22        <bean id="ticketSoldSystem" class="umltest.ticketmachine.TicketSoldSystem">
23               <constructor-arg name="destinationKeyboard" ref="destinationKeyboard" />
24               <constructor-arg name="ticketKindKeyboard" ref="ticketKindKeyboard" />
25               <constructor-arg name="actionKeyboard" ref="actionKeyboard" />
26               <constructor-arg name="money" ref="money" />
27        </bean>
28 
29 
30        <bean id="screen" class="umltest.ticketmachine.Screen">
31               <constructor-arg value="318"/>
32        </bean>
33 
34        <aop:config>
35               <aop:aspect ref="screen">
36                      <aop:pointcut id="ticketmach" expression="execution(* umltest.ticketmachine.*.*(..)))"/>
37                      <aop:before method="showTextBefore"  pointcut-ref="ticketmach" />
38                      <aop:after method="showTextAfter" pointcut-ref="ticketmach" />
39               </aop:aspect>
40        </aop:config>
41 
42 
43 </beans>
原文地址:https://www.cnblogs.com/wxdlut/p/6022405.html