Skip to content

Commit 8dfa9b1

Browse files
'add'
1 parent eac1b2f commit 8dfa9b1

54 files changed

Lines changed: 1973 additions & 43 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17.7 KB
Loading
27.8 KB
Loading
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,178 @@ public class FileCopyData {
386386

387387
```
388388

389+
## 装饰模式:IO设计的核心思想
390+
391+
在不影响原有对象的前提下,无侵入的给一个对象增一些额外的功能。
392+
393+
new InputStreamReader( new FileInputStream(new File("d:/abc.txt")) ) ;
394+
395+
396+
397+
398+
399+
装饰模式
400+
401+
![1576461139194](IO.assets/1576461139194.png)
402+
403+
装饰者:需要持有主题(被装饰者)的一个引用。
404+
405+
![1576462471474](IO.assets/1576462471474.png)
406+
407+
装饰主题接口
408+
409+
```java
410+
package decorator;
411+
412+
/*
413+
* Created by 颜群
414+
*/
415+
public interface Phone {
416+
void call();//基础功能
417+
}
418+
419+
```
420+
421+
装饰主题的基础功能
422+
423+
```java
424+
package decorator;
425+
426+
/*
427+
* Created by 颜群
428+
*/
429+
public class ConcretePhone implements Phone {
430+
@Override
431+
public void call() {
432+
System.out.println("打电话....");
433+
}
434+
}
435+
436+
```
437+
438+
装饰者抽象类
439+
440+
```java
441+
package decorator;
442+
443+
/*
444+
* Created by 颜群
445+
*/
446+
public abstract class SmartPhone implements Phone{
447+
private Phone phone ;//装饰者 持有 主题的一个引用
448+
449+
public SmartPhone(){
450+
}
451+
452+
public SmartPhone(Phone phone){
453+
this.phone = phone ;
454+
}
455+
456+
//装饰者 包含 原主题已有的功能
457+
@Override
458+
public void call() {
459+
phone.call();
460+
}
461+
}
462+
463+
```
464+
465+
具体装饰者1
466+
467+
```java
468+
package decorator;
469+
470+
/*
471+
* Created by 颜群
472+
*/
473+
public class ConcreteSmarPhone1 extends SmartPhone{
474+
public ConcreteSmarPhone1(Phone phone){
475+
super(phone) ;
476+
}
477+
478+
public void call(){
479+
super.call();
480+
//额外的新功能
481+
changeColor();
482+
}
483+
484+
public void changeColor(){
485+
System.out.println("智能变色...");
486+
}
487+
488+
489+
490+
}
491+
492+
```
493+
494+
具体装饰者2
495+
496+
```java
497+
package decorator;
498+
499+
/*
500+
* Created by 颜群
501+
*/
502+
public class ConcreteSmarPhone2 extends SmartPhone{
503+
public ConcreteSmarPhone2(Phone phone){
504+
super(phone) ;
505+
}
506+
507+
public void call(){
508+
super.call();
509+
//额外的新功能
510+
changeSize();
511+
}
512+
513+
public void changeSize(){
514+
System.out.println("智能改变尺寸...");
515+
}
516+
517+
518+
519+
}
520+
521+
```
522+
523+
测试类
524+
525+
```java
526+
package decorator;
527+
528+
/*
529+
* Created by 颜群
530+
*/
531+
public class Test {
532+
public static void main(String[] args) {
533+
ConcretePhone phone = new ConcretePhone();
534+
//主题(被装饰者)
535+
phone.call();
536+
System.out.println("---");
537+
538+
ConcreteSmarPhone1 phone1 = new ConcreteSmarPhone1( phone ) ;
539+
phone1.call();
540+
541+
542+
System.out.println("---");
543+
544+
ConcreteSmarPhone2 phone2 = new ConcreteSmarPhone2( phone ) ;
545+
phone2.call();
546+
547+
548+
System.out.println("-------");
549+
550+
SmartPhone smart = new ConcreteSmarPhone2(new ConcreteSmarPhone1( phone )) ;
551+
smart.call();
552+
}
553+
}
554+
555+
```
556+
557+
558+
559+
560+
389561

390562

391563

25.7 KB
Loading
5.63 KB
Loading

0 commit comments

Comments
 (0)