Skip to content

Commit 7e80c08

Browse files
author
when
committed
新增两种设计模式的内容 author:whenknown date:2019/12/26
1 parent 4e20871 commit 7e80c08

File tree

1 file changed

+134
-3
lines changed

1 file changed

+134
-3
lines changed

Java/设计模式.md

Lines changed: 134 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,140 @@ public class Main{
394394

395395

396396

397-
## 2.3 组合模式(Composite Pattern
398-
## 2.4 装饰者模式(Decorator Pattern
399-
## 2.5 外观模式(Facade Pattern
397+
## 2.3 *组合模式(Composite Pattern
398+
399+
> 介绍:JavaAWTSWING中,对ButtonCheckbox是树叶,Container是树枝
400+
>
401+
> 优点:1.高层模块调用简单 2.节点增加自由
402+
>
403+
> 缺点:使用组合时候,叶子和树枝都是实现类,不是接口,违反了依赖倒置原则
404+
>
405+
> 使用场景:部分,整体场景;如:树形菜单,文件夹
406+
407+
```xml
408+
//组合模式,就是在一个对象中包含其他对象,这些被包含的对象可能是终点对象(不再包含别的对象),
409+
//也有可能是非终点对象(其内部还包含其他对象,或叫组对象),
410+
//我们将对象称为节点,即一个根节点包含许多子节点,
411+
//这些子节点有的不再包含子节点,而有的仍然包含子节点,以此类推。
412+
```
413+
414+
```java
415+
实际逻辑:
416+
一个实体类:员工
417+
员工:老板--->CTO
418+
--->经理
419+
--->CMO
420+
--->C经理
421+
--->财务总监
422+
--->财务经理
423+
按照等级分
424+
425+
```
426+
427+
428+
429+
## 2.4 *装饰者模式(Decorator Pattern
430+
431+
- 允许向一个现有的对象添加新功能,同时不改变其结构.
432+
- 创建一个装饰类来包装原有的类,保持原类方法签名完整的前提下,提供额外的功能
433+
434+
```java
435+
//图形:画
436+
public interface Shape{
437+
void draw();
438+
}
439+
440+
//画圆
441+
public class Circle implements Shape{
442+
@Override
443+
public void draw(){
444+
System.out.println("画圆");
445+
}
446+
}
447+
448+
//画方形
449+
public class Rectangle implements Shape{
450+
@Override
451+
public void draw(){
452+
System.out.println("方形");
453+
}
454+
}
455+
```
456+
457+
```java
458+
//形状装饰 抽象
459+
public abstract class ShapeDecorator implements Shape {
460+
protected Shape decoratedShape;
461+
462+
public ShapeDecorator(Shape decoratedShape){
463+
this.decoratedShape = decoratedShape;
464+
}
465+
466+
public void draw(){
467+
decoratedShape.draw();
468+
}
469+
}
470+
//红色装饰 实现
471+
public class RedShapeDecorator extends ShapeDecorator {
472+
473+
public RedShapeDecorator(Shape decoratedShape) {
474+
super(decoratedShape);
475+
}
476+
477+
@Override
478+
public void draw() {
479+
decoratedShape.draw();
480+
setRedBorder(decoratedShape);
481+
}
482+
private void setRedBorder(Shape decoratedShape){
483+
System.out.println("Border Color: Red");
484+
}
485+
}
486+
//以上在打印了 形状以后 会后续打印出来 颜色 :相当于给物品上色
487+
```
488+
489+
490+
491+
## 2.5 *外观模式(Facade Pattern
492+
493+
- 隐藏系统的复杂性,向客户端提供了一个访问系统的接口.
494+
495+
- 意图:为子系统的一组接口提供了一个一致性的界面,外观模式定义一个高级接口,这个接口使得子系统更加容易使用
496+
497+
- 应用实例:1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。 2JAVA 的三层开发模式。
498+
499+
```java
500+
圆形
501+
:draw 画圆
502+
长方形
503+
:draw 画长方形
504+
正方形
505+
:draw 画正方形
506+
```
507+
508+
```java
509+
外观类
510+
public void drawCircle(){
511+
circle.draw();
512+
}
513+
public void drawRectangle(){
514+
rectangle.draw();
515+
}
516+
public void drawSquare(){
517+
square.draw();
518+
}
519+
520+
具体实现:
521+
//第一步:画圆
522+
外观类.drawCircle();
523+
//第二步:画长方形
524+
外观类.drawRectangle();
525+
//第三步:画正方形
526+
外观类.drawSquare();
527+
```
528+
529+
530+
400531
## 2.6 享元模式(Flyweight Pattern
401532
## 2.7 代理模式(Proxy Pattern
402533

0 commit comments

Comments
 (0)