402
00:17:30,860 --> 00:17:34,610
那首先要说在前面的,就是说
403
00:17:36,590 --> 00:17:39,770
这个状态机实际上已经是一种
404
00:17:42,060 --> 00:17:45,320
描述行为上的差别了
405
00:17:45,820 --> 00:17:47,860
就是说,实际上它是说什么
406
00:17:48,140 --> 00:17:49,140
一个
407
00:17:49,420 --> 00:17:51,110
事件进来
408
00:17:51,390 --> 00:17:56,890
对吧,然后最终你是执行了哪些action,输出就是action嘛
409
00:17:57,170 --> 00:17:59,450
哪些action是你要做的
410
00:17:59,970 --> 00:18:00,630
411
00:18:03,500 --> 00:18:04,450
也就是说
412
00:18:05,150 --> 00:18:06,540
它的区别
413
00:18:06,930 --> 00:18:09,350
为什么要通过状态机来判断
414
00:18:09,350 --> 00:18:11,570
因为这个action1,action2
415
00:18:12,120 --> 00:18:13,770
action3等等
416
00:18:14,170 --> 00:18:15,520
是不一样的
417
00:18:19,380 --> 00:18:21,460
那么这已经是一种
418
00:18:21,780 --> 00:18:24,260
行为上的差别
419
00:18:25,590 --> 00:18:28,040
就是把差别写在行为里面
420
00:18:28,380 --> 00:18:30,512
那我们在第八章的时候
421
00:18:30,512 --> 00:18:31,364
软件方法
422
00:18:31,364 --> 00:18:33,710
第八章的时候已经说了
423
00:18:35,230 --> 00:18:41,932
这张幻灯片就是从分析设计高阶那个裁过来的
424
00:18:41,932 --> 00:18:43,050
截过来的
425
00:18:44,860 --> 00:18:46,360
说类图的时候
426
00:18:46,850 --> 00:18:48,010
的幻灯片
427
00:18:48,490 --> 00:18:50,960
如果能通过
428
00:18:52,170 --> 00:18:53,550
属性值的差别
429
00:18:53,550 --> 00:18:55,850
也就是说,通过关联
430
00:18:56,920 --> 00:18:58,640
来显式建模的
431
00:19:00,810 --> 00:19:02,767
那就尽量变成关联
432
00:19:02,767 --> 00:19:05,160
包括属性也是一种关联
433
00:19:07,230 --> 00:19:07,980
那实际上
434
00:19:07,980 --> 00:19:08,730
就是
435
00:19:10,020 --> 00:19:11,875
在类图那个地方也说过
436
00:19:11,875 --> 00:19:13,730
就是说,实际上就是一种
437
00:19:14,100 --> 00:19:16,230
设计模式里面说的那个
438
00:19:16,840 --> 00:19:19,030
组合优于继承
439
00:19:21,780 --> 00:19:22,640
那个
440
00:19:23,290 --> 00:19:23,592
441
00:19:23,592 --> 00:19:24,500
就是说
442
00:19:24,780 --> 00:19:26,300
属性的差别
443
00:19:26,970 --> 00:19:28,309
能解决的
444
00:19:28,309 --> 00:19:32,060
就不要拖到行为上的差别
445
00:19:36,930 --> 00:19:41,707
当然,像设计模式里面说什么组合优于继承
446
00:19:41,707 --> 00:19:45,447
它本质上并没有解决这个行为差别的问题
447
00:19:45,447 --> 00:19:47,108
只是把它推迟了
448
00:19:47,108 --> 00:19:50,224
比如说,变成一个什么策略模式
449
00:19:50,224 --> 00:19:52,509
但实际上后面还是泛化
450
00:19:52,509 --> 00:19:57,080
行为差别还是在策略模式那里面写代码
451
00:19:57,360 --> 00:19:59,749
没有把背后的道理
452
00:19:59,749 --> 00:20:01,900
变成属性的差别
453
00:20:04,090 --> 00:20:07,190
所以我们在第八章的时候也说了嘛
454
00:20:07,190 --> 00:20:08,170
很多那个什么
455
00:20:09,170 --> 00:20:10,192
设计模式
456
00:20:10,192 --> 00:20:12,646
策略模式什么的
457
00:20:12,646 --> 00:20:15,918
表面上好像做了事情,当然也有改善
458
00:20:15,918 --> 00:20:17,350
可以随机的
459
00:20:17,660 --> 00:20:19,116
随时切换策略
460
00:20:19,116 --> 00:20:19,480
对吧
461
00:20:19,480 --> 00:20:21,848
通过把策略分离出去
462
00:20:21,848 --> 00:20:22,576
随时切换
463
00:20:22,576 --> 00:20:23,669
但是还是什么
464
00:20:23,669 --> 00:20:25,490
实际上还是面向过程的
465
00:20:29,330 --> 00:20:31,418
还是在行为上的差别
466
00:20:31,418 --> 00:20:33,090
并没有本质的变化
467
00:20:33,560 --> 00:20:36,420
那属性值差别就像这个了
468
00:20:36,860 --> 00:20:37,505
人
469
00:20:37,505 --> 00:20:40,730
假设我要有一个计税的
470
00:20:41,440 --> 00:20:42,660
一个行为
471
00:20:43,820 --> 00:20:44,938
那个行为的话
472
00:20:44,938 --> 00:20:46,430
它是跟收入有关的
473
00:20:49,430 --> 00:20:52,881
男人的话可能是收入乘
474
00:20:52,881 --> 00:20:55,100
0.1,10%
475
00:20:55,550 --> 00:20:58,170
女人的话,9%,0.09
476
00:20:59,230 --> 00:21:00,416
那有两种做法
477
00:21:00,416 --> 00:21:01,800
一个是泛化了
478
00:21:01,800 --> 00:21:02,789
男人,计税
479
00:21:02,789 --> 00:21:03,580
然后
480
00:21:03,580 --> 00:21:05,360
0.1,这个,0.09
481
00:21:06,370 --> 00:21:08,453
就是写在代码里面了
482
00:21:08,453 --> 00:21:09,703
在这里有区别
483
00:21:09,703 --> 00:21:11,370
但显然这是不好的
484
00:21:11,370 --> 00:21:13,246
你看这0.1,0.09
485
00:21:13,246 --> 00:21:13,454
486
00:21:13,454 --> 00:21:14,913
这是一个硬编码
487
00:21:14,913 --> 00:21:15,330
对吧
488
00:21:21,360 --> 00:21:23,110
那我们把它变成概念
489
00:21:25,070 --> 00:21:26,290
把硬编码
490
00:21:27,090 --> 00:21:28,527
拿出来,变成概念
491
00:21:28,527 --> 00:21:29,605
这个是什么
492
00:21:29,605 --> 00:21:30,323
是税率嘛
493
00:21:30,323 --> 00:21:31,761
跟性别相关的税率
494
00:21:31,761 --> 00:21:32,300
对不对
495
00:21:33,250 --> 00:21:34,860
人有性别
496
00:21:34,860 --> 00:21:36,930
每个性别有一个税率
497
00:21:36,930 --> 00:21:38,310
那代码怎么写
498
00:21:38,310 --> 00:21:41,760
就是税费等于收入乘以性别的税率
499
00:21:42,870 --> 00:21:45,467
通过这些组合就可以计算出来
500
00:21:45,467 --> 00:21:46,580
就是通过这个
501
00:21:47,290 --> 00:21:48,480
这些运算
502
00:21:49,370 --> 00:21:50,570
就可以把它
503
00:21:51,580 --> 00:21:52,780
运算出来
504
00:21:54,230 --> 00:21:55,056
就不用泛化
505
00:21:55,056 --> 00:21:58,030
也不用什么if else
506
00:21:59,440 --> 00:22:00,760
也就是说
507
00:22:03,500 --> 00:22:06,649
能在属性这个级别上解决的
508
00:22:06,649 --> 00:22:08,830
就在属性级别上解决
509
00:22:10,250 --> 00:22:11,600
实在不行了
510
00:22:11,600 --> 00:22:14,300
在行为这个层面上解决
511
00:22:15,420 --> 00:22:19,300
那状态已经是行为解决的一种方式了
512
00:22:23,920 --> 00:22:26,380
那如果在行为上解决的话
513
00:22:27,310 --> 00:22:29,460
有这么几种方式嘛
514
00:22:30,000 --> 00:22:32,640
来表达行为上差别
515
00:22:33,730 --> 00:22:36,090
比如说,条件分支
516
00:22:36,960 --> 00:22:39,740
if else或switch case都行
517
00:22:41,380 --> 00:22:44,000
如果仅仅是一个条件
518
00:22:45,670 --> 00:22:47,260
那这个问题不大
519
00:22:49,810 --> 00:22:51,670
但是往往是什么
520
00:22:51,670 --> 00:22:54,328
我们的系统之所以复杂
521
00:22:54,328 --> 00:22:54,860
就是
522
00:22:55,320 --> 00:22:58,170
如果你用条件分支来解决的话
523
00:22:58,950 --> 00:23:00,460
它很可能是
524
00:23:00,920 --> 00:23:02,030
多个嵌套
525
00:23:02,310 --> 00:23:04,568
要么是多层嵌套
526
00:23:04,568 --> 00:23:05,070
if
527
00:23:07,450 --> 00:23:07,748
528
00:23:07,748 --> 00:23:08,644
然后呢
529
00:23:08,644 --> 00:23:11,930
else,else下面还可以加if
530
00:23:13,380 --> 00:23:13,609
531
00:23:13,609 --> 00:23:17,740
然后在else下面还可以再if,多层嵌套的
532
00:23:20,550 --> 00:23:22,180
比如说,像右边这个
533
00:23:22,960 --> 00:23:26,481
它这个变量倒是简单,就一个
534
00:23:26,481 --> 00:23:31,010
这就是一个典型的面试题嘛
535
00:23:31,500 --> 00:23:33,240
3的时候,输出什么
536
00:23:33,240 --> 00:23:34,980
5的时候,输出什么,
537
00:23:35,260 --> 00:23:36,920
但是你看它这个
538
00:23:37,390 --> 00:23:40,000
首先,你要判断
539
00:23:41,100 --> 00:23:43,500
3而且是5
540
00:23:44,000 --> 00:23:46,934
如果这个表达式
541
00:23:46,934 --> 00:23:47,260
是
542
00:23:48,130 --> 00:23:49,190
假的
543
00:23:49,840 --> 00:23:51,570
那就有两种可能嘛
544
00:23:51,570 --> 00:23:52,220
要么是
545
00:23:53,530 --> 00:23:53,712
546
00:23:53,712 --> 00:23:55,358
就有三种可能
547
00:23:55,358 --> 00:23:56,455
要么不是3
548
00:23:56,455 --> 00:23:57,370
要么不是5
549
00:23:57,370 --> 00:23:58,650
要么两个都不是
550
00:23:58,930 --> 00:23:59,547
对吧
551
00:23:59,547 --> 00:24:01,400
而每种的话
552
00:24:02,710 --> 00:24:04,710
输出都是不一样的
553
00:24:11,120 --> 00:24:12,180
那这个
554
00:24:13,710 --> 00:24:14,490
就是
555
00:24:15,310 --> 00:24:16,400
多个的
556
00:24:16,680 --> 00:24:17,370
557
00:24:17,970 --> 00:24:20,130
条件,嵌套的条件
558
00:24:21,730 --> 00:24:23,450
那更复杂的就是
559
00:24:23,730 --> 00:24:25,863
如果它是多个变量的呢
560
00:24:25,863 --> 00:24:26,290
对吧