-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
1307 lines (1065 loc) · 72.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
<meta charset="utf-8">
<title>一位有匠心的手艺人</title>
<meta name="author" content="bingo">
<meta name="description" content="摘要 本文译自Fast Fibonacci 斐波那契数列在诸多方面,从头状花序到通用编码(如禁忌编码)表现出种种有趣的性质。研究高效计算斐氏数列的过程充满乐趣。 斐波那契数列的经典公式表示为如下递推形式: % <![CDATA[ F_n=\begin{cases} 1 & \mathrm{if …">
<!-- http://t.co/dKP3o1e -->
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="canonical" href="http://guevara.github.io">
<link href="/favicon.png" rel="icon">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
<link href="/atom.xml" rel="alternate" title="一位有匠心的手艺人" type="application/atom+xml">
<script src="/javascripts/modernizr-2.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>!window.jQuery && document.write(unescape('%3Cscript src="./javascripts/lib/jquery.min.js"%3E%3C/script%3E'))</script>
<script src="/javascripts/octopress.js" type="text/javascript"></script>
<!--Fonts from Google"s Web font directory at http://google.com/webfonts -->
<link href="http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic" rel="stylesheet" type="text/css">
<!-- mathjax config similar to math.stackexchange -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script type="text/javascript"
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-50645712-1']);
_gaq.push(['_setDomainName', 'github.io']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body >
<header role="banner"><hgroup>
<h1><a href="/">一位有匠心的手艺人</a></h1>
<h2>和他的生活方式</h2>
</hgroup>
</header>
<nav role="navigation"><ul class="subscription" data-subscription="rss email">
<li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
<li><a href="bingo.chern@gmail.com" rel="subscribe-email" title="subscribe via email">Email</a></li>
</ul>
<form action="http://google.com/search" method="get">
<fieldset role="search">
<input type="hidden" name="q" value="site:guevara.github.io" />
<input class="search" type="text" name="q" results="0" placeholder="Search"/>
</fieldset>
</form>
<ul class="main-navigation">
<li><a href="/">博客</a></li>
<li><a href="/blog/archives">归档</a></li>
<li><a href="/about">关于</a></li>
</ul>
</nav>
<div id="main">
<div id="content">
<div class="blog-index">
<article>
<header>
<h1 class="entry-title"><a href="/blog/2014/05/03/fast-fibonacci/">[译]快速斐波那契算法</a></h1>
<p class="meta">
<time datetime="2014-05-03T08:55:00+08:00" pubdate data-updated="true">May 3<span>rd</span>, 2014</time>
| <a href="/blog/2014/05/03/fast-fibonacci/#disqus_thread">Comments</a>
</p>
</header>
<div class="entry-content"><div class="note info">
<h5>摘要</h5>
<p>本文译自<a href="http://hbfs.wordpress.com/2012/05/22/fast-fibonacci/" target="_blank" title="Fast Fibonacci">Fast Fibonacci</a></p>
</div>
<p><a href="http://en.wikipedia.org/wiki/Fibonacci_number">斐波那契数列</a>在诸多方面,从<a href="http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibnat.html#seeds">头状花序</a>到<a href="http://en.wikipedia.org/wiki/Universal_code_%28data_compression%29">通用编码</a>(如<a href="http://books.google.ca/books?id=81AfzW6vux4C&pg=PA100&lpg=PA100&dq=%22taboo+codes%22&source=bl&ots=PVUooaK7Fm&sig=hdBqPpvYlRNWskAMrVawTO6i_k4&hl=en&sa=X&ei=P0WyT9qmLsqIgwfa34m7CQ&redir_esc=y#v=onepage&q=%22taboo%20codes%22&f=false">禁忌编码</a>)表现出种种有趣的性质。研究高效计算斐氏数列的过程充满乐趣。</p>
<p>斐波那契数列的经典公式表示为如下递推形式:</p>
<script type="math/tex; mode=display">% <![CDATA[
F_n=\begin{cases}
1 & \mathrm{if~}n\leqslant{2} \\
F_{n-1}+F_{n-2} &\mathrm{otherwise}
\end{cases}
%]]></script>
<p>由此引出其直观(朴素)的递归实现:</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">uint64_t</span> <span class="nf">fibo_rec</span><span class="p">(</span><span class="kt">uint64_t</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o"><=</span> <span class="mi">2</span><span class="p">)</span>
</span><span class="line"> <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"> <span class="k">else</span>
</span><span class="line"> <span class="k">return</span> <span class="n">fibo_rec</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">fibo_rec</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">2</span><span class="p">);</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>看上去很完美,可是计算<script type="math/tex">F_n</script>的过程中产生了<script type="math/tex">O(F_n)</script>次递归调用!运行时间呈指数增长(因为<script type="math/tex">F_n \approx \phi^n</script>,其中<script type="math/tex">\phi=\frac{1}{2}(1+\sqrt{5})</script>为黄金比例)。</p>
<p>通过显式地优化尾递可以消除递归调用:</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">uint64_t</span> <span class="nf">fibo_iter</span><span class="p">(</span><span class="kt">uint64_t</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">n_1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n_2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="k">for</span> <span class="p">(</span><span class="kt">uint64_t</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">t</span> <span class="o">=</span> <span class="n">n_1</span> <span class="o">+</span> <span class="n">n_2</span><span class="p">;</span>
</span><span class="line"> <span class="n">n_2</span> <span class="o">=</span> <span class="n">n_1</span><span class="p">;</span>
</span><span class="line"> <span class="n">n_1</span> <span class="o">=</span> <span class="n">t</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line">
</span><span class="line"> <span class="k">return</span> <span class="n">n_1</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>这次计算<script type="math/tex">F_n</script>的时间复杂度下降到<script type="math/tex">O(n)</script>,大大优于原始递归版本。迭代版本借助临时变量保存和,并使用移位寄存器将后继的<strong>斐波那契数列</strong>分别保存到<code>n_2</code>和<code>n_1</code>。有些人可能会忌讳临时变量,那么可以将上述代码重写为: </p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">uint64_t</span> <span class="nf">fibo_iter_var</span><span class="p">(</span><span class="kt">uint64_t</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">n_1</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n_2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="k">for</span> <span class="p">(</span><span class="kt">uint64_t</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="n">n_1</span> <span class="o">=</span> <span class="n">n_1</span> <span class="o">+</span> <span class="n">n_2</span><span class="p">;</span>
</span><span class="line"> <span class="n">n_2</span> <span class="o">=</span> <span class="n">n_1</span> <span class="o">-</span> <span class="n">n_2</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line">
</span><span class="line"> <span class="k">return</span> <span class="n">n_1</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p><code>n_2 = n_1 - n_2</code>部分化简得<code>n_1</code>(<script type="math/tex">n_2 \gets n_1 - n_2</script>,即<script type="math/tex">n_2 \gets (n_1 + n_2) - n_2</script>,结果为<script type="math/tex">n_2 \gets n_1</script>)。此处我们通过额外操作抵消掉临时变量,好坏别当别论。</p>
<center>⁂</center>
<p>是否有进一步优化的空间?根据以往的经验,线性时间似乎是最好的结果。无独有偶,有些聪明的家伙发现矩阵</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
0 & 1 \\
1 & 1
\end{array}
\right]
%]]></script>
<p>具有“平移”并增加一个矢量分量的性质。比如</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
0 & 1 \\
1 & 1
\end{array}
\right]
\left[
\begin{array}{c}
a\\
b
\end{array}
\right]
=
\left[
\begin{array}{cc}
b\\
a + b
\end{array}
\right]
%]]></script>
<p>看上去有几分眼熟吧。事实上我们将<code>a</code>代换为<script type="math/tex">F_ {n-2}</script>,<code>b</code>代换为<script type="math/tex">F_ {n-1}</script>,得</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
0 & 1\\
1 & 1
\end{array}
\right]
\left[
\begin{array}{cc}
F_{n-2}\\
F_{n-1}
\end{array}
\right]
=
\left[
\begin{array}{cc}
F_{n-2}\\
F_{n-1} + F_{n-2}
\end{array}
\right]
=
\left[
\begin{array}{c}
F_{n-1}\\
F_n
\end{array}
\right]
%]]></script>
<p>有点眉目了!但这是基于已知<script type="math/tex">F_ {n-1}</script>和<script type="math/tex">F_ {n-2}</script>的前提下,一旦成立意味着存在(递归)分解形式:</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
0 & 1\\
1 & 1
\end{array}
\right]
\left[
\begin{array}{cc}
F_{n-2}\\
F_{n-1}
\end{array}
\right]
=
\left[
\begin{array}{cc}
0 & 1\\
1 & 1
\end{array}
\right]
\left[
\begin{array}{cc}
0 & 1\\
1 & 1
\end{array}
\right]
\left[
\begin{array}{cc}
F_{n-3}\\
F_{n-2}
\end{array}
\right]
%]]></script>
<p>反复如此迭代到初始条件<script type="math/tex">F_1 = 1</script>和<script type="math/tex">F_2 = 1</script>,那么有</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
0 & 1\\
1 & 1
\end{array}
\right]^n
\left[
\begin{array}{c}
1\\
1
\end{array}
\right]
=
\left[
\begin{array}{c}
F_{n-1}\\
F_{n}
\end{array}
\right]
%]]></script>
<p>矩阵运算似乎代价不菲,至少要进行线性次数的矩阵乘法。幸运的是不需要那么多次,我们有办法在<script type="math/tex">O(\lg n)</script>时间复杂度内计算出<script type="math/tex">A^n</script>(此处矩阵维数为常数<script type="math/tex">2 \times{2}</script>,故可视矩阵乘积为常量)。如何计算呢?且观察式子<script type="math/tex">x^5 = (x^2)^2x</script>和<script type="math/tex">x^7 =((x^2)x)^2x</script>。整数情况时<script type="math/tex">x^n</script>计算过程如下:</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">int</span> <span class="nf">expo</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="kt">int</span> <span class="n">t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"> <span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">;</span>
</span><span class="line"> <span class="k">while</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&</span> <span class="mi">1</span><span class="p">)</span>
</span><span class="line"> <span class="n">t</span> <span class="o">*=</span> <span class="n">y</span><span class="p">;</span>
</span><span class="line"> <span class="n">y</span> <span class="o">*=</span> <span class="n">y</span><span class="p">;</span>
</span><span class="line"> <span class="n">n</span> <span class="o">>>=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line"> <span class="k">return</span> <span class="n">t</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>矩阵计算方式同理可得。以上代码中<code>t</code>为乘积当前值,<code>y</code>为乘积“平方”。给定两个矩阵</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a_t & b_t \\
c_t & d_t
\end{array}
\right]
%]]></script>
<p>和</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a_y & b_y \\
c_y & d_y
\end{array}
\right]
%]]></script>
<p>第一个初始化为单位阵,第二个初始化为斐波那契矩阵。根据矩阵的对称性,<script type="math/tex">c_t</script>和<script type="math/tex">c_y</script>各自恒等于<script type="math/tex">b_t</script>和<script type="math/tex">b_y</script>,这两个变量可以分别消掉。</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a_t & b_t \\
c_t & d_t
\end{array}
\right]
\left[
\begin{array}{cc}
a_y & b_y \\
c_y & d_y
\end{array}
\right]
=
\left[
\begin{array}{cc}
a_t a_y + b_t b_y & a_t b_y + b_t d_y\\
--- & b_t b_y + d_t d_y
\end{array}
\right]
%]]></script>
<p>和</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a_y & b_y \\
c_y & d_y
\end{array}
\right]^2
=
\left[
\begin{array}{cc}
a_y^2 + b_y^2 & b_y(a_y + d_y)\\
--- & b_y^2 + d_y^2
\end{array}
\right]
%]]></script>
<p>其中虚线位置表示对称部分。将上述式子代入幂函数,将有</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">uint64_t</span> <span class="nf">fibo_expo</span><span class="p">(</span><span class="kt">uint64_t</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">a_t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b_t</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">d_t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// "1"</span>
</span><span class="line"> <span class="n">a_y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">b_y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">d_y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">// "y"</span>
</span><span class="line">
</span><span class="line"> <span class="k">while</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&</span> <span class="mi">1</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="c1">// t*=y;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">a</span> <span class="o">=</span> <span class="n">a_t</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">b_y</span><span class="p">;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">b</span> <span class="o">=</span> <span class="n">a_t</span> <span class="o">*</span> <span class="n">b_y</span> <span class="o">+</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">d_y</span><span class="p">;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">d</span> <span class="o">=</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">b_y</span> <span class="o">+</span> <span class="n">d_t</span> <span class="o">*</span> <span class="n">d_y</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="n">a_t</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span>
</span><span class="line"> <span class="n">b_t</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
</span><span class="line"> <span class="n">d_t</span> <span class="o">=</span> <span class="n">d</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line">
</span><span class="line"> <span class="c1">//y*=y;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">a</span> <span class="o">=</span> <span class="n">a_y</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">b_y</span> <span class="o">*</span> <span class="n">b_y</span><span class="p">;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b_y</span> <span class="o">*</span> <span class="p">(</span><span class="n">a_y</span> <span class="o">+</span> <span class="n">d_y</span><span class="p">);</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">d</span> <span class="o">=</span> <span class="n">b_y</span> <span class="o">*</span> <span class="n">b_y</span> <span class="o">+</span> <span class="n">d_y</span> <span class="o">*</span> <span class="n">d_y</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="n">a_y</span> <span class="o">=</span> <span class="n">a</span><span class="p">;</span>
</span><span class="line"> <span class="n">b_y</span> <span class="o">=</span> <span class="n">b</span><span class="p">;</span>
</span><span class="line"> <span class="n">d_y</span> <span class="o">=</span> <span class="n">d</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="n">n</span> <span class="o">>>=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line"> <span class="k">return</span> <span class="n">b_t</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>提取出公共子式,改变赋值顺序,消除多余的临时变量,得</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">uint64_t</span> <span class="nf">fibo_expo_var</span><span class="p">(</span><span class="kt">uint64_t</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">a_t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b_t</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">d_t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="c1">// "1"</span>
</span><span class="line"> <span class="n">a_y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">b_y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">d_y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">// "y"</span>
</span><span class="line">
</span><span class="line"> <span class="k">while</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&</span> <span class="mi">1</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="c1">// t*=y;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">bx</span> <span class="o">=</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">b_y</span><span class="p">;</span>
</span><span class="line"> <span class="n">b_t</span> <span class="o">=</span> <span class="n">a_t</span> <span class="o">*</span> <span class="n">b_y</span> <span class="o">+</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">d_y</span><span class="p">;</span>
</span><span class="line"> <span class="n">a_t</span> <span class="o">=</span> <span class="n">a_t</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">bx</span><span class="p">;</span>
</span><span class="line"> <span class="n">d_t</span> <span class="o">=</span> <span class="n">bx</span> <span class="o">+</span> <span class="n">d_t</span> <span class="o">*</span> <span class="n">d_y</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line">
</span><span class="line"> <span class="c1">//y*=y;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">b2</span> <span class="o">=</span> <span class="n">b_y</span> <span class="o">*</span> <span class="n">b_y</span><span class="p">;</span>
</span><span class="line"> <span class="n">b_y</span> <span class="o">*=</span> <span class="p">(</span><span class="n">a_y</span> <span class="o">+</span> <span class="n">d_y</span><span class="p">);</span>
</span><span class="line"> <span class="n">a_y</span> <span class="o">=</span> <span class="n">a_y</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">b2</span><span class="p">;</span>
</span><span class="line"> <span class="n">d_y</span> <span class="o">=</span> <span class="n">b2</span> <span class="o">+</span> <span class="n">d_y</span> <span class="o">*</span> <span class="n">d_y</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="n">n</span> <span class="o">>>=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line"> <span class="k">return</span> <span class="n">b_t</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<center>⁂</center>
<p>太棒了,我们找着了一个<script type="math/tex">O(\lg n)</script>的算法来计算斐波那契数列。不过仍有稍加改进的余地。再怎么改进呢?且注意,</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a & b\\
b & d
\end{array}
\right]
=
\left[
\begin{array}{cc}
a & b\\
b & a+b
\end{array}
\right]
%]]></script>
<p>这样一来消去变量<code>d</code>,得到仅包含变量<code>a</code>和<code>b</code>的方程。修改乘积项和平方项使其只包含变量<code>a</code>和<code>b</code>:</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a_t & b_t\\
b_t & a_t + b_t
\end{array}
\right]
\left[
\begin{array}{cc}
a_y & b_y\\
b_y & a_y + b_y
\end{array}
\right]
=
\left[
\begin{array}{cc}
a_ta_y+b_t + b_y & a_tb_y + b_t(ay+b_y)\\
--- & ---
\end{array}
\right]
%]]></script>
<p>以及</p>
<script type="math/tex; mode=display">% <![CDATA[
\left[
\begin{array}{cc}
a_y & b_y\\
b_y & d_y
\end{array}
\right]^2
=
\left[
\begin{array}{cc}
a_y^2+b_y^2 & a_yb_y+b_y(a_y+b_y)\\
--- & ---
\end{array}
\right]
%]]></script>
<p>其中虚线表示无需关心的部分。利用上述结果,可得:</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
</pre></td><td class="code"><pre><code class="c"><span class="line"><span class="kt">uint64_t</span> <span class="nf">fibo_expo_var_2</span><span class="p">(</span><span class="kt">uint64_t</span> <span class="n">n</span><span class="p">)</span>
</span><span class="line"><span class="p">{</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">a_t</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="n">b_t</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="c1">// "1"</span>
</span><span class="line"> <span class="n">a_y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">b_y</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="c1">// "y"</span>
</span><span class="line">
</span><span class="line"> <span class="k">while</span> <span class="p">(</span><span class="n">n</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">&</span> <span class="mi">1</span><span class="p">)</span>
</span><span class="line"> <span class="p">{</span>
</span><span class="line"> <span class="c1">// t*=y;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">bx</span> <span class="o">=</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">b_y</span><span class="p">;</span>
</span><span class="line"> <span class="n">b_t</span> <span class="o">=</span> <span class="n">a_t</span> <span class="o">*</span> <span class="n">b_y</span> <span class="o">+</span> <span class="n">b_t</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">bx</span><span class="p">;</span>
</span><span class="line"> <span class="n">a_t</span> <span class="o">=</span> <span class="n">a_t</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">bx</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line">
</span><span class="line"> <span class="c1">//y*=y;</span>
</span><span class="line"> <span class="kt">uint64_t</span> <span class="n">b2</span> <span class="o">=</span> <span class="n">b_y</span> <span class="o">*</span> <span class="n">b_y</span><span class="p">;</span>
</span><span class="line"> <span class="n">b_y</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">*</span> <span class="n">b_y</span> <span class="o">+</span> <span class="n">b2</span><span class="p">;</span>
</span><span class="line"> <span class="n">a_y</span> <span class="o">=</span> <span class="n">a_y</span> <span class="o">*</span> <span class="n">a_y</span> <span class="o">+</span> <span class="n">b2</span><span class="p">;</span>
</span><span class="line">
</span><span class="line"> <span class="n">n</span> <span class="o">>>=</span> <span class="mi">1</span><span class="p">;</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line"> <span class="k">return</span> <span class="n">b_t</span><span class="p">;</span>
</span><span class="line"><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>这几个算法就运行速度比较结果如何?原始递归算法效率低得惊人,生成<script type="math/tex">F_ {50}</script>需要一分多钟,而其他算法只花费几微秒。进行10000000(1000万)次迭代测试(使用相同的随机输入,随机数n位于1到70之间),结果如下</p>
<table>
<tbody>
<tr>
<td>iter</td>
<td>3.29529s</td>
</tr>
<tr>
<td>iter_var</td>
<td>3.30153s</td>
</tr>
<tr>
<td>expo</td>
<td>2.28118s</td>
</tr>
<tr>
<td>expo_var</td>
<td>2.2531s</td>
</tr>
<tr>
<td>expo_var_2</td>
<td>2.22531s</td>
</tr>
</tbody>
</table>
<p><br />
最后三个版本的确快很多,但是快速乘幂算法版本之间的效率差距并不显著。假如改进程度相比迭代版本没有那么大的话,我们应该意识到刚只是计算相对较小的斐波那契数列。64位无符号整数最大仅允许存储<script type="math/tex">F_{92}</script>(因为<script type="math/tex">log_ \phi 2^{64} \approx 92</script>,<script type="math/tex">F_n \approx \phi^n</script>,所以两边取对数解出n,推出<script type="math/tex">\phi^n = 2^{64}</script>),不过通过大整数(抽象为类)来计算<script type="math/tex">F_{129713151}</script>,远比迭代方法和递归算法快得多得多。</p>
</div>
</article>
<article>
<header>
<h1 class="entry-title"><a href="/blog/2014/04/27/the-abuse-of-design-patterns-in-writing-a-hello-world-program/">[译]设计模式滥用之Hello World</a></h1>
<p class="meta">
<time datetime="2014-04-27T11:27:00+08:00" pubdate data-updated="true">Apr 27<span>th</span>, 2014</time>
| <a href="/blog/2014/04/27/the-abuse-of-design-patterns-in-writing-a-hello-world-program/#disqus_thread">Comments</a>
</p>
</header>
<div class="entry-content"><div class="note info">
<h5>摘要</h5>
<p>本文译自<a href="http://taskinoor.wordpress.com/2011/09/21/the-abuse-of-design-patterns-in-writing-a-hello-world-program/" target="_blank" title="The Abuse of Design Patterns in writing a Hello World Program">The Abuse of Design Patterns in writing a Hello World Program</a></p>
</div>
<blockquote><p>设计模式流行之初,我曾在某新闻组里看到一条评论,说有人号称他们试图倾尽23个GoF设计模式去编写一个奇葩程序。不过他们说最终没有成,极其所能也只用到其中20个。他们希望客户叫他们返工,没准能加塞另外3个。</p><p>试图用遍所有模式是行不通的,终将以生拼硬凑的设计告终——投机取巧的设计充满百无一用的灵活性。这年头软件太过复杂了。我们不能猜测还要什么,而应该关注于本该要什么。</p></blockquote>
<p><strong>gamma</strong><sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> </p>
<p>大家刚开始学习设计模式时,很少考虑模式的适用场景,想方设法到处套用,觉得模式用得越多,设计越好。结果产生大量不必要复杂的代码。<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup></p>
<p>与其说是“运用模式”,不如说是“滥用模式”。有人试图在<strong>Hello World</strong>程序中套用模式,不可避免地导致滥用。</p>
<p>让我们考察一个示例。这是一个经典问题: 编写一个程序,打印字符串<code>Hello World</code>至标准输出。</p>
<p>编程新手会写出如下代码(Java语言):</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
</pre></td><td class="code"><pre><code class="java"><span class="line"><span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">"hello world"</span><span class="o">);</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>这段代码看起来超乎简单。能否加点设计模式?接下来…</p>
<p>首先,定义两个接口<code>Subject</code>和<code>Observer</code>,加入<strong>观察者模式</strong>。</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
</pre></td><td class="code"><pre><code class="java"><span class="line"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Subject</span> <span class="o">{</span>
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">attach</span><span class="o">(</span><span class="n">Observer</span> <span class="n">observer</span><span class="o">);</span>
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">detach</span><span class="o">(</span><span class="n">Observer</span> <span class="n">observer</span><span class="o">);</span>
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">notifyObservers</span><span class="o">();</span>
</span><span class="line"><span class="o">}</span>
</span><span class="line">
</span><span class="line"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Observer</span> <span class="o">{</span>
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">update</span><span class="o">(</span><span class="n">Subject</span> <span class="n">subject</span><span class="o">);</span>
</span><span class="line"><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>然后,定义两个类<code>HelloWorldSubject</code>和<code>HelloWorldObserver</code>实现以上两个接口。</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
<span class="line-number">21</span>
<span class="line-number">22</span>
<span class="line-number">23</span>
<span class="line-number">24</span>
<span class="line-number">25</span>
<span class="line-number">26</span>
<span class="line-number">27</span>
<span class="line-number">28</span>
<span class="line-number">29</span>
<span class="line-number">30</span>
<span class="line-number">31</span>
<span class="line-number">32</span>
<span class="line-number">33</span>
<span class="line-number">34</span>
<span class="line-number">35</span>
<span class="line-number">36</span>
<span class="line-number">37</span>
<span class="line-number">38</span>
<span class="line-number">39</span>
<span class="line-number">40</span>
<span class="line-number">41</span>
<span class="line-number">42</span>
<span class="line-number">43</span>
<span class="line-number">44</span>
<span class="line-number">45</span>
<span class="line-number">46</span>
</pre></td><td class="code"><pre><code class="java"><span class="line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorldSubject</span> <span class="kd">implements</span> <span class="n">Subject</span> <span class="o">{</span>
</span><span class="line">
</span><span class="line"> <span class="kd">private</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Observer</span><span class="o">></span> <span class="n">observers</span><span class="o">;</span>
</span><span class="line"> <span class="kd">private</span> <span class="n">String</span> <span class="n">str</span><span class="o">;</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="nf">HelloWorldSubject</span><span class="o">()</span> <span class="o">{</span>
</span><span class="line"> <span class="kd">super</span><span class="o">();</span>
</span><span class="line">
</span><span class="line"> <span class="n">observers</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o"><</span><span class="n">Observer</span><span class="o">>();</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">attach</span><span class="o">(</span><span class="n">Observer</span> <span class="n">observer</span><span class="o">)</span> <span class="o">{</span>
</span><span class="line"> <span class="n">observers</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">observer</span><span class="o">);</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">detach</span><span class="o">(</span><span class="n">Observer</span> <span class="n">observer</span><span class="o">)</span> <span class="o">{</span>
</span><span class="line"> <span class="n">observers</span><span class="o">.</span><span class="na">remove</span><span class="o">(</span><span class="n">observer</span><span class="o">);</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">notifyObservers</span><span class="o">()</span> <span class="o">{</span>
</span><span class="line"> <span class="n">Iterator</span><span class="o"><</span><span class="n">Observer</span><span class="o">></span> <span class="n">iter</span> <span class="o">=</span> <span class="n">observers</span><span class="o">.</span><span class="na">iterator</span><span class="o">();</span>
</span><span class="line">
</span><span class="line"> <span class="k">while</span> <span class="o">(</span><span class="n">iter</span><span class="o">.</span><span class="na">hasNext</span><span class="o">())</span> <span class="o">{</span>
</span><span class="line"> <span class="n">Observer</span> <span class="n">observer</span> <span class="o">=</span> <span class="n">iter</span><span class="o">.</span><span class="na">next</span><span class="o">();</span>
</span><span class="line"> <span class="n">observer</span><span class="o">.</span><span class="na">update</span><span class="o">(</span><span class="k">this</span><span class="o">);</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="n">String</span> <span class="nf">getStr</span><span class="o">()</span> <span class="o">{</span>
</span><span class="line"> <span class="k">return</span> <span class="n">str</span><span class="o">;</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setStr</span><span class="o">(</span><span class="n">String</span> <span class="n">str</span><span class="o">)</span> <span class="o">{</span>
</span><span class="line"> <span class="k">this</span><span class="o">.</span><span class="na">str</span> <span class="o">=</span> <span class="n">str</span><span class="o">;</span>
</span><span class="line"> <span class="n">notifyObservers</span><span class="o">();</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line"><span class="o">}</span>
</span><span class="line">
</span><span class="line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorldObserver</span> <span class="kd">implements</span> <span class="n">Observer</span> <span class="o">{</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">update</span><span class="o">(</span><span class="n">Subject</span> <span class="n">subject</span><span class="o">)</span> <span class="o">{</span>
</span><span class="line"> <span class="n">HelloWorldSubject</span> <span class="n">sub</span> <span class="o">=</span> <span class="o">(</span><span class="n">HelloWorldSubject</span><span class="o">)</span><span class="n">subject</span><span class="o">;</span>
</span><span class="line"> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">sub</span><span class="o">.</span><span class="na">getStr</span><span class="o">());</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>接着,再添加一个<strong>命令模式</strong>。</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
</pre></td><td class="code"><pre><code class="java"><span class="line"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">Command</span> <span class="o">{</span>
</span><span class="line"> <span class="kt">void</span> <span class="nf">execute</span><span class="o">();</span>
</span><span class="line"><span class="o">}</span>
</span><span class="line">
</span><span class="line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorldCommand</span> <span class="kd">implements</span> <span class="n">Command</span> <span class="o">{</span>
</span><span class="line">
</span><span class="line"> <span class="kd">private</span> <span class="n">HelloWorldSubject</span> <span class="n">subject</span><span class="o">;</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="nf">HelloWorldCommand</span><span class="o">(</span><span class="n">Subject</span> <span class="n">subject</span><span class="o">)</span> <span class="o">{</span>
</span><span class="line"> <span class="kd">super</span><span class="o">();</span>
</span><span class="line">
</span><span class="line"> <span class="k">this</span><span class="o">.</span><span class="na">subject</span> <span class="o">=</span> <span class="o">(</span><span class="n">HelloWorldSubject</span><span class="o">)</span><span class="n">subject</span><span class="o">;</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">execute</span><span class="o">()</span> <span class="o">{</span>
</span><span class="line"> <span class="n">subject</span><span class="o">.</span><span class="na">setStr</span><span class="o">(</span><span class="s">"hello world"</span><span class="o">);</span>
</span><span class="line"> <span class="o">}</span>
</span><span class="line">
</span><span class="line"><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure></notextile></div>
<p>然后添加一个<strong>抽象工厂模式</strong>。</p>
<div class="bogus-wrapper"><notextile><figure class="code"><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class="line-number">1</span>
<span class="line-number">2</span>
<span class="line-number">3</span>
<span class="line-number">4</span>
<span class="line-number">5</span>
<span class="line-number">6</span>
<span class="line-number">7</span>
<span class="line-number">8</span>
<span class="line-number">9</span>
<span class="line-number">10</span>
<span class="line-number">11</span>
<span class="line-number">12</span>
<span class="line-number">13</span>
<span class="line-number">14</span>
<span class="line-number">15</span>
<span class="line-number">16</span>
<span class="line-number">17</span>
<span class="line-number">18</span>
<span class="line-number">19</span>
<span class="line-number">20</span>
</pre></td><td class="code"><pre><code class="java"><span class="line"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">AbstractFactory</span> <span class="o">{</span>
</span><span class="line"> <span class="kd">public</span> <span class="n">Subject</span> <span class="nf">createSubject</span><span class="o">();</span>
</span><span class="line"> <span class="kd">public</span> <span class="n">Observer</span> <span class="nf">createObserver</span><span class="o">();</span>
</span><span class="line"> <span class="kd">public</span> <span class="n">Command</span> <span class="nf">createCommand</span><span class="o">(</span><span class="n">Subject</span> <span class="n">subject</span><span class="o">);</span>
</span><span class="line"><span class="o">}</span>
</span><span class="line">
</span><span class="line"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HelloWorldFactory</span> <span class="kd">implements</span> <span class="n">AbstractFactory</span> <span class="o">{</span>
</span><span class="line">
</span><span class="line"> <span class="kd">public</span> <span class="n">Subject</span> <span class="nf">createSubject</span><span class="o">()</span> <span class="o">{</span>
</span><span class="line"> <span class="k">return</span> <span class="k">new</span> <span class="nf">HelloWorldSubject</span><span class="o">();</span>
</span><span class="line"> <span class="o">}</span>