├── .gitignore
├── .idea
├── .name
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── encodings.xml
├── gradle.xml
├── libraries
│ ├── Gradle__junit_junit_4_11.xml
│ └── Gradle__org_hamcrest_hamcrest_core_1_3.xml
├── misc.xml
├── modules.xml
├── scopes
│ └── scope_settings.xml
├── uiDesigner.xml
├── vcs.xml
└── workspace.xml
├── DesignPatterns.iml
├── README.md
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
└── com
└── baron
└── patterns
├── adapter
├── AdapterPatternTest.java
├── README.md
├── adaptee
│ ├── Turkey.java
│ └── WildTurkey.java
├── adapter
│ └── TurkeyAdapter.java
└── target
│ ├── Duck.java
│ └── MallardDuck.java
├── command
├── CommandPatternTest.java
├── README.md
├── command
│ ├── Command.java
│ ├── LightOffCommand.java
│ ├── LightOnCommand.java
│ ├── StereoOffWithCDCommand.java
│ └── StereoOnWithCDCommand.java
├── executor
│ └── RemoteControl.java
└── requester
│ ├── Light.java
│ └── Stereo.java
├── composite
└── README.md
├── compound
├── DuckSimulator.java
├── README.md
├── adapter
│ └── GooseAdapter.java
├── composite
│ └── Flock.java
├── decorator
│ └── QuackCounter.java
├── factory
│ ├── AbstractDuckFactory.java
│ ├── CountingDuckFactory.java
│ └── DuckFactory.java
├── model
│ ├── DuckCall.java
│ ├── Goose.java
│ ├── MallardDuck.java
│ ├── Quackable.java
│ ├── RedheadDuck.java
│ └── RubberDuck.java
└── observer
│ ├── Observer.java
│ └── QuackObservable.java
├── decorator
├── DecoratorPatternTest.java
├── README.md
├── component
│ ├── Beverage.java
│ ├── DarkRoast.java
│ └── Decaf.java
└── decorator
│ ├── CondimentDecorator.java
│ ├── Mocha.java
│ └── Whip.java
├── facade
├── FacadePatternTest.java
└── README.md
├── factory
├── FactoryPatternTest.java
├── README.md
├── factory
│ ├── ChicagoPizzaStore.java
│ ├── NYPizzaStore.java
│ └── PizzaStore.java
└── product
│ ├── ChicagoStyleCheesePizza.java
│ ├── ChicagoStyleClamPizza.java
│ ├── NYStyleCheesePizza.java
│ ├── NYStyleClamPizza.java
│ └── Pizza.java
├── iterator
└── README.md
├── observer
├── ObserverPatternTest.java
├── README.md
├── observer
│ ├── CurrentConditionsDisplay.java
│ ├── DisplayElement.java
│ ├── ForecastDisplay.java
│ └── Observer.java
└── subject
│ ├── Subject.java
│ └── WeatherData.java
├── package-info.java
├── proxy
├── MyRemote.java
├── MyRemoteImpl.java
└── README.md
├── singleton
├── README.md
└── Singleton.java
├── state
├── README.md
├── StatePatternTest.java
└── state
│ ├── GumballMachine.java
│ ├── HasQuarterState.java
│ ├── NoQuarterState.java
│ ├── SoldOutState.java
│ ├── SoldState.java
│ └── State.java
├── strategy
├── Duck.java
├── MallardDuck.java
├── README.md
├── StrategyPatternTest.java
└── behavior
│ ├── FlyBehavior.java
│ ├── QuackBehavior.java
│ └── impl
│ ├── FlyNoWay.java
│ ├── FlyWithWings.java
│ ├── MuteQuack.java
│ ├── Quack.java
│ └── Squeak.java
└── template
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 | /*/build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
--------------------------------------------------------------------------------
/.idea/.name:
--------------------------------------------------------------------------------
1 | DesignPatterns
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
15 |
16 |
--------------------------------------------------------------------------------
/.idea/libraries/Gradle__junit_junit_4_11.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/libraries/Gradle__org_hamcrest_hamcrest_core_1_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/scopes/scope_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 |
37 |
38 |
39 |
57 |
58 |
59 |
142 |
143 |
144 |
147 |
148 |
149 |
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 | true
283 |
284 |
285 |
286 |
287 |
288 |
289 |
290 |
291 |
292 |
293 |
294 |
295 |
296 |
297 |
298 | Abstraction issues
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 | true
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 | 1450060254825
864 |
865 | 1450060254825
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 |
1001 |
1002 |
1003 |
1004 |
1005 |
1006 |
1007 |
1008 |
1009 |
1010 |
1011 |
1012 |
1013 |
1014 |
1015 |
1016 |
1017 |
1018 |
1019 |
1020 |
1021 |
1022 |
1023 |
1024 |
1025 |
1026 |
1027 |
1028 |
1029 |
1030 |
1031 |
1032 |
1033 |
1034 |
1035 |
1036 |
1037 |
1038 |
1039 |
1040 |
1041 |
1042 |
1043 |
1044 |
1045 |
1046 |
1047 |
1048 |
1049 |
1050 |
1051 |
1052 |
1053 |
1054 |
1055 |
1056 |
1057 |
1058 |
1059 |
1060 |
1061 |
1062 |
1063 |
1064 |
1065 |
1066 |
1067 |
1068 |
1069 |
1070 |
1071 |
1072 |
1073 |
1074 |
1075 |
1076 |
1077 |
1078 |
1079 |
1080 |
1081 |
1082 |
1083 |
1084 |
1085 |
1086 |
1087 |
1088 |
1089 |
1090 |
1091 |
1092 |
1093 |
1094 |
1095 |
1096 |
1097 |
1098 |
1099 |
1100 |
1101 |
1102 |
1103 |
1104 |
1105 |
1106 |
1107 |
1108 |
1109 |
1110 |
1111 |
1112 |
1113 |
1114 |
1115 |
1116 |
1117 |
1118 |
1119 |
1120 |
1121 |
1122 |
1123 |
1124 |
1125 |
1126 |
1127 |
1128 |
1129 |
1130 |
1131 |
1132 |
1133 |
1134 |
1135 |
1136 |
1137 |
1138 |
1139 |
1140 |
1141 |
1142 |
1143 |
1144 |
1145 |
1146 |
1147 |
1148 |
1149 |
1150 |
1151 |
1152 |
1153 |
1154 |
1155 |
1156 |
1157 |
1158 |
1159 |
1160 |
1161 |
1162 |
1163 |
1164 |
1165 |
1166 |
1167 |
1168 |
1169 |
1170 |
1171 |
1172 |
1173 |
1174 |
1175 |
1176 |
1177 |
1178 |
1179 |
1180 |
1181 |
1182 |
1183 |
1184 |
1185 |
1186 |
1187 |
1188 |
1189 |
1190 |
1191 |
1192 |
1193 |
1194 |
1195 |
1196 |
1197 |
1198 |
1199 |
1200 |
1201 |
1202 |
1203 |
1204 |
1205 |
1206 |
1207 |
1208 |
1209 |
1210 |
1211 |
1212 |
1213 |
1214 |
1215 |
1216 |
1217 |
1218 |
1219 |
1220 |
1221 |
1222 |
1223 |
1224 |
1225 |
1226 |
1227 |
1228 |
1229 |
1230 |
1231 |
1232 |
1233 |
1234 |
1235 |
1236 |
1237 |
1238 |
1239 |
1240 |
1241 |
1242 |
1243 |
1244 |
1245 |
1246 |
1247 |
1248 |
1249 |
1250 |
1251 |
1252 |
1253 |
1254 |
1255 |
1256 |
1257 |
1258 |
1259 |
1260 |
1261 |
1262 |
1263 |
1264 |
1265 |
1266 |
1267 |
1268 |
1269 |
1270 |
1271 |
1272 |
1273 |
1274 |
1275 |
1276 |
1277 |
1278 |
1279 |
1280 |
1281 |
1282 |
1283 |
1284 |
1285 |
1286 |
1287 |
1288 |
1289 |
1290 |
1291 |
1292 |
1293 |
1294 |
1295 |
1296 |
1297 |
1298 |
1299 |
1300 |
1301 |
1302 |
1303 |
1304 |
1305 |
1306 |
1307 |
1308 |
1309 |
1310 |
1311 |
1312 |
1313 |
1314 |
1315 |
1316 |
1317 |
1318 |
1319 |
1320 |
1321 |
1322 |
1323 |
1324 |
1325 |
1326 |
1327 |
1328 |
1329 |
1330 |
1331 |
1332 |
1333 |
1334 |
1335 |
1336 |
1337 |
1338 |
1339 |
1340 |
1341 |
1342 |
1343 |
1344 |
1345 |
1346 |
1347 |
1348 |
1349 |
1350 |
1351 |
1352 |
1353 |
1354 |
1355 |
1356 |
1357 |
1358 |
--------------------------------------------------------------------------------
/DesignPatterns.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #Design Patterns
2 | > 设计模型学习笔记
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | sourceCompatibility = 1.5
4 | version = '1.0'
5 |
6 | repositories {
7 | mavenCentral()
8 | }
9 |
10 | dependencies {
11 | testCompile group: 'junit', name: 'junit', version: '4.11'
12 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BaronZ88/DesignPatterns/7f83069e6056cd486bb62c39cd2e8eeb89a0ecf8/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 14 10:44:21 CST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-bin.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'DesignPatterns'
2 |
3 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/AdapterPatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.adapter;
2 |
3 | import com.baron.patterns.adapter.adaptee.WildTurkey;
4 | import com.baron.patterns.adapter.adapter.TurkeyAdapter;
5 | import com.baron.patterns.adapter.target.Duck;
6 | import com.baron.patterns.adapter.target.MallardDuck;
7 |
8 | /**
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class AdapterPatternTest {
14 |
15 | public static void main(String[] args) {
16 |
17 | MallardDuck duck = new MallardDuck();
18 | WildTurkey turkey = new WildTurkey();
19 |
20 | TurkeyAdapter adapter = new TurkeyAdapter(turkey);
21 |
22 | System.out.println("==========火鸡===========");
23 | turkey.gobble();
24 | turkey.fly();
25 |
26 | System.out.println("\n==========鸭子===========");
27 | testDuck(duck);
28 |
29 | System.out.println("\n==========适配器===========");
30 | testDuck(adapter);
31 | }
32 |
33 | static void testDuck(Duck duck) {
34 | duck.quack();
35 | duck.fly();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/README.md:
--------------------------------------------------------------------------------
1 | #适配器模式(AdapterPattern)
2 |
3 | ####定义:
4 | > 将一个类的接口,转换成客户期望的另一个接口。适配器让原本接口不兼容的类可以合作无间。
5 |
6 | ###设计原则
7 | 1.
8 |
9 |
10 | ###类图
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/adaptee/Turkey.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.adapter.adaptee;
2 |
3 | /**
4 | * 火鸡接口,被适配者
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public interface Turkey {
11 |
12 | void gobble();//火鸡不会呱呱叫,只会咯咯(gobble)叫
13 |
14 | void fly();//火鸡也会飞,但是并飞不远
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/adaptee/WildTurkey.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.adapter.adaptee;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public class WildTurkey implements Turkey {
9 |
10 | @Override
11 | public void gobble() {
12 | System.out.println("咯咯咯......");
13 | }
14 |
15 | @Override
16 | public void fly() {
17 | System.out.println("我火鸡哥玩儿命的挥动翅膀,好不容易才飞了一小段距离");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/adapter/TurkeyAdapter.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.adapter.adapter;
2 |
3 | import com.baron.patterns.adapter.adaptee.Turkey;
4 | import com.baron.patterns.adapter.target.Duck;
5 |
6 | /**
7 | * 适配器,将火鸡的接口转换成鸭子的接口
8 | *
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class TurkeyAdapter implements Duck {
14 |
15 | private Turkey turkey;
16 |
17 | public TurkeyAdapter(Turkey turkey) {
18 | this.turkey = turkey;
19 | }
20 |
21 | @Override
22 | public void quack() {
23 | turkey.gobble();
24 | }
25 |
26 | @Override
27 | public void fly() {
28 | //虽然火鸡也能飞,但是飞行距离太短,需要多次挥动它那萌萌哒的翅膀才能和火鸡飞的一样远
29 | for (int i = 0; i < 5; i++) {
30 | turkey.fly();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/target/Duck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.adapter.target;
2 |
3 | /**
4 | * 鸭子,目标对象
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public interface Duck {
11 |
12 | void quack();
13 |
14 | void fly();
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/adapter/target/MallardDuck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.adapter.target;
2 |
3 | /**
4 | * 绿头鸭,目标对象
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class MallardDuck implements Duck {
11 |
12 | @Override
13 | public void quack() {
14 | System.out.println("呱呱呱......");
15 | }
16 |
17 | @Override
18 | public void fly() {
19 | System.out.println("鸭哥我虽然带了绿帽子,但是依然可以只有飞翔!");
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/CommandPatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command;
2 |
3 | import com.baron.patterns.command.command.LightOffCommand;
4 | import com.baron.patterns.command.command.LightOnCommand;
5 | import com.baron.patterns.command.command.StereoOffWithCDCommand;
6 | import com.baron.patterns.command.command.StereoOnWithCDCommand;
7 | import com.baron.patterns.command.executor.RemoteControl;
8 | import com.baron.patterns.command.requester.Light;
9 | import com.baron.patterns.command.requester.Stereo;
10 |
11 | /**
12 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
13 | * @version 1.0
14 | * @since 1.0
15 | */
16 | public class CommandPatternTest {
17 |
18 | public static void main(String[] args) {
19 |
20 | RemoteControl remoteControl = new RemoteControl();
21 |
22 | Light light = new Light();
23 | Stereo stereo = new Stereo();
24 |
25 | LightOnCommand lightOnCommand = new LightOnCommand(light);
26 | LightOffCommand lightOffCommand = new LightOffCommand(light);
27 |
28 | StereoOnWithCDCommand stereoOnWithCDCommand = new StereoOnWithCDCommand(stereo);
29 | StereoOffWithCDCommand stereoOffWithCDCommand = new StereoOffWithCDCommand(stereo);
30 |
31 | remoteControl.setCommand(lightOnCommand, lightOffCommand);
32 | remoteControl.onButtonPressed();
33 | remoteControl.offButtonPressed();
34 |
35 | System.out.println();
36 |
37 | remoteControl.setCommand(stereoOnWithCDCommand,stereoOffWithCDCommand);
38 | remoteControl.onButtonPressed();
39 | remoteControl.offButtonPressed();
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/README.md:
--------------------------------------------------------------------------------
1 | #命令模式(CommandPattern)
2 |
3 | ####定义:
4 | > 将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
5 |
6 | ###设计原则
7 | 1.
8 |
9 | ###类图
10 |
11 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/command/Command.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.command;
2 |
3 | /**
4 | * 命令对象接口,通过具体的命令对象将请求者和执行者解耦
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public interface Command {
11 |
12 | void execute();
13 |
14 | void undo();
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/command/LightOffCommand.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.command;
2 |
3 | import com.baron.patterns.command.requester.Light;
4 |
5 | /**
6 | * 开灯命令,将具体的请求命令封装成对象
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class LightOffCommand implements Command {
13 |
14 | Light light;
15 |
16 | public LightOffCommand(Light light) {
17 | this.light = light;
18 | }
19 |
20 | @Override
21 | public void execute() {
22 | this.light.off();
23 | }
24 |
25 | @Override
26 | public void undo() {
27 | this.light.on();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/command/LightOnCommand.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.command;
2 |
3 | import com.baron.patterns.command.requester.Light;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class LightOnCommand implements Command {
11 |
12 | Light light;
13 |
14 | public LightOnCommand(Light light) {
15 | this.light = light;
16 | }
17 |
18 | @Override
19 | public void execute() {
20 | this.light.on();
21 | }
22 |
23 | @Override
24 | public void undo() {
25 | this.light.off();
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/command/StereoOffWithCDCommand.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.command;
2 |
3 | import com.baron.patterns.command.requester.Stereo;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class StereoOffWithCDCommand implements Command {
11 |
12 | private Stereo stereo;
13 |
14 | public StereoOffWithCDCommand(Stereo stereo) {
15 | this.stereo = stereo;
16 | }
17 |
18 | @Override
19 | public void execute() {
20 | stereo.setVolume(0);
21 | stereo.off();
22 | }
23 |
24 | @Override
25 | public void undo() {
26 | stereo.on();
27 | stereo.setCD();
28 | stereo.setVolume(11);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/command/StereoOnWithCDCommand.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.command;
2 |
3 | import com.baron.patterns.command.requester.Stereo;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class StereoOnWithCDCommand implements Command {
11 |
12 | private Stereo stereo;
13 |
14 | public StereoOnWithCDCommand(Stereo stereo) {
15 | this.stereo = stereo;
16 | }
17 |
18 | @Override
19 | public void execute() {
20 | stereo.on();
21 | stereo.setCD();
22 | stereo.setVolume(11);
23 | }
24 |
25 | @Override
26 | public void undo() {
27 | stereo.off();
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/executor/RemoteControl.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.executor;
2 |
3 | import com.baron.patterns.command.command.Command;
4 |
5 | /**
6 | * 遥控器,执行请求的对象
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class RemoteControl {
13 |
14 | private Command onCommand;
15 | private Command offCommand;
16 |
17 | public RemoteControl() {
18 | }
19 |
20 | public void setCommand(Command onCommand, Command offCommand) {
21 | this.onCommand = onCommand;
22 | this.offCommand = offCommand;
23 | }
24 |
25 | public void onButtonPressed() {
26 | if (this.onCommand != null) {
27 | this.onCommand.execute();
28 | }
29 | }
30 |
31 | public void offButtonPressed() {
32 | if (this.offCommand != null) {
33 | this.offCommand.execute();
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/requester/Light.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.requester;
2 |
3 | /**
4 | * 点灯,发出请求的对象
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class Light {
11 |
12 | public void on() {
13 | System.out.println("等开了");
14 | }
15 |
16 | public void off() {
17 | System.out.println("妈蛋啊,灯关了!!!");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/command/requester/Stereo.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.command.requester;
2 |
3 | /**
4 | * 音响,发出请求的对象
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class Stereo {
11 |
12 | public void on() {
13 | System.out.println("打开音响");
14 | }
15 |
16 | public void setCD() {
17 | System.out.println("放入CD");
18 | }
19 |
20 | public void setVolume(int i) {
21 | System.out.println("设置播放音量为:" + i);
22 | }
23 |
24 | public void off(){
25 | System.out.println("关闭音响");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/composite/README.md:
--------------------------------------------------------------------------------
1 | #组合模式(CompositePattern)
2 |
3 | ####定义:
4 | > 允许你将对象组合成树形结构来表现“整体/部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合。
5 |
6 | ###设计原则
7 | 1.
8 |
9 |
10 | ###类图
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/DuckSimulator.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound;
2 |
3 | import com.baron.patterns.compound.adapter.GooseAdapter;
4 | import com.baron.patterns.compound.composite.Flock;
5 | import com.baron.patterns.compound.decorator.QuackCounter;
6 | import com.baron.patterns.compound.factory.AbstractDuckFactory;
7 | import com.baron.patterns.compound.factory.CountingDuckFactory;
8 | import com.baron.patterns.compound.model.*;
9 |
10 | /**
11 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
12 | * @version 1.0
13 | * @since 1.0
14 | */
15 | public class DuckSimulator {
16 |
17 | public static void main(String[] args) {
18 | DuckSimulator simulator = new DuckSimulator();
19 | AbstractDuckFactory duckFactory = new CountingDuckFactory();
20 | simulator.simulate(duckFactory);
21 | }
22 |
23 | void simulate(AbstractDuckFactory duckFactory) {
24 |
25 | Quackable redheadDuck = duckFactory.createRedheadDuck();
26 | Quackable duckCall = duckFactory.createDuckCall();
27 | Quackable rubberDuck = duckFactory.createRubberDuck();
28 | Quackable gooseDuck = new GooseAdapter(new Goose());
29 |
30 | System.out.println("\nDuck Simulator:With Composite - Flocks");
31 |
32 | Flock flockOfDucks = new Flock();
33 |
34 | flockOfDucks.add(redheadDuck);
35 | flockOfDucks.add(duckCall);
36 | flockOfDucks.add(rubberDuck);
37 | flockOfDucks.add(gooseDuck);
38 |
39 | Flock flockOfMallards = new Flock();
40 |
41 | Quackable mallardDuckOne = duckFactory.createMallardDuck();
42 | Quackable mallardDuckTwo = duckFactory.createMallardDuck();
43 | Quackable mallardDuckThree = duckFactory.createMallardDuck();
44 | Quackable mallardDuckFour = duckFactory.createMallardDuck();
45 |
46 | flockOfMallards.add(mallardDuckOne);
47 | flockOfMallards.add(mallardDuckTwo);
48 | flockOfMallards.add(mallardDuckThree);
49 | flockOfMallards.add(mallardDuckFour);
50 |
51 | flockOfDucks.add(flockOfMallards);
52 |
53 | System.out.println("\nDuck Simulator:Whole Flock Simulator");
54 | simulate(flockOfDucks);
55 |
56 | System.out.println("\nDuck Simulator:Mallard Flock Simulator");
57 | simulate(flockOfMallards);
58 |
59 | System.out.println("\nThe ducks quacked " + QuackCounter.getQuacks() + " times");
60 | }
61 |
62 | void simulate(Quackable quackable) {
63 | quackable.quack();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/README.md:
--------------------------------------------------------------------------------
1 | #复合模式(CompoundPattern)
2 |
3 | ####定义:
4 | > 多模式一起工作
5 |
6 | ###设计原则
7 | 1.
8 |
9 |
10 | ###类图
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/adapter/GooseAdapter.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.adapter;
2 |
3 | import com.baron.patterns.compound.model.Goose;
4 | import com.baron.patterns.compound.model.Quackable;
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public class GooseAdapter implements Quackable {
12 |
13 | Goose goose;
14 |
15 | public GooseAdapter(Goose goose) {
16 | this.goose = goose;
17 | }
18 |
19 | @Override
20 | public void quack() {
21 | goose.honk();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/composite/Flock.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.composite;
2 |
3 | import com.baron.patterns.compound.model.Quackable;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Iterator;
7 |
8 | /**
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class Flock implements Quackable {
14 |
15 | ArrayList quackers = new ArrayList();
16 |
17 | public void add(Quackable quacker) {
18 | quackers.add(quacker);
19 | }
20 |
21 | @Override
22 | public void quack() {
23 |
24 | Iterator iterator = quackers.iterator();
25 | while (iterator.hasNext()) {
26 | Quackable quacker = iterator.next();
27 | quacker.quack();
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/decorator/QuackCounter.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.decorator;
2 |
3 | import com.baron.patterns.compound.model.Quackable;
4 |
5 | /**
6 | * 装饰鸭子用于统计鸭子叫的次数
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class QuackCounter implements Quackable {
13 |
14 | Quackable duck;
15 | static int numberOfQuacks;
16 |
17 | public QuackCounter(Quackable duck) {
18 | this.duck = duck;
19 | }
20 |
21 | @Override
22 | public void quack() {
23 | duck.quack();
24 | numberOfQuacks++;
25 | }
26 |
27 | public static int getQuacks() {
28 | return numberOfQuacks;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/factory/AbstractDuckFactory.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.factory;
2 |
3 | import com.baron.patterns.compound.model.Quackable;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public abstract class AbstractDuckFactory {
11 |
12 | public abstract Quackable createMallardDuck();
13 | public abstract Quackable createRedheadDuck();
14 | public abstract Quackable createDuckCall();
15 | public abstract Quackable createRubberDuck();
16 | }
17 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/factory/CountingDuckFactory.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.factory;
2 |
3 | import com.baron.patterns.compound.decorator.QuackCounter;
4 | import com.baron.patterns.compound.model.*;
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public class CountingDuckFactory extends AbstractDuckFactory {
12 |
13 | @Override
14 | public Quackable createMallardDuck() {
15 | return new QuackCounter(new MallardDuck());
16 | }
17 |
18 | @Override
19 | public Quackable createRedheadDuck() {
20 | return new QuackCounter(new RedheadDuck());
21 | }
22 |
23 | @Override
24 | public Quackable createDuckCall() {
25 | return new QuackCounter(new DuckCall());
26 | }
27 |
28 | @Override
29 | public Quackable createRubberDuck() {
30 | return new QuackCounter(new RubberDuck());
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/factory/DuckFactory.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.factory;
2 |
3 | import com.baron.patterns.compound.model.*;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class DuckFactory extends AbstractDuckFactory {
11 |
12 | @Override
13 | public Quackable createMallardDuck() {
14 | return new MallardDuck();
15 | }
16 |
17 | @Override
18 | public Quackable createRedheadDuck() {
19 | return new RedheadDuck();
20 | }
21 |
22 | @Override
23 | public Quackable createDuckCall() {
24 | return new DuckCall();
25 | }
26 |
27 | @Override
28 | public Quackable createRubberDuck() {
29 | return new RubberDuck();
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/model/DuckCall.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.model;
2 |
3 | /**
4 | * 鸭鸣器
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class DuckCall implements Quackable {
11 |
12 | @Override
13 | public void quack() {
14 | System.out.println("Kwak");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/model/Goose.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.model;
2 |
3 | /**
4 | * 鹅
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class Goose {
11 |
12 | public void honk(){
13 | System.out.println("Honk");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/model/MallardDuck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.model;
2 |
3 | /**
4 | * 绿头鸭
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class MallardDuck implements Quackable {
11 |
12 | @Override
13 | public void quack() {
14 | System.out.println("Quack");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/model/Quackable.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.model;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public interface Quackable {
9 |
10 | void quack();
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/model/RedheadDuck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.model;
2 |
3 | /**
4 | * 红头鸭
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class RedheadDuck implements Quackable {
11 | @Override
12 | public void quack() {
13 | System.out.println("Quack");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/model/RubberDuck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.model;
2 |
3 | /**
4 | * 橡皮鸭
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class RubberDuck implements Quackable {
11 |
12 | @Override
13 | public void quack() {
14 | System.out.println("Squeak");
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/observer/Observer.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.observer;
2 |
3 | /**
4 | *
5 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
6 | * @version 1.0
7 | * @since 1.0
8 | */
9 | public interface Observer {
10 |
11 | void update(QuackObservable duck);
12 | }
13 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/compound/observer/QuackObservable.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.compound.observer;
2 |
3 | import java.util.Observer;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public interface QuackObservable {
11 |
12 | void registerObservable(Observer observer);
13 |
14 | void notifyObservers();
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/DecoratorPatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator;
2 |
3 | import com.baron.patterns.decorator.component.Beverage;
4 | import com.baron.patterns.decorator.component.DarkRoast;
5 | import com.baron.patterns.decorator.component.Decaf;
6 | import com.baron.patterns.decorator.decorator.Mocha;
7 | import com.baron.patterns.decorator.decorator.Whip;
8 |
9 | /**
10 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
11 | * @version 1.0
12 | * @since 1.0
13 | */
14 | public class DecoratorPatternTest {
15 |
16 | public static void main(String[] args) {
17 |
18 | Beverage beverage = new Decaf();//这是顾客要的脱因咖啡
19 | beverage = new Mocha(beverage);//顾客想要摩卡,用摩卡装饰
20 | beverage = new Whip(beverage);//顾客想要奶泡,用奶泡装饰
21 | System.out.println(beverage.getDescription() + " 共花费$" + beverage.cost());
22 |
23 | Beverage beverage2 = new DarkRoast();//这是顾客要的深培咖啡
24 | beverage2 = new Mocha(beverage2);//顾客想要摩卡,用摩卡装饰
25 | beverage2 = new Mocha(beverage2);//顾客想要摩卡,用摩卡装饰
26 | beverage2 = new Whip(beverage2);//顾客想要奶泡,用奶泡装饰
27 | System.out.println(beverage2.getDescription() + " 共花费$" + beverage2.cost());
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/README.md:
--------------------------------------------------------------------------------
1 | #装饰者模式(DecoratorPattern)
2 |
3 | ####定义:
4 | > 动态的将责任附加到对象上。若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
5 |
6 | ###设计原则
7 | 1. 类应该低扩展开放,对修改关闭。
8 |
9 |
10 | ###类图
11 |
12 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/component/Beverage.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator.component;
2 |
3 | /**
4 | * 饮料,被装饰者抽象基类
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public abstract class Beverage {
11 |
12 | String description = "还不知道是什么饮料";
13 |
14 | /**
15 | * @return 返回饮料详细描述
16 | */
17 | public String getDescription() {
18 | return description;
19 | }
20 |
21 | /**
22 | * 计算价格
23 | *
24 | * @return double类型的价格
25 | */
26 | public abstract double cost();
27 | }
28 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/component/DarkRoast.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator.component;
2 |
3 | /**
4 | * 深培咖啡,被装饰者
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class DarkRoast extends Beverage {
11 |
12 | public DarkRoast() {
13 | description = "深培咖啡";
14 | }
15 |
16 | @Override
17 | public double cost() {
18 | return 3.99;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/component/Decaf.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator.component;
2 |
3 | /**
4 | * 脱因咖啡,被装饰者
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class Decaf extends Beverage {
11 |
12 | public Decaf(){
13 | description = "脱因咖啡";
14 | }
15 |
16 | @Override
17 | public double cost() {
18 | return 4.89;
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/decorator/CondimentDecorator.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator.decorator;
2 |
3 | import com.baron.patterns.decorator.component.Beverage;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public abstract class CondimentDecorator extends Beverage {
11 |
12 | public abstract String getDescription();
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/decorator/Mocha.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator.decorator;
2 |
3 | import com.baron.patterns.decorator.component.Beverage;
4 |
5 | /**
6 | * 摩卡,装饰者
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class Mocha extends CondimentDecorator {
13 |
14 | private Beverage beverage;
15 |
16 | public Mocha(Beverage beverage) {
17 | this.beverage = beverage;
18 | }
19 |
20 | @Override
21 | public String getDescription() {
22 | return this.beverage.getDescription() + ",加摩卡";
23 | }
24 |
25 | @Override
26 | public double cost() {
27 | return this.beverage.cost() + 1.99;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/decorator/decorator/Whip.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.decorator.decorator;
2 |
3 | import com.baron.patterns.decorator.component.Beverage;
4 |
5 | /**
6 | * 奶泡,装饰者
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class Whip extends CondimentDecorator {
13 |
14 | private Beverage beverage;
15 |
16 | public Whip(Beverage beverage) {
17 | this.beverage = beverage;
18 | }
19 |
20 | @Override
21 | public String getDescription() {
22 | return this.beverage.getDescription() + ",加奶泡";
23 | }
24 |
25 | @Override
26 | public double cost() {
27 | return this.beverage.cost() + .88;
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/facade/FacadePatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.facade;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public class FacadePatternTest {
9 | }
10 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/facade/README.md:
--------------------------------------------------------------------------------
1 | #外观模式(FacadePattern)
2 |
3 | ####定义:
4 | > 提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。
5 |
6 | ###设计原则
7 | 1.最少知识原则:只和你的密友交谈
8 |
9 |
10 | ###类图
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/FactoryPatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory;
2 |
3 | import com.baron.patterns.factory.factory.ChicagoPizzaStore;
4 | import com.baron.patterns.factory.factory.NYPizzaStore;
5 | import com.baron.patterns.factory.product.Pizza;
6 |
7 | /**
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class FactoryPatternTest {
13 |
14 | public static void main(String[] args) {
15 | NYPizzaStore nyPizzaStore = new NYPizzaStore();
16 | Pizza pizza = nyPizzaStore.orderPizza("cheese");
17 | System.out.println("二狗子定了一个" + pizza.getName() + "\n");
18 |
19 | ChicagoPizzaStore chicagoPizzaStore = new ChicagoPizzaStore();
20 | pizza = chicagoPizzaStore.orderPizza("clam");
21 | System.out.println("隔壁老王定了一个" + pizza.getName() + "\n");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/README.md:
--------------------------------------------------------------------------------
1 | #工厂模式(FactoryPattern)
2 |
3 | ####定义:
4 | > 定义了一个创建对象的接口,但由子类来决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
5 |
6 | ###设计原则
7 | 1.
8 |
9 |
10 | ###类图
11 |
12 |
13 |
14 | > 注:该Demo中还包含了模板方法模式
15 |
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/factory/ChicagoPizzaStore.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.factory;
2 |
3 | import com.baron.patterns.factory.product.*;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class ChicagoPizzaStore extends PizzaStore {
11 | @Override
12 | Pizza createPizza(String type) {
13 |
14 | if ("cheese".equals(type)) {
15 | return new ChicagoStyleCheesePizza();
16 | } else if ("clam".equals(type)) {
17 | return new ChicagoStyleClamPizza();
18 | }
19 | return null;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/factory/NYPizzaStore.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.factory;
2 |
3 | import com.baron.patterns.factory.product.NYStyleCheesePizza;
4 | import com.baron.patterns.factory.product.NYStyleClamPizza;
5 | import com.baron.patterns.factory.product.Pizza;
6 |
7 | /**
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class NYPizzaStore extends PizzaStore {
13 |
14 | @Override
15 | Pizza createPizza(String type) {
16 |
17 | if ("cheese".equals(type)) {
18 | return new NYStyleCheesePizza();
19 | } else if ("clam".equals(type)) {
20 | return new NYStyleClamPizza();
21 | }
22 | return null;
23 | }
24 | }
25 |
26 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/factory/PizzaStore.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.factory;
2 |
3 | import com.baron.patterns.factory.product.Pizza;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public abstract class PizzaStore {
11 |
12 | abstract Pizza createPizza(String type);
13 |
14 | public Pizza orderPizza(String type) {
15 |
16 | Pizza pizza = createPizza(type);
17 |
18 | pizza.prepare();
19 | pizza.bake();
20 | pizza.cut();
21 | pizza.box();
22 |
23 | return pizza;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/product/ChicagoStyleCheesePizza.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.product;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public class ChicagoStyleCheesePizza extends Pizza {
9 |
10 | public ChicagoStyleCheesePizza() {
11 | name = "芝加哥田园风光芝士披萨";
12 | dough = "朝鲜进贡的金三胖专用白面";
13 | sauce = "菠萝、小番茄、胡萝卜";
14 | }
15 |
16 | @Override
17 | public void cut() {
18 | System.out.println("这次咱切成五角形");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/product/ChicagoStyleClamPizza.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.product;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public class ChicagoStyleClamPizza extends Pizza {
9 |
10 | public ChicagoStyleClamPizza(){
11 | name = "芝加哥番茄炒蛋披萨";
12 | dough = "朝鲜进贡的金三胖专用白面";
13 | sauce = "高大上的番茄炒蛋";
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/product/NYStyleCheesePizza.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.product;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public class NYStyleCheesePizza extends Pizza {
9 |
10 | public NYStyleCheesePizza(){
11 | name = "纽约风情烤肉披萨";
12 | dough = "中国大东北进口的高级白面";
13 | sauce = "纽约风味的风情烤肉";
14 | }
15 |
16 | @Override
17 | public void box() {
18 | System.out.println("将披萨装进印有纽约店logo的包装盒里");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/product/NYStyleClamPizza.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.product;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public class NYStyleClamPizza extends Pizza {
9 |
10 | public NYStyleClamPizza() {
11 | name = "纽约韭菜炒蛋披萨";
12 | dough = "中国大东北进口的高级白面";
13 | sauce = "韭菜、鸡蛋";
14 | }
15 |
16 | @Override
17 | public void box() {
18 | System.out.println("将披萨装进印有纽约店logo的包装盒里");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/factory/product/Pizza.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.factory.product;
2 |
3 | import java.util.ArrayList;
4 |
5 | /**
6 | * 抽象披萨类
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public abstract class Pizza {
13 |
14 | String name;//披萨名
15 | String dough;//面团
16 | String sauce;//调料
17 |
18 | public void prepare(){
19 | System.out.println("我们准备制作"+name);
20 | System.out.println("开始和面....,采用的是"+dough);
21 | System.out.println("添加"+sauce);
22 | }
23 |
24 | /**
25 | * 烘培
26 | */
27 | public void bake(){
28 | System.out.println("烘培25分中");
29 | }
30 |
31 | /**
32 | * 切块
33 | */
34 | public void cut(){
35 | System.out.println("把披萨切成斜片");
36 | }
37 |
38 | /**
39 | * 包装
40 | */
41 | public void box(){
42 | System.out.println("将披萨装载连锁店的盒子里");
43 | }
44 |
45 | public String getName() {
46 | return name;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/iterator/README.md:
--------------------------------------------------------------------------------
1 | #迭代器模式(IteratorPattern)
2 |
3 | ####定义:
4 | > 提供一种方法顺序访问一个聚合对象中的各个元素,而不是暴露起内部的表示。
5 |
6 | ###设计原则
7 | 1.单一职责原则:一个类应该只有一个引起变化的原因。
8 |
9 | ###类图
10 |
11 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/ObserverPatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer;
2 |
3 | import com.baron.patterns.observer.observer.CurrentConditionsDisplay;
4 | import com.baron.patterns.observer.observer.ForecastDisplay;
5 | import com.baron.patterns.observer.subject.WeatherData;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * 模拟一个气象站(测试类)
12 | *
13 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
14 | * @version 1.0
15 | * @see com.baron.patterns.observer.subject.WeatherData
16 | * @see com.baron.patterns.observer.observer.CurrentConditionsDisplay
17 | * @see com.baron.patterns.observer.observer.ForecastDisplay
18 | * @since 1.0
19 | */
20 | public class ObserverPatternTest {
21 |
22 | public static void main(String[] args) {
23 |
24 | WeatherData weatherData = new WeatherData();
25 | CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
26 | ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
27 |
28 | List forecastTemperatures = new ArrayList();
29 | forecastTemperatures.add(22f);
30 | forecastTemperatures.add(-1f);
31 | forecastTemperatures.add(9f);
32 | forecastTemperatures.add(23f);
33 | forecastTemperatures.add(27f);
34 | forecastTemperatures.add(30f);
35 | forecastTemperatures.add(10f);
36 | weatherData.setMeasurements(22f, 0.8f, 1.2f, forecastTemperatures);
37 |
38 | System.out.println("============================================");
39 |
40 | List forecastTemperatures2 = new ArrayList();
41 | forecastTemperatures2.add(2f);
42 | forecastTemperatures2.add(9f);
43 | forecastTemperatures2.add(9f);
44 | forecastTemperatures2.add(13f);
45 | forecastTemperatures2.add(17f);
46 | forecastTemperatures2.add(20f);
47 | forecastTemperatures2.add(20f);
48 | weatherData.setMeasurements(2f, 0.7f, 1.1f, forecastTemperatures2);
49 |
50 | System.out.println("============================================");
51 |
52 | List forecastTemperatures3 = new ArrayList();
53 | forecastTemperatures3.add(21f);
54 | forecastTemperatures3.add(11f);
55 | forecastTemperatures3.add(12f);
56 | forecastTemperatures3.add(33f);
57 | forecastTemperatures3.add(37f);
58 | forecastTemperatures3.add(30f);
59 | forecastTemperatures3.add(20f);
60 | weatherData.setMeasurements(21f, 0.6f, 0.9f, forecastTemperatures3);
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/README.md:
--------------------------------------------------------------------------------
1 | #观察者模式(ObserverPattern)
2 |
3 | ####定义:
4 | > 定义了对象间一对多依赖,这样一来,当一个对象发生状态时,他的所有依赖者都会收到通知并自动更新。
5 |
6 | ####观察者模式介绍
7 | [http://www.jianshu.com/p/d55ee6e83d66](http://www.jianshu.com/p/d55ee6e83d66)
8 |
9 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/observer/CurrentConditionsDisplay.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer.observer;
2 |
3 | import com.baron.patterns.observer.subject.WeatherData;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class CurrentConditionsDisplay implements Observer, DisplayElement {
11 |
12 | private WeatherData weatherData;
13 |
14 | private float temperature;//温度
15 | private float humidity;//湿度
16 | private float pressure;//气压
17 |
18 | public CurrentConditionsDisplay(WeatherData weatherData) {
19 | this.weatherData = weatherData;
20 | this.weatherData.registerObserver(this);
21 | }
22 |
23 | @Override
24 | public void display() {
25 | System.out.println("当前温度为:" + this.temperature + "℃");
26 | System.out.println("当前湿度为:" + this.humidity);
27 | System.out.println("当前气压为:" + this.pressure);
28 | }
29 |
30 | @Override
31 | public void update() {
32 | this.temperature = this.weatherData.getTemperature();
33 | this.humidity = this.weatherData.getHumidity();
34 | this.pressure = this.weatherData.getPressure();
35 | display();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/observer/DisplayElement.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer.observer;
2 |
3 | /**
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @version 1.0
6 | * @since 1.0
7 | */
8 | public interface DisplayElement {
9 |
10 | void display();
11 | }
12 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/observer/ForecastDisplay.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer.observer;
2 |
3 | import com.baron.patterns.observer.subject.WeatherData;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class ForecastDisplay implements Observer, DisplayElement {
13 |
14 | private WeatherData weatherData;
15 |
16 | private List forecastTemperatures;//未来几天的温度
17 |
18 | public ForecastDisplay(WeatherData weatherData) {
19 | this.weatherData = weatherData;
20 | this.weatherData.registerObserver(this);
21 | }
22 |
23 | @Override
24 | public void display() {
25 | System.out.println("未来几天的气温");
26 | int count = forecastTemperatures.size();
27 | for (int i = 0; i < count; i++) {
28 | System.out.println("第" + i + "天:" + forecastTemperatures.get(i) + "℃");
29 | }
30 | }
31 |
32 | @Override
33 | public void update() {
34 | this.forecastTemperatures = this.weatherData.getForecastTemperatures();
35 | display();
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/observer/Observer.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer.observer;
2 |
3 | /**
4 | * 观察者
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public interface Observer {
11 |
12 | void update();
13 | }
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/subject/Subject.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer.subject;
2 |
3 | import com.baron.patterns.observer.observer.Observer;
4 |
5 | /**
6 | * 主题(发布者、被观察者)
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public interface Subject {
13 |
14 | /**
15 | * 注册观察者
16 | */
17 | void registerObserver(Observer observer);
18 |
19 | /**
20 | * 移除观察者
21 | */
22 | void removeObserver(Observer observer);
23 |
24 | /**
25 | * 通知观察者
26 | */
27 | void notifyObservers();
28 | }
29 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/observer/subject/WeatherData.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.observer.subject;
2 |
3 | import com.baron.patterns.observer.observer.Observer;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 |
8 | /**
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class WeatherData implements Subject {
14 |
15 | private List observers;
16 |
17 | private float temperature;//温度
18 | private float humidity;//湿度
19 | private float pressure;//气压
20 | private List forecastTemperatures;//未来几天的温度
21 |
22 | public WeatherData() {
23 | this.observers = new ArrayList();
24 | }
25 |
26 | @Override
27 | public void registerObserver(Observer observer) {
28 | this.observers.add(observer);
29 | }
30 |
31 | @Override
32 | public void removeObserver(Observer observer) {
33 | this.observers.remove(observer);
34 | }
35 |
36 | @Override
37 | public void notifyObservers() {
38 | for (Observer observer : observers) {
39 | observer.update();
40 | }
41 | }
42 |
43 | public void measurementsChanged() {
44 | notifyObservers();
45 | }
46 |
47 | public void setMeasurements(float temperature, float humidity, float pressure, List forecastTemperatures) {
48 | this.temperature = temperature;
49 | this.humidity = humidity;
50 | this.pressure = pressure;
51 | this.forecastTemperatures = forecastTemperatures;
52 | measurementsChanged();
53 | }
54 |
55 | public float getTemperature() {
56 | return temperature;
57 | }
58 |
59 | public float getHumidity() {
60 | return humidity;
61 | }
62 |
63 | public float getPressure() {
64 | return pressure;
65 | }
66 |
67 | public List getForecastTemperatures() {
68 | return forecastTemperatures;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | *
4 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
5 | * @since 1.0
6 | * @version 1.0
7 | */
8 | package com.baron.patterns;
--------------------------------------------------------------------------------
/src/com/baron/patterns/proxy/MyRemote.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.proxy;
2 |
3 | import java.rmi.Remote;
4 | import java.rmi.RemoteException;
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public interface MyRemote extends Remote {
12 |
13 | public String sayHello() throws RemoteException;
14 | }
15 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/proxy/MyRemoteImpl.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.proxy;
2 |
3 | import java.rmi.RemoteException;
4 | import java.rmi.server.UnicastRemoteObject;
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
12 |
13 | protected MyRemoteImpl() throws RemoteException {
14 |
15 | }
16 |
17 | @Override
18 | public String sayHello() throws RemoteException {
19 | return "Server says, 'Hey'";
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/proxy/README.md:
--------------------------------------------------------------------------------
1 | #代理模式(ProxyPattern)
2 |
3 | ####定义:
4 | >
5 |
6 | ###设计原则
7 | 1.
8 |
9 |
10 | ###类图
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/singleton/README.md:
--------------------------------------------------------------------------------
1 | #单例模式(SingletonPattern)
2 |
3 | ####定义:
4 | > 确保一个类只有一个实例,并提供一个全局的访问点。
5 |
6 | ###设计原则
7 | 1.
8 |
9 | ###类图
10 |
11 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/singleton/Singleton.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.singleton;
2 |
3 | /**
4 | * 单例类,双重检查加锁
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class Singleton {
11 |
12 | private volatile static Singleton uniqueInstance;
13 |
14 | private Singleton() {}
15 |
16 | public static Singleton getInstance() {
17 | if (uniqueInstance == null) {
18 | synchronized (Singleton.class) {
19 | if (uniqueInstance == null) {
20 | uniqueInstance = new Singleton();
21 | }
22 | }
23 | }
24 | return uniqueInstance;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/README.md:
--------------------------------------------------------------------------------
1 | #状态模式(StatePattern)
2 |
3 | ####定义:
4 | > 允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。
5 |
6 | ###设计原则
7 | 1.
8 |
9 |
10 | ###类图
11 |
12 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/StatePatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state;
2 |
3 | import com.baron.patterns.state.state.GumballMachine;
4 |
5 | /**
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class StatePatternTest {
11 |
12 | public static void main(String[] args) {
13 |
14 | GumballMachine machine = new GumballMachine(10);
15 | machine.insertQuarter();
16 | machine.turnCrank();
17 | System.out.println("剩余糖果:"+machine.getCount()+"\n");
18 |
19 | machine.ejectQuarter();
20 | machine.turnCrank();
21 | System.out.println("剩余糖果:" + machine.getCount() + "\n");
22 |
23 | machine.insertQuarter();
24 | machine.ejectQuarter();
25 | System.out.println("剩余糖果:" + machine.getCount() + "\n");
26 |
27 | machine.insertQuarter();
28 | machine.turnCrank();
29 | System.out.println("剩余糖果:"+machine.getCount()+"\n");
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/state/GumballMachine.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state.state;
2 |
3 | import com.baron.patterns.state.state.*;
4 |
5 | /**
6 | * 糖果售卖机
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class GumballMachine {
13 |
14 | private HasQuarterState hasQuarterState;
15 | private NoQuarterState noQuarterState;
16 | private SoldOutState soldOutState;
17 | private SoldState soldState;
18 |
19 | private State state = soldOutState;
20 | private int count = 0;//售卖机剩余糖果数量
21 |
22 | public GumballMachine(int numberGumballs) {
23 | this.hasQuarterState = new HasQuarterState(this);
24 | this.noQuarterState = new NoQuarterState(this);
25 | this.soldOutState = new SoldOutState(this);
26 | this.soldState = new SoldState(this);
27 | this.count = numberGumballs;
28 | if (numberGumballs > 0) {
29 | this.state = noQuarterState;
30 | }
31 | }
32 |
33 | public void insertQuarter() {
34 | state.insertQuarter();
35 | }
36 |
37 | public void ejectQuarter() {
38 | state.ejectQuarter();
39 | }
40 |
41 | public void turnCrank() {
42 | state.turnCrank();
43 | state.dispense();
44 | }
45 |
46 |
47 | void releaseBall() {
48 | System.out.println("售出糖果");
49 | if (count > 0) {
50 | count--;
51 | }
52 | }
53 |
54 | public void setState(State state) {
55 | this.state = state;
56 | }
57 |
58 | public State getState() {
59 | return state;
60 | }
61 |
62 | public int getCount() {
63 | return count;
64 | }
65 |
66 | public HasQuarterState getHasQuarterState() {
67 | return hasQuarterState;
68 | }
69 |
70 | public NoQuarterState getNoQuarterState() {
71 | return noQuarterState;
72 | }
73 |
74 | public SoldOutState getSoldOutState() {
75 | return soldOutState;
76 | }
77 |
78 | public SoldState getSoldState() {
79 | return soldState;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/state/HasQuarterState.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state.state;
2 |
3 | /**
4 | * 状态之有25美分了
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class HasQuarterState implements State {
11 |
12 | private GumballMachine gumballMachine;
13 |
14 | public HasQuarterState(GumballMachine gumballMachine) {
15 | this.gumballMachine = gumballMachine;
16 | }
17 |
18 | @Override
19 | public void insertQuarter() {
20 | System.out.println("钱多烧得晃是不?你特么不给给钱了吗?!!");
21 | }
22 |
23 | @Override
24 | public void ejectQuarter() {
25 | System.out.println("好吧,钱退给你");
26 | //退币后修改为无币状态
27 | this.gumballMachine.setState(this.gumballMachine.getNoQuarterState());
28 | }
29 |
30 | @Override
31 | public void turnCrank() {
32 | System.out.println("波动了开关");
33 | }
34 |
35 | @Override
36 | public void dispense() {
37 | gumballMachine.releaseBall();
38 | if(gumballMachine.getCount()>0){
39 | this.gumballMachine.setState(this.gumballMachine.getNoQuarterState());
40 | }else{
41 | System.out.println("妈蛋,糖果卖完了!!!");
42 | this.gumballMachine.setState(this.gumballMachine.getSoldOutState());
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/state/NoQuarterState.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state.state;
2 |
3 | /**
4 | * 状态之木有钱
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class NoQuarterState implements State {
11 |
12 | GumballMachine gumballMachine;
13 |
14 | public NoQuarterState(GumballMachine gumballMachine) {
15 | this.gumballMachine = gumballMachine;
16 | }
17 |
18 | @Override
19 | public void insertQuarter() {
20 | //这里加入投币代码
21 | System.out.println("客官您投入了25分硬币");
22 | gumballMachine.setState(gumballMachine.getHasQuarterState());
23 | }
24 |
25 | @Override
26 | public void ejectQuarter() {
27 | //这里加入退币代码
28 | System.out.println("小子!你特么都没投钱,还要求退币?!!!");
29 | }
30 |
31 | @Override
32 | public void turnCrank() {
33 | //这里加入转动曲柄代码
34 | System.out.println("没给钱拨你妹的曲柄开关啊");
35 | }
36 |
37 | @Override
38 | public void dispense() {
39 | //这里加入发糖果代码
40 | System.out.println("哥哥,咱先给钱好不好");
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/state/SoldOutState.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state.state;
2 |
3 | /**
4 | * 状态之售罄
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class SoldOutState implements State {
11 |
12 | private GumballMachine gumballMachine;
13 |
14 | public SoldOutState(GumballMachine gumballMachine) {
15 | this.gumballMachine = gumballMachine;
16 | }
17 |
18 | @Override
19 | public void insertQuarter() {
20 | System.out.println("不能投币,糖果卖完了!");
21 | }
22 |
23 | @Override
24 | public void ejectQuarter() {
25 | System.out.println("无法退币,你没投币!");
26 | }
27 |
28 | @Override
29 | public void turnCrank() {
30 | System.out.println("没糖果了,拨你妹的曲柄啊!!!");
31 | }
32 |
33 | @Override
34 | public void dispense() {
35 | System.out.println("没有可以售出的糖果");
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/state/SoldState.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state.state;
2 |
3 | /**
4 | * 状态之售出糖果
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public class SoldState implements State {
11 |
12 | GumballMachine gumballMachine;
13 |
14 | public SoldState(GumballMachine gumballMachine) {
15 | this.gumballMachine = gumballMachine;
16 | }
17 |
18 | @Override
19 | public void insertQuarter() {
20 | System.out.println("等等骚年,我们已经给你一个糖果了");
21 | }
22 |
23 | @Override
24 | public void ejectQuarter() {
25 | System.out.println("抱歉,你已经拨动曲柄,现在无法退币了");
26 | }
27 |
28 | @Override
29 | public void turnCrank() {
30 | System.out.println("好的,我们将会给你颗糖果");
31 | }
32 |
33 | @Override
34 | public void dispense() {
35 | gumballMachine.releaseBall();
36 | if (gumballMachine.getCount() > 0) {
37 | System.out.println("发糖果啦");
38 | gumballMachine.setState(gumballMachine.getNoQuarterState());
39 | } else {
40 | System.out.println("妈蛋,糖果卖完了!!!");
41 | gumballMachine.setState(gumballMachine.getSoldOutState());
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/state/state/State.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.state.state;
2 |
3 | /**
4 | * 状态接口,所有糖果机的状态都必须实现这一接口。接口的方法直接隐射到糖果机上可能发生的动作.
5 | *
6 | * 准确的说接口中的方法是糖果机在该状态下的操作
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public interface State {
13 |
14 | /**
15 | * 投入25分硬币
16 | */
17 | void insertQuarter();
18 |
19 | /**
20 | * 吐出25分硬币
21 | */
22 | void ejectQuarter();
23 |
24 | /**
25 | * 转动曲柄
26 | */
27 | void turnCrank();
28 |
29 | /**
30 | * 发放糖果
31 | */
32 | void dispense();
33 | }
34 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/Duck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy;
2 |
3 | import com.baron.patterns.strategy.behavior.FlyBehavior;
4 | import com.baron.patterns.strategy.behavior.QuackBehavior;
5 |
6 | /**
7 | * 鸭子抽象类(基类)
8 | *
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public abstract class Duck {
14 |
15 |
16 | protected FlyBehavior flyBehavior;
17 | protected QuackBehavior quackBehavior;
18 |
19 | /**
20 | * 鸭子的外观
21 | */
22 | public abstract void display();
23 |
24 | /**
25 | * 游泳
26 | */
27 | public void swim() {
28 | System.out.println("我游啊游啊游......");
29 | }
30 |
31 | /**
32 | * 飞行
33 | */
34 | public void performFly() {
35 | //将飞行这件小事委托给行为类
36 | if (flyBehavior != null)
37 | flyBehavior.fly();
38 | }
39 |
40 | /**
41 | * 叫
42 | */
43 | public void performQuack() {
44 | //将浪叫这件小事委托给行为类
45 | if (quackBehavior != null)
46 | quackBehavior.quack();
47 | }
48 |
49 | /**
50 | * 设置鸭子的飞行行为
51 | *
52 | * @param flyBehavior 飞
53 | */
54 | public void setFlyBehavior(FlyBehavior flyBehavior) {
55 | this.flyBehavior = flyBehavior;
56 | }
57 |
58 | /**
59 | * 设置鸭子叫的行为
60 | *
61 | * @param quackBehavior 叫
62 | */
63 | public void setQuackBehavior(QuackBehavior quackBehavior) {
64 | this.quackBehavior = quackBehavior;
65 | }
66 |
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/MallardDuck.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy;
2 |
3 | import com.baron.patterns.strategy.behavior.impl.FlyNoWay;
4 | import com.baron.patterns.strategy.behavior.impl.Quack;
5 |
6 | /**
7 | * Duck的子类,野鸭
8 | *
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class MallardDuck extends Duck {
14 |
15 | public MallardDuck() {
16 | flyBehavior = new FlyNoWay();
17 | quackBehavior = new Quack();
18 | }
19 |
20 | @Override
21 | public void display() {
22 | System.out.println("我可是一只货真价实的野鸭子!");
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/README.md:
--------------------------------------------------------------------------------
1 | #策略模式(StrategyPattern)
2 |
3 | ####定义:
4 | > 定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
5 |
6 | ###设计原则
7 | 1. 找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起。
8 | 2. 针对接口变成,而不是针对实现编程。
9 | > *“针对接口编程”的真正意思是针对超类型编程*。
10 | 3. 多用组合,少用集成。
11 |
12 | ###类图
13 |
14 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/StrategyPatternTest.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy;
2 |
3 | import com.baron.patterns.strategy.behavior.impl.FlyWithWings;
4 | import com.baron.patterns.strategy.behavior.impl.MuteQuack;
5 |
6 | /**
7 | * 鸭子模拟器,测试类
8 | *
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class StrategyPatternTest {
14 |
15 | public static void main(String[] args) {
16 |
17 | Duck duck = new MallardDuck();
18 | duck.swim();
19 | duck.display();
20 | duck.performFly();
21 | duck.performQuack();
22 |
23 | //现在会飞了
24 | duck.setFlyBehavior(new FlyWithWings());
25 | duck.performFly();
26 | //可是不会叫了
27 | duck.setQuackBehavior(new MuteQuack());
28 | duck.performQuack();
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/FlyBehavior.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior;
2 |
3 | /**
4 | * 鸭子飞行的行为
5 | *
6 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
7 | * @version 1.0
8 | * @since 1.0
9 | */
10 | public interface FlyBehavior {
11 |
12 | /**
13 | * 飞行
14 | */
15 | void fly();
16 | }
17 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/QuackBehavior.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior;
2 |
3 | /**
4 | * Created by baron on 15/12/14.
5 | *
6 | * 鸭子叫的行为
7 | */
8 | public interface QuackBehavior {
9 |
10 | /**
11 | * 叫
12 | */
13 | void quack();
14 | }
15 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/impl/FlyNoWay.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior.impl;
2 |
3 | import com.baron.patterns.strategy.behavior.FlyBehavior;
4 |
5 | /**
6 | * 飞行行为的实现类(飞不了)
7 | *
8 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
9 | * @version 1.0
10 | * @since 1.0
11 | */
12 | public class FlyNoWay implements FlyBehavior {
13 |
14 | @Override
15 | public void fly() {
16 | //什么都不做
17 | System.out.println("我不能飞 :(");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/impl/FlyWithWings.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior.impl;
2 |
3 | import com.baron.patterns.strategy.behavior.FlyBehavior;
4 |
5 |
6 | /**
7 | * 飞行行为的实现类,执行具体的飞行行为
8 | *
9 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
10 | * @version 1.0
11 | * @since 1.0
12 | */
13 | public class FlyWithWings implements FlyBehavior {
14 | @Override
15 | public void fly() {
16 | //挥动翅膀飞行
17 | System.out.println("我飞起来了!!!");
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/impl/MuteQuack.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior.impl;
2 |
3 | import com.baron.patterns.strategy.behavior.QuackBehavior;
4 |
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public class MuteQuack implements QuackBehavior {
12 | @Override
13 | public void quack() {
14 | //什么都不做,不会叫
15 | System.out.print("整个世界都清净了.... ps:我不会叫 :(");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/impl/Quack.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior.impl;
2 |
3 | import com.baron.patterns.strategy.behavior.QuackBehavior;
4 |
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public class Quack implements QuackBehavior {
12 | @Override
13 | public void quack() {
14 | //实现鸭子嘎嘎叫
15 | System.out.println("嘎嘎嘎嘎嘎嘎嘎......");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/strategy/behavior/impl/Squeak.java:
--------------------------------------------------------------------------------
1 | package com.baron.patterns.strategy.behavior.impl;
2 |
3 | import com.baron.patterns.strategy.behavior.QuackBehavior;
4 |
5 |
6 | /**
7 | * @author Baron Zhang (baron[dot]zhanglei[at]gmail[dot]com)
8 | * @version 1.0
9 | * @since 1.0
10 | */
11 | public class Squeak implements QuackBehavior {
12 | @Override
13 | public void quack() {
14 | //实现鸭子吱吱叫
15 | System.out.println("吱吱吱吱吱吱吱......");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/com/baron/patterns/template/README.md:
--------------------------------------------------------------------------------
1 | #模板模式(TemplatePattern)
2 |
3 | ####定义:
4 | >
5 |
6 | ###设计原则
7 | 1.
8 |
9 | ###类图
10 |
11 |
--------------------------------------------------------------------------------