深入浅出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 }

//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 }

//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 }

// 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 }

// 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 }

// 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 }

// 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 }

// 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 }

//Money

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

// 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 }

// 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 }

// 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 }

// 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>

Spring及UML的更多相关文章

  1. spring源码:学习线索(li)

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  2. 浅谈对Spring Framework的认识

    Spring Framework,作为一个应用框架,官方的介绍如下: The Spring Framework provides a comprehensive programming and con ...

  3. Spring 000 框架简介 (转载)

    转载自:https://my.oschina.net/myriads/blog/37922 1.使用框架的意义与Spring的主要内容 随着软件结构的日益庞大,软件模块化趋势出现,软件开发也需要多人合 ...

  4. Spring任务调度器之Task的使用

    Spring Task提供两种方式进行配置,正如大家所想吧,还是一种是annotation(标注),而另外一种就是XML配置了.但其实这里我觉得比较尴尬,因为任务调度这样的需求,通常改动都是比较多的, ...

  5. spring源码分析(一)IoC、DI

    创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...

  6. 【Spring】Spring AOP实现原理

    Spring AOP实现原理 在之前的一文中介绍过Spring AOP的功能使用,但是没有深究AOP的实现原理,今天正好看到几篇好文,于是就自己整理了一下AOP实现的几种方式,同时把代理模式相关知识也 ...

  7. spring入门(二)【加载properties文件】

    在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...

  8. 形象化的spring 依赖注入原理

      转. IoC就是Inversion of Control,控制反转.在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制.这称为控制反转. 下面我们以几个例子来说明什 ...

  9. 设计模式——Spring IoC中用到的模板方法模式

    基本概念 什么是模板方法(Template method):父类定义了骨架(调用哪些方法及顺序),某些特定方法由子类实现. 最大的好处:代码复用,减少重复代码.除了子类要实现的特定方法,其他方法及方法 ...

  10. spring源码学习之路---AOP初探(六)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 最近工作很忙,但当初打算学习 ...

随机推荐

  1. MIT 6.5840 Raft Implementation(2A, Leader Election)

    Raft实现思路+细节 2A 任务分解 总体来说,2A中主要的任务就是选出领导人,在选出领导人的时候,我们要遵循下图. 在2A中,由于并没有出现日志复制,所以我们只需要考察两者的任期是否相等,以及接收 ...

  2. 输入平方米的三种方式㎡ m2 m²

    如何在Word中输入平方米字符? 第1种方法 Win10自带输入法,输入"平方米",默认第5个就出来了㎡,也可以直接复制使用. 这种方式最直接,字母m和右上角的2是1个字符,所以不 ...

  3. [mysql]状态检查常用SQL

    前言 使用MySQL自身命令获取数据库服务状态. 连接数 -- 最大使用连接数 show status like 'Max_used_connections'; -- 系统配置的最大连接数 show ...

  4. CentOS安装ffmpeg并转码视频为mp4

    前言 现需要将一批avi格式的视频转码为mp4,以下为操作步骤.系统版本为CentOS 7. 如果不安装x264,转码后只有声音,没有视频. 编译安装nasm wget https://www.nas ...

  5. 文心一言 VS 讯飞星火 VS chatgpt (73)-- 算法导论7.1 4题

    四.如何修改 QUICKSORT,使得它能够以非递增序进行排序? 文心一言: 快速排序(Quicksort)是一种常用的排序算法,基于分治策略进行设计.默认情况下,快速排序会以递增序进行排序.若想修改 ...

  6. 高性能MySQL实战(一):表结构

    最近因需求改动新增了一些数据库表,但是在定义表结构时,具体列属性的选择有些不知其所以然,索引的添加也有遗漏和不规范的地方,所以我打算为创建一个高性能表的过程以实战的形式写一个专题,以此来学习和巩固这些 ...

  7. ArcMap用一个面要素擦除另一个面要素的部分

      本文介绍在ArcMap软件中,基于擦除("Erase")工具,对矢量面要素的部分区域加以剔除的操作.   假如我们已知这样一个研究区域,其包括了陆地与水体两个部分.   与此同 ...

  8. 《SQL与数据库基础》23. 读写分离

    目录 读写分离 一主一从 准备 配置 双主双从 准备 配置 主库配置 从库配置 从库关联主库 主库相互复制 双主双从读写分离 本文以 MySQL 为例.以 MyCat 数据库中间件为例,通过 MyCa ...

  9. vue3探索——组件通信之依赖注入

    背景 通常情况下,当我们需要从父组件向子组件传递数据时,会使用 props.想象一下这样的结构:有一些多层级嵌套的组件,形成了一颗巨大的组件树,而某个深层的子组件需要一个较远的祖先组件中的部分数据.在 ...

  10. 程序员视角下的API数据接口对接指南

    ​ 在当今互联网时代,许多应用程序和网站都需要使用第三方的API接口来获取数据.API(Application Programming Interface)允许不同的应用程序之间进行数据交互,从而提高 ...