1730 lines
43 KiB
Markdown
1730 lines
43 KiB
Markdown
|
<!-- GFM-TOC -->
|
|||
|
* [<EFBFBD><EFBFBD> 1 <20><> <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>](#<23><>-1-<2D><>-<2D><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>)
|
|||
|
* [<EFBFBD><EFBFBD> 2 <20><> <20>۲<EFBFBD><DBB2><EFBFBD>ģʽ](#<23><>-2-<2D><>-<2D>۲<EFBFBD><DBB2><EFBFBD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 3 <20><> װ<><D7B0>ģʽ](#<23><>-3-<2D><>-װ<><D7B0>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 4 <20><> <20><><EFBFBD><EFBFBD>ģʽ](#<23><>-4-<2D><>-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [4.1 <20><EFBFBD><F2B5A5B9><EFBFBD>](#41-<2D><EFBFBD><F2B5A5B9><EFBFBD>)
|
|||
|
* [4.2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#42-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [4.3 <20><><EFBFBD><EFBFBD>ģʽ](#43-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 5 <20><> <20><><EFBFBD><EFBFBD>ģʽ](#<23><>-5-<2D><>-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 6 <20><> <20><><EFBFBD><EFBFBD>ģʽ](#<23><>-6-<2D><>-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 7 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#<23><>-7-<2D><>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [7.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#71-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [7.2 <20><><EFBFBD><EFBFBD>ģʽ](#72-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 8 <20><> ģ<>巽<EFBFBD><E5B7BD>ģʽ](#<23><>-8-<2D><>-ģ<>巽<EFBFBD><E5B7BD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 9 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#<23><>-9-<2D><>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [9.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ](#91-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [9.2 Java <20><><EFBFBD>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>](#92-java-<2D><><EFBFBD>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>)
|
|||
|
* [9.3 <20><><EFBFBD><EFBFBD>ģʽ](#93-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 10 <20><> ״̬ģʽ](#<23><>-10-<2D><>-״̬ģʽ)
|
|||
|
* [<EFBFBD><EFBFBD> 11 <20><> <20><><EFBFBD><EFBFBD>ģʽ // TODO](#<23><>-11-<2D><>-<2D><><EFBFBD><EFBFBD>ģʽ--todo)
|
|||
|
* [<EFBFBD><EFBFBD> 12 <20><> <20><><EFBFBD><EFBFBD>ģʽ](#<23><>-12-<2D><>-<2D><><EFBFBD><EFBFBD>ģʽ)
|
|||
|
* [12.1 MVC](#121-mvc)
|
|||
|
* [12.1.1 <20><>ͳ MVC](#1211-<2D><>ͳ-mvc)
|
|||
|
* [12.1.2 Web <20>е<EFBFBD> MVC](#1212-web-<2D>е<EFBFBD>-mvc)
|
|||
|
* [<EFBFBD><EFBFBD> 13 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3>ദ](#<23><>-13-<2D><>-<2D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3>ദ)
|
|||
|
* [<EFBFBD><EFBFBD> 14 <20><> ʣ<>µ<EFBFBD>ģʽ // TODO](#<23><>-14-<2D><>-ʣ<>µ<EFBFBD>ģʽ--todo)
|
|||
|
<!-- GFM-TOC -->
|
|||
|
|
|||
|
# <20><> 1 <20><> <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
**1. <20><><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD>Ǵ<EFBFBD><EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѧϰ<EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>鸴<EFBFBD>á<EFBFBD>
|
|||
|
|
|||
|
ӵ<E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D>ͬ<EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD>з<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD>롣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/144d28a0-1dc5-4aba-8961-ced5bc88428a.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
|
|||
|
|
|||
|
**<2A><>װ<EFBFBD>仯**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>仯<EFBFBD><E4BBAF><EFBFBD><EFBFBD>Ѽ<EFBFBD>ӽкͷ<D0BA><CDB7>е<EFBFBD><D0B5><EFBFBD>Ϊ<EFBFBD><CEAA>ʽ<EFBFBD><CABD>
|
|||
|
|
|||
|
**<2A><><EFBFBD>Խӿڱ<D3BF><DAB1>̣<EFBFBD><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>࣬<EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD>Ǿ<EFBFBD><C7BE><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD>ࡣ<EFBFBD><E0A1A3><EFBFBD><EFBFBD><EFBFBD>еķ<D0B5><C4B7><EFBFBD>ʵ<EFBFBD>ֲ<EFBFBD><D6B2>ڸ<EFBFBD><DAB8>࣬<EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><DAB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࡣ<EFBFBD><E0A1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Զ<EFBFBD>̬<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һԭ<EFBFBD><EFBFBD><EFBFBD>кͷ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֶ<EFBFBD><EFBFBD>ֲ<EFBFBD>ͬ<EFBFBD>Ľкͷ<EFBFBD><EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥʵ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľкͷ<EFBFBD><EFBFBD>з<EFBFBD>ʽ<EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1c8ccf5c-7ecd-4b8a-b160-3f72a510ce26.png)
|
|||
|
|
|||
|
**<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD>ü̳<C3BC>** <20><><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD> has-a <20><>ϵ<EFBFBD><CFB5>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>̬<EFBFBD>ı<EFBFBD>ʵ<EFBFBD>֣<EFBFBD>ֻҪͨ<D2AA><CDA8><EFBFBD>ı丸<C4B1><E4B8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>༴<EFBFBD>ɡ<EFBFBD><C9A1><EFBFBD><EFBFBD>̳оͲ<D0BE><CDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD>̳<EFBFBD><CCB3><EFBFBD>ϵ<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>Ѿ<EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Duck <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> FlyBehavior <20><> QuackBehavior <20>࣬performQuack() <20><> performFly() <20><><EFBFBD><EFBFBD>ί<EFBFBD>и<EFBFBD><D0B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD>һ<EFBFBD><D2BB> Duck <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ը<EFBFBD><D4B8><EFBFBD><EFBFBD><EFBFBD>Ҫȥʵ<C8A5><CAB5><EFBFBD><EFBFBD> FlyBehavior <20><> QuackBehavior <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACB2><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD>Զ<EFBFBD>̬<EFBFBD>ؽ<EFBFBD><D8BD>иı䡣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29574e6f-295c-444e-83c7-b162e8a73a83.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e13833c8-e215-462e-855c-1d362bb8d4a0.jpg)
|
|||
|
|
|||
|
**6. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
**<2A><><EFBFBD><EFBFBD>ģʽ** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD>壬<EFBFBD>ֱ<EFBFBD><D6B1><EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD>㷨<EFBFBD>ı仯<C4B1><E4BBAF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>㷨<EFBFBD>Ŀͻ<C4BF><CDBB><EFBFBD>
|
|||
|
|
|||
|
**7. ʵ<>ִ<EFBFBD><D6B4><EFBFBD>**
|
|||
|
|
|||
|
```java
|
|||
|
public abstract class Duck {
|
|||
|
FlyBehavior flyBehavior;
|
|||
|
QuackBehavior quackBehavior;
|
|||
|
|
|||
|
public Duck(){
|
|||
|
}
|
|||
|
|
|||
|
public void performFly(){
|
|||
|
flyBehavior.fly();
|
|||
|
}
|
|||
|
|
|||
|
public void setFlyBehavior(FlyBehavior fb){
|
|||
|
flyBehavior = fb;
|
|||
|
}
|
|||
|
|
|||
|
public void performQuack(){
|
|||
|
quackBehavior.quack();
|
|||
|
}
|
|||
|
|
|||
|
public void setQuackBehavior(QuackBehavior qb){
|
|||
|
quackBehavior = qb;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class MallarDuck extends Duck{
|
|||
|
public MallarDuck(){
|
|||
|
flyBehavior = new FlyWithWings();
|
|||
|
quackBehavior = new Quack();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface FlyBehavior {
|
|||
|
void fly();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class FlyNoWay implements FlyBehavior{
|
|||
|
@Override
|
|||
|
public void fly() {
|
|||
|
System.out.println("FlyBehavior.FlyNoWay");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class FlyWithWings implements FlyBehavior{
|
|||
|
@Override
|
|||
|
public void fly() {
|
|||
|
System.out.println("FlyBehavior.FlyWithWings");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface QuackBehavior {
|
|||
|
void quack();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Quack implements QuackBehavior{
|
|||
|
@Override
|
|||
|
public void quack() {
|
|||
|
System.out.println("QuackBehavior.Quack");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class MuteQuack implements QuackBehavior{
|
|||
|
@Override
|
|||
|
public void quack() {
|
|||
|
System.out.println("QuackBehavior.MuteQuack");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Squeak implements QuackBehavior{
|
|||
|
@Override
|
|||
|
public void quack() {
|
|||
|
System.out.println("QuackBehavior.Squeak");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class MiniDuckSimulator {
|
|||
|
public static void main(String[] args) {
|
|||
|
Duck mallarDuck = new MallarDuck();
|
|||
|
mallarDuck.performQuack();
|
|||
|
mallarDuck.performFly();
|
|||
|
mallarDuck.setFlyBehavior(new FlyNoWay());
|
|||
|
mallarDuck.performFly();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
ִ<EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
```html
|
|||
|
QuackBehavior.Quack
|
|||
|
FlyBehavior.FlyWithWings
|
|||
|
FlyBehavior.FlyNoWay
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 2 <20><> <20>۲<EFBFBD><DBB2><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>״̬ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD>֪ͨ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>¡<EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣨Subject<EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><EFBFBD>۲<EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>Observer<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>۲<EFBFBD><EFBFBD>ߡ<EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/26cb5e7e-6fa3-44ad-854e-fe24d1a5278c.jpg)
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><EFBFBD>۲<EFBFBD><EFBFBD>ߣ<EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><EFBFBD><EFBFBD>ߵĹ<EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>ά<EFBFBD><EFBFBD>һ<EFBFBD>Ź۲<EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ġ<EFBFBD>
|
|||
|
|
|||
|
<EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD>Ϊע<EFBFBD>ᡢ<EFBFBD>Ƴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ܡ<EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5c558190-fccd-4b5e-98ed-1896653fc97f.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/760a5d63-d96d-4dd9-bf9a-c3d126b2f401.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
|
|||
|
|
|||
|
**Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>Ŭ<EFBFBD><C5AC>** <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD>Խ<EFBFBD><D4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD>̫<EFBFBD><CCAB><EFBFBD><EFBFBD><EFBFBD>˴˵<CBB4>ϸ<EFBFBD>ڡ<EFBFBD><DAA1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD>以<EFBFBD><E4BBA5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̶Ⱥܵͣ<DCB5><CDA3><EFBFBD><EFBFBD><EFBFBD>ϵͳ<CFB5><CDB3><EFBFBD>е<EFBFBD><D0B5>ԣ<EFBFBD><D4A3>ܹ<EFBFBD>Ӧ<EFBFBD>Ա仯<D4B1><E4BBAF>
|
|||
|
|
|||
|
**6. ʵ<>ִ<EFBFBD><D6B4><EFBFBD>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Subject {
|
|||
|
public void resisterObserver(Observer o);
|
|||
|
public void removeObserver(Observer o);
|
|||
|
public void notifyObserver();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
import java.util.ArrayList;
|
|||
|
import java.util.List;
|
|||
|
|
|||
|
public class WeatherData implements Subject {
|
|||
|
private List<Observer> observers;
|
|||
|
private float temperature;
|
|||
|
private float humidity;
|
|||
|
private float pressure;
|
|||
|
|
|||
|
public WeatherData() {
|
|||
|
observers = new ArrayList<>();
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void resisterObserver(Observer o) {
|
|||
|
observers.add(o);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void removeObserver(Observer o) {
|
|||
|
int i = observers.indexOf(o);
|
|||
|
if (i >= 0) {
|
|||
|
observers.remove(i);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void notifyObserver() {
|
|||
|
for (Observer o : observers) {
|
|||
|
o.update(temperature, humidity, pressure);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void setMeasurements(float temperature, float humidity, float pressure) {
|
|||
|
this.temperature = temperature;
|
|||
|
this.humidity = humidity;
|
|||
|
this.pressure = pressure;
|
|||
|
notifyObserver();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface Observer {
|
|||
|
public void update(float temp, float humidity, float pressure);
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class CurrentConditionsDisplay implements Observer {
|
|||
|
private Subject weatherData;
|
|||
|
|
|||
|
public CurrentConditionsDisplay(Subject weatherData) {
|
|||
|
this.weatherData = weatherData;
|
|||
|
weatherData.resisterObserver(this);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void update(float temp, float humidity, float pressure) {
|
|||
|
System.out.println("CurrentConditionsDisplay.update:" + temp + " " + humidity + " " + pressure);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class StatisticsDisplay implements Observer {
|
|||
|
private Subject weatherData;
|
|||
|
|
|||
|
public StatisticsDisplay(Subject weatherData) {
|
|||
|
this.weatherData = weatherData;
|
|||
|
weatherData.resisterObserver(this);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void update(float temp, float humidity, float pressure) {
|
|||
|
System.out.println("StatisticsDisplay.update:" + temp + " " + humidity + " " + pressure);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class WeatherStation {
|
|||
|
public static void main(String[] args) {
|
|||
|
WeatherData weatherData = new WeatherData();
|
|||
|
CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
|
|||
|
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
|
|||
|
|
|||
|
weatherData.setMeasurements(0, 0, 0);
|
|||
|
weatherData.setMeasurements(1, 1, 1);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
ִ<EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
```html
|
|||
|
CurrentConditionsDisplay.update:0.0 0.0 0.0
|
|||
|
StatisticsDisplay.update:0.0 0.0 0.0
|
|||
|
CurrentConditionsDisplay.update:1.0 1.0 1.0
|
|||
|
StatisticsDisplay.update:1.0 1.0 1.0
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 3 <20><> װ<><D7B0>ģʽ
|
|||
|
|
|||
|
**1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>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FBD>Mocha <20><><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD> Whip <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6>̳<EFBFBD><CCB3><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>࣬<EFBFBD><E0A3AC><EFBFBD><EFBFBD> cost() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cost() <20><><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cost() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA> DarkRoast <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Mocha<68><61><EFBFBD><EFBFBD>ôֻ<C3B4><D6BB>Ҫ<EFBFBD><D2AA> Mocha <20><><EFBFBD><EFBFBD> DarkRoast<73><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ Whip <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Whip <20><><EFBFBD><EFBFBD> Mocha<68><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cost() <20><><EFBFBD><EFBFBD><EFBFBD>ܰ<EFBFBD><DCB0><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD>ļ۸<DBB8><F1B6BCB0><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/41a4cb30-f393-4b3b-abe4-9941ccf8fa1f.jpg)
|
|||
|
|
|||
|
**3. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
װ<EFBFBD><EFBFBD><EFBFBD>ߺ;<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֲ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߾<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>νװ<EFBFBD>Σ<EFBFBD><E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ϊֻ<EFBFBD>о<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>ʵ<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫί<EFBFBD>и<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3dc454fb-efd4-4eb8-afde-785b2182caeb.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9c997ac5-c8a7-44fe-bf45-2c10eb773e53.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
|
|||
|
|
|||
|
**<2A><>Ӧ<EFBFBD>ö<EFBFBD><C3B6><EFBFBD>չ<EFBFBD><D5B9><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD>Ĺرա<D8B1>**Ҳ<><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¹<EFBFBD><C2B9><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD>Ĵ<DEB8><C4B4>롣<EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µIJ<C2B5><C4B2>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȥ<D2AA><EFBFBD><DEB8><EFBFBD><EFBFBD>ϵĴ<CFB5><C4B4>롣<EFBFBD>۲<EFBFBD><DBB2><EFBFBD>ģʽҲ<CABD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><F2A1A3B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B6BC>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>Ѹ<EFBFBD>ԭ<EFBFBD><D4AD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܸı<DCB8><C4B1>ĵط<C4B5><D8B7><EFBFBD>
|
|||
|
|
|||
|
**6. Java I/O <20>е<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD>ģʽ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2a40042a-03c8-4556-ad1f-72d89f8c555c.jpg)
|
|||
|
|
|||
|
**7. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Beverage {
|
|||
|
public double cost();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class HouseBlend implements Beverage{
|
|||
|
@Override
|
|||
|
public double cost() {
|
|||
|
return 1;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class DarkRoast implements Beverage{
|
|||
|
@Override
|
|||
|
public double cost() {
|
|||
|
return 1;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public abstract class CondimentDecorator implements Beverage{
|
|||
|
protected Beverage beverage;
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Mocha extends CondimentDecorator {
|
|||
|
|
|||
|
public Mocha(Beverage beverage) {
|
|||
|
this.beverage = beverage;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public double cost() {
|
|||
|
return 1 + beverage.cost();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Milk extends CondimentDecorator {
|
|||
|
|
|||
|
public Milk(Beverage beverage) {
|
|||
|
this.beverage = beverage;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public double cost() {
|
|||
|
return 1 + beverage.cost();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class StartbuzzCoffee {
|
|||
|
public static void main(String[] args) {
|
|||
|
Beverage beverage = new HouseBlend();
|
|||
|
beverage = new Mocha(beverage);
|
|||
|
beverage = new Milk(beverage);
|
|||
|
System.out.println(beverage.cost());
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```html
|
|||
|
3.0
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 4 <20><> <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
## 4.1 <20><EFBFBD><F2B5A5B9><EFBFBD>
|
|||
|
|
|||
|
**1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD>в<EFBFBD>ͬ<EFBFBD><EFBFBD> Pizza<7A><61><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD>ͬ<E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EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ü<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>еĿͻ<EFBFBD><EFBFBD>Ҫ֪<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϸ<EFBFBD>ڣ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı䣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD>еĿͻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı䡣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c470eb9b-fb05-45c5-8bb7-1057dc3c16de.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dc3e704c-7c57-42b8-93ea-ddd068665964.jpg)
|
|||
|
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Pizza {
|
|||
|
public void make();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class CheesePizza implements Pizza{
|
|||
|
@Override
|
|||
|
public void make() {
|
|||
|
System.out.println("CheesePizza");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class GreekPizza implements Pizza{
|
|||
|
@Override
|
|||
|
public void make() {
|
|||
|
System.out.println("GreekPizza");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class SimplePizzaFactory {
|
|||
|
public Pizza createPizza(String type) {
|
|||
|
if (type.equals("cheese")) {
|
|||
|
return new CheesePizza();
|
|||
|
} else if (type.equals("greek")) {
|
|||
|
return new GreekPizza();
|
|||
|
} else {
|
|||
|
throw new UnsupportedOperationException();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class PizzaStore {
|
|||
|
public static void main(String[] args) {
|
|||
|
SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory();
|
|||
|
Pizza pizza = simplePizzaFactory.createPizza("cheese");
|
|||
|
pizza.make();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
CheesePizza
|
|||
|
```
|
|||
|
|
|||
|
## 4.2 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Pizza <20><><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>ķ<EFBFBD>ζ<EFBFBD><CEB6><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>磬һ<E7A3AC><D2BB><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>ŦԼ<C5A6><D4BC> cheese <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Pizza <20><><EFBFBD><EFBFBD>֥<EFBFBD>Ӹ<EFBFBD><D3B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Pizza <20>Dz<EFBFBD>ͬ<EFBFBD>ġ<EFBFBD>
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴٵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࡣ
|
|||
|
|
|||
|
**3. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
<EFBFBD>ڼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E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ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD>Ʒ<EFBFBD>࣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/903093ec-acc8-4f9b-bf2c-b990b9a5390c.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/664f8901-5dc7-4644-a072-dad88cc5133a.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Pizza {
|
|||
|
public void make();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface PizzaStore {
|
|||
|
public Pizza orderPizza(String item);
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NYStyleCheesePizza implements Pizza{
|
|||
|
@Override
|
|||
|
public void make() {
|
|||
|
System.out.println("NYStyleCheesePizza is making..");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NYStyleVeggiePizza implements Pizza {
|
|||
|
@Override
|
|||
|
public void make() {
|
|||
|
System.out.println("NYStyleVeggiePizza is making..");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ChicagoStyleCheesePizza implements Pizza{
|
|||
|
@Override
|
|||
|
public void make() {
|
|||
|
System.out.println("ChicagoStyleCheesePizza is making..");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ChicagoStyleVeggiePizza implements Pizza{
|
|||
|
@Override
|
|||
|
public void make() {
|
|||
|
System.out.println("ChicagoStyleVeggiePizza is making..");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NYPizzaStore implements PizzaStore {
|
|||
|
@Override
|
|||
|
public Pizza orderPizza(String item) {
|
|||
|
Pizza pizza = null;
|
|||
|
if (item.equals("cheese")) {
|
|||
|
pizza = new NYStyleCheesePizza();
|
|||
|
} else if (item.equals("veggie")) {
|
|||
|
pizza = new NYStyleVeggiePizza();
|
|||
|
} else {
|
|||
|
throw new UnsupportedOperationException();
|
|||
|
}
|
|||
|
pizza.make();
|
|||
|
return pizza;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ChicagoPizzaStore implements PizzaStore {
|
|||
|
@Override
|
|||
|
public Pizza orderPizza(String item) {
|
|||
|
Pizza pizza = null;
|
|||
|
if (item.equals("cheese")) {
|
|||
|
pizza = new ChicagoStyleCheesePizza();
|
|||
|
} else if (item.equals("veggie")) {
|
|||
|
pizza = new ChicagoStyleVeggiePizza();
|
|||
|
} else {
|
|||
|
throw new UnsupportedOperationException();
|
|||
|
}
|
|||
|
pizza.make();
|
|||
|
return pizza;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class PizzaTestDrive {
|
|||
|
public static void main(String[] args) {
|
|||
|
PizzaStore nyStore = new NYPizzaStore();
|
|||
|
nyStore.orderPizza("cheese");
|
|||
|
PizzaStore chicagoStore = new ChicagoPizzaStore();
|
|||
|
chicagoStore.orderPizza("cheese");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```html
|
|||
|
NYStyleCheesePizza is making..
|
|||
|
ChicagoStyleCheesePizza is making..
|
|||
|
```
|
|||
|
|
|||
|
## 4.3 <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|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C> PizzaStore <20><><EFBFBD>ڸ߲<DAB8><DFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵײ<DAB5><D7B2><EFBFBD><EFBFBD><EFBFBD> Pizza <20>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD>ࡣ<EFBFBD><E0A1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Pizza <20>ij<EFBFBD><C4B3><EFBFBD><EFBFBD>࣬<EFBFBD><E0A3AC>ô<EFBFBD>Ϳ<EFBFBD><CDBF>Բ<EFBFBD><D4B2>ù<EFBFBD><C3B9><EFBFBD> Pizza <20>ľ<EFBFBD><C4BE><EFBFBD>ʵ<EFBFBD><CAB5>ϸ<EFBFBD>ڡ<EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ddf72ca9-c0be-49d7-ab81-57a99a974c8e.jpg)
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD>ṩһ<EFBFBD><EFBFBD><EFBFBD>ӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ػ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>壬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>ȷָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࡣ
|
|||
|
|
|||
|
**3. ģʽ<C4A3><CABD>ͼ**
|
|||
|
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><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><EFBFBD>AbstractFactory <20>е<EFBFBD> CreateProductA <20><> CreateProductB <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϲ<EFBFBD><CFB9><EFBFBD>ģʽ<C4A3>Ķ<EFBFBD><C4B6>塣<EFBFBD><E5A1A3><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Client <20><><EFBFBD>֣<EFBFBD>Client Ҫͨ<D2AA><CDA8> AbstractFactory ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кܴ<D0BA><DCB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD>Client <20><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Э<EFBFBD><D0AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӹ߲<D3B8><DFB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD> Cilent <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> AbstractFactory <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽʹ<CABD><CAB9><EFBFBD>˼̳С<CCB3>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d301774f-e0d2-41f3-95f4-bfe39859b52e.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8785dabd-1285-4bd0-b3aa-b05cc060a24a.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Dough {
|
|||
|
public String doughType();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ThickCrustDough implements Dough{
|
|||
|
|
|||
|
@Override
|
|||
|
public String doughType() {
|
|||
|
return "ThickCrustDough";
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ThinCrustDough implements Dough {
|
|||
|
@Override
|
|||
|
public String doughType() {
|
|||
|
return "ThinCrustDough";
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface Sauce {
|
|||
|
public String sauceType();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class MarinaraSauce implements Sauce {
|
|||
|
@Override
|
|||
|
public String sauceType() {
|
|||
|
return "MarinaraSauce";
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class PlumTomatoSauce implements Sauce {
|
|||
|
@Override
|
|||
|
public String sauceType() {
|
|||
|
return "PlumTomatoSauce";
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface PizzaIngredientFactory {
|
|||
|
public Dough createDough();
|
|||
|
public Sauce createSauce();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NYPizzaIngredientFactory implements PizzaIngredientFactory{
|
|||
|
@Override
|
|||
|
public Dough createDough() {
|
|||
|
return new ThickCrustDough();
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public Sauce createSauce() {
|
|||
|
return new MarinaraSauce();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory{
|
|||
|
@Override
|
|||
|
public Dough createDough() {
|
|||
|
return new ThinCrustDough();
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public Sauce createSauce() {
|
|||
|
return new PlumTomatoSauce();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NYPizzaStore {
|
|||
|
private PizzaIngredientFactory ingredientFactory;
|
|||
|
|
|||
|
public NYPizzaStore() {
|
|||
|
ingredientFactory = new NYPizzaIngredientFactory();
|
|||
|
}
|
|||
|
|
|||
|
public void makePizza() {
|
|||
|
Dough dough = ingredientFactory.createDough();
|
|||
|
Sauce sauce = ingredientFactory.createSauce();
|
|||
|
System.out.println(dough.doughType());
|
|||
|
System.out.println(sauce.sauceType());
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NYPizzaStoreTestDrive {
|
|||
|
public static void main(String[] args) {
|
|||
|
NYPizzaStore nyPizzaStore = new NYPizzaStore();
|
|||
|
nyPizzaStore.makePizza();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```html
|
|||
|
ThickCrustDough
|
|||
|
MarinaraSauce
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 5 <20><> <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
ȷ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṩ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ȫ<EFBFBD>ַ<EFBFBD><EFBFBD>ʵ㡣
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD> Java ʵ<><CAB5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>˽<EFBFBD>й<EFBFBD><D0B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>˽<EFBFBD>о<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>о<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ú<EFBFBD><C3BA><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ψһ<CEA8><D2BB>˽<EFBFBD>о<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/59aff6c1-8bc5-48e4-9e9c-082baeb2f274.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>˽<EFBFBD>о<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳٻ<EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺô<EFBFBD><EFBFBD>ǣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>࣬<EFBFBD><EFBFBD>ô<EFBFBD>Ͳ<EFBFBD><EFBFBD>ᴴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˽<EFBFBD>о<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD>̻߳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><EFBFBD>ܹ<EFBFBD>ͬʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> if(uniqueInstance == null) <20>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>飬<EFBFBD><E9A3AC>ô<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD> uniqueInstance ˽<>о<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
public class Singleton {
|
|||
|
|
|||
|
private static Singleton uniqueInstance;
|
|||
|
|
|||
|
private Singleton() {
|
|||
|
}
|
|||
|
|
|||
|
public static Singleton getUniqueInstance() {
|
|||
|
if (uniqueInstance == null) {
|
|||
|
uniqueInstance = new Singleton();
|
|||
|
}
|
|||
|
return uniqueInstance;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
**4. <20>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ**
|
|||
|
|
|||
|
ֻ<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD> getUniqueInstance() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø÷<C3B8><C3B7><EFBFBD>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB><EFBFBD>̷߳<DFB3><CCB7>ʣ<EFBFBD><CAA3>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˶<EFBFBD> uniqueInstance <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⡣<EFBFBD><E2A1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB><EFBFBD>߳̽<DFB3><CCBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD>ϻ<EFBFBD><CFBB><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>˷ѡ<CBB7>
|
|||
|
|
|||
|
```java
|
|||
|
public static synchronized Singleton getUniqueInstance() {
|
|||
|
if (uniqueInstance == null) {
|
|||
|
uniqueInstance = new Singleton();
|
|||
|
}
|
|||
|
return uniqueInstance;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
**5. <20>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
private static Singleton uniqueInstance = new Singleton();
|
|||
|
```
|
|||
|
|
|||
|
**6. <20>̲߳<DFB3><CCB2><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><C4BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӷ<EFBFBD> getUniqueInstance() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м<EFBFBD><D0BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>Ҫ<EFBFBD><D2AA> uniqueInstance = new Singleton(); <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD> uniqueInstance <20>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```java
|
|||
|
public class Singleton {
|
|||
|
|
|||
|
private volatile static Singleton uniqueInstance;
|
|||
|
|
|||
|
private Singleton() {
|
|||
|
}
|
|||
|
|
|||
|
public static synchronized Singleton getUniqueInstance() {
|
|||
|
if (uniqueInstance == null) {
|
|||
|
synchronized (Singleton.class) {
|
|||
|
if (uniqueInstance == null) {
|
|||
|
uniqueInstance = new Singleton();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return uniqueInstance;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 6 <20><> <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ң<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кܶఴť<EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ť<EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>зdz<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļҵ磬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӽҵ硣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/7b8f0d8e-a4fa-4c9d-b9a0-3e6a11cb3e33.jpg)
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c3ca36b2-8459-4cf1-98b0-cc95a0e94f20.jpg)
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>װ<EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>ʹ<EFBFBD>ò<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
**3. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1e09d75f-6268-4425-acf8-8ecd1b4a0ef3.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
Invoker <20><>ң<EFBFBD><D2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3AC><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> execute() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Receiver <20>ǵ<EFBFBD><C7B5>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4><EFBFBD>ߡ<EFBFBD>ConcreteCommand <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Receiver <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>ί<EFBFBD>и<EFBFBD> Receiver <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD> LightOnCommand <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> excute <20><><EFBFBD><EFBFBD>ί<EFBFBD>и<EFBFBD> Light <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Light <20><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD> on() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɲ<EFBFBD><C9B2><EFBFBD><EFBFBD><EFBFBD>Invoker <20><><EFBFBD><EFBFBD> Client <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Invoker <20><><EFBFBD><EFBFBD><EFBFBD>ɵģ<C9B5><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Client <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ef94f62-98ce-464d-a646-842d9c72c8b8.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Command {
|
|||
|
public void execute();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Light {
|
|||
|
|
|||
|
public void on() {
|
|||
|
System.out.println("Light is on!");
|
|||
|
}
|
|||
|
|
|||
|
public void off() {
|
|||
|
System.out.println("Light is off!");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class LightOnCommand implements Command{
|
|||
|
Light light;
|
|||
|
|
|||
|
public LightOnCommand(Light light) {
|
|||
|
this.light = light;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void execute() {
|
|||
|
light.on();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
/**
|
|||
|
* ң<><D2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
public class SimpleRemoteControl {
|
|||
|
Command slot;
|
|||
|
|
|||
|
public SimpleRemoteControl() {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public void setCommand(Command command) {
|
|||
|
this.slot = command;
|
|||
|
}
|
|||
|
|
|||
|
public void buttonWasPressed() {
|
|||
|
slot.execute();
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class RemoteLoader {
|
|||
|
public static void main(String[] args) {
|
|||
|
SimpleRemoteControl remote = new SimpleRemoteControl();
|
|||
|
Light light = new Light();
|
|||
|
LightOnCommand lightOnCommand = new LightOnCommand(light);
|
|||
|
remote.setCommand(lightOnCommand);
|
|||
|
remote.buttonWasPressed();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```html
|
|||
|
Light is on!
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 7 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
## 7.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľӿڣ<EFBFBD>ת<EFBFBD><EFBFBD>Ϊ<EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ӿڡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ժ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>䡣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8e8ba824-7a9e-4934-a212-e6a41dcc1602.jpg)
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ࣨAdapter<EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Adaptee<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>ί<EFBFBD>и<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ̳У<EFBFBD>Adapter <20><><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD> Target <20><> Adaptee <20><><EFBFBD>ͣ<EFBFBD><CDA3>Ȱ<EFBFBD><C8B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Adaptee <20><><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> Adapter <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٰ<EFBFBD><D9B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Target <20><><EFBFBD>͵ģ<CDB5><C4A3><EFBFBD><EFBFBD><EFBFBD> Client <20>Ϳ<EFBFBD><CDBF><EFBFBD><D4B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Target <20>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/253bd869-ea48-4092-9aed-6906ccb2f3b0.jpg)
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/a797959a-0ed5-475b-8d97-df157c672019.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>Ѽ<EFBFBD>ӣ<EFBFBD>Duck<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Turkey<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Duck <20><> quack() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Turkey ֻ<><D6BB> gobble() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA> Turkey Ҳ<><D2B2> Duck <20><> quack() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1a511c76-bb6b-40ab-b8aa-39eeb619d673.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface Duck {
|
|||
|
public void quack();
|
|||
|
public void fly();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface Turkey {
|
|||
|
public void gobble();
|
|||
|
public void fly();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class WildTurkey implements Turkey{
|
|||
|
@Override
|
|||
|
public void gobble() {
|
|||
|
System.out.println("gobble!");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void fly() {
|
|||
|
System.out.println("fly!");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class TurkeyAdapter implements Duck{
|
|||
|
Turkey turkey;
|
|||
|
|
|||
|
public TurkeyAdapter(Turkey turkey) {
|
|||
|
this.turkey = turkey;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void quack() {
|
|||
|
turkey.gobble();
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void fly() {
|
|||
|
turkey.fly();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class DuckTestDrive {
|
|||
|
public static void main(String[] args) {
|
|||
|
Turkey turkey = new WildTurkey();
|
|||
|
Duck duck = new TurkeyAdapter(turkey);
|
|||
|
duck.quack();
|
|||
|
duck.fly();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
```html
|
|||
|
gobble!
|
|||
|
fly!
|
|||
|
```
|
|||
|
|
|||
|
## 7.2 <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD>ṩ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ͳһ<EFBFBD>Ľӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<EFBFBD>е<EFBFBD>һȺ<EFBFBD>ӿڡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>۶<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>߲<EFBFBD><EFBFBD>ӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵͳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>á<EFBFBD>
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/78f2314e-2643-41df-8f3d-b7e28294094b.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>ͥӰԺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>йۿ<EFBFBD><EFBFBD><EFBFBD>Ӱʱ<EFBFBD><EFBFBD>Ҫ<EFBFBD>Ժܶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ü<EFBFBD>ͥӰԺ<EFBFBD><EFBFBD>ֻ<EFBFBD>ṩһ<EFBFBD><EFBFBD><EFBFBD>Ľӿڣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ṩһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӱ<EFBFBD>Ľӿڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>þ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/106f5585-b2e7-4718-be5d-3b322d1ef42a.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25387681-89f8-4365-a2fa-83b86449ee84.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
|
|||
|
|
|||
|
**<2A><><EFBFBD><EFBFBD>֪ʶԭ<CAB6><D4AD>**<2A><>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD≯<EFBFBD><CCB8><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2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|
ģ<EFBFBD>巽<EFBFBD><EFBFBD> templateMethod() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD>ĹǼܣ<C7BC>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD> primitiveOperation1() <20><> primitiveOperation2() <20><><EFBFBD><EFBFBD>ִ<EFBFBD>е<EFBFBD>˳<EFBFBD><EFBFBD> primitiveOperation1() <20><> primitiveOperation2() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȥ<EFBFBD><C8A5><EFBFBD><EFBFBD>ʵ<EFBFBD>֡<EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ed62f400-192c-4185-899b-187958201f0c.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD>忧<EFBFBD>Ⱥͳ<EFBFBD><EFBFBD>趼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijЩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е㲻һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d8f873fc-00bc-41ee-a87c-c1b4c0172844.png)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/aa20c123-b6b5-432a-83d3-45dc39172192.jpg)
|
|||
|
|
|||
|
**5. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
|
|||
|
|
|||
|
**<2A><><EFBFBD><EFB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
|
|||
|
|
|||
|
**6. <20><><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>hock<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ijЩ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD>ͬʵ<EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><EFBFBD>п<EFBFBD><EFBFBD>ޣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ʲô<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD>ģ<EFBFBD>巽<EFBFBD><EFBFBD> templteMethod() <20>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><CDB8><EFBFBD>Ĭ<EFBFBD><C4AC>ʵ<EFBFBD>ֲ<EFBFBD><D6B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>ʵ<EFBFBD>֡<EFBFBD>
|
|||
|
|
|||
|
**7. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public abstract class CaffeineBeverage {
|
|||
|
|
|||
|
final void prepareRecipe(){
|
|||
|
boilWater();
|
|||
|
brew();
|
|||
|
pourInCup();
|
|||
|
addCondiments();
|
|||
|
}
|
|||
|
|
|||
|
abstract void brew();
|
|||
|
|
|||
|
abstract void addCondiments();
|
|||
|
|
|||
|
void boilWater(){
|
|||
|
System.out.println("boilWater");
|
|||
|
}
|
|||
|
|
|||
|
void pourInCup(){
|
|||
|
System.out.println("pourInCup");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Coffee extends CaffeineBeverage{
|
|||
|
@Override
|
|||
|
void brew() {
|
|||
|
System.out.println("Coffee.brew");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
void addCondiments() {
|
|||
|
System.out.println("Coffee.addCondiments");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Tea extends CaffeineBeverage{
|
|||
|
@Override
|
|||
|
void brew() {
|
|||
|
System.out.println("Tea.brew");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
void addCondiments() {
|
|||
|
System.out.println("Tea.addCondiments");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class CaffeineBeverageTestDrive {
|
|||
|
public static void main(String[] args) {
|
|||
|
CaffeineBeverage caffeineBeverage = new Coffee();
|
|||
|
caffeineBeverage.prepareRecipe();
|
|||
|
System.out.println("-----------");
|
|||
|
caffeineBeverage = new Tea();
|
|||
|
caffeineBeverage.prepareRecipe();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```html
|
|||
|
boilWater
|
|||
|
Coffee.brew
|
|||
|
pourInCup
|
|||
|
Coffee.addCondiments
|
|||
|
-----------
|
|||
|
boilWater
|
|||
|
Tea.brew
|
|||
|
pourInCup
|
|||
|
Tea.addCondiments
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 9 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
## 9.1 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. ģʽ<C4A3><CAB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BD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/439deca7-fed0-4c89-87e5-7088d10f1fdb.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public class Aggregate {
|
|||
|
|
|||
|
private int[] items;
|
|||
|
|
|||
|
public Aggregate() {
|
|||
|
items = new int[10];
|
|||
|
for (int i = 0; i < items.length; i++) {
|
|||
|
items[i] = i;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public Iterator createIterator() {
|
|||
|
return new ConcreteIterator(items);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public interface Iterator {
|
|||
|
boolean hasNext();
|
|||
|
int next();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class ConcreteIterator implements Iterator {
|
|||
|
|
|||
|
private int[] items;
|
|||
|
private int position = 0;
|
|||
|
|
|||
|
public ConcreteIterator(int[] items) {
|
|||
|
this.items = items;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public boolean hasNext() {
|
|||
|
return position < items.length;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public int next() {
|
|||
|
return items[position++];
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Client {
|
|||
|
public static void main(String[] args) {
|
|||
|
Aggregate aggregate = new Aggregate();
|
|||
|
Iterator iterator = aggregate.createIterator();
|
|||
|
while(iterator.hasNext()){
|
|||
|
System.out.println(iterator.next());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
```html
|
|||
|
0
|
|||
|
1
|
|||
|
2
|
|||
|
3
|
|||
|
4
|
|||
|
5
|
|||
|
6
|
|||
|
7
|
|||
|
8
|
|||
|
9
|
|||
|
```
|
|||
|
|
|||
|
## 9.2 Java <20><><EFBFBD>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
**1. ʵ<>ֽӿ<D6BD>**
|
|||
|
|
|||
|
Java <20><><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD> Iterator <20>ӿڣ<D3BF><DAA3><EFBFBD>ʹ<EFBFBD><CAB9> Java ʵ<><CAB5>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD>þۺ϶<DBBA><CFB6><EFBFBD>ʵ<EFBFBD><CAB5> Iterable <20>ӿڣ<D3BF><DAA3>ýӿ<C3BD><D3BF><EFBFBD>һ<EFBFBD><D2BB> iterator() <20><><EFBFBD><EFBFBD><EFBFBD>᷵<EFBFBD><E1B7B5>һ<EFBFBD><D2BB> Iterator <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> Java <20><><EFBFBD>õĵ<C3B5><C4B5><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD>֣<EFBFBD><D6A3>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> foreach ѭ<><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۺ϶<DBBA><CFB6><EFBFBD><EFBFBD>е<EFBFBD>ÿ<EFBFBD><C3BF>Ԫ<EFBFBD>ء<EFBFBD>
|
|||
|
|
|||
|
**2. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
import java.util.Iterator;
|
|||
|
|
|||
|
public class Aggregate implements Iterable<Integer>{
|
|||
|
|
|||
|
private int[] items;
|
|||
|
|
|||
|
public Aggregate() {
|
|||
|
items = new int[10];
|
|||
|
for (int i = 0; i < items.length; i++) {
|
|||
|
items[i] = i;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public Iterator<Integer> iterator() {
|
|||
|
return new ConcreteIterator(items);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
import java.util.Iterator;
|
|||
|
|
|||
|
public class ConcreteIterator implements Iterator<Integer> {
|
|||
|
|
|||
|
private int[] items;
|
|||
|
private int position = 0;
|
|||
|
|
|||
|
public ConcreteIterator(int[] items) {
|
|||
|
this.items = items;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public boolean hasNext() {
|
|||
|
return position < items.length;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public Integer next() {
|
|||
|
return items[position++];
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Client {
|
|||
|
public static void main(String[] args) {
|
|||
|
Aggregate aggregate = new Aggregate();
|
|||
|
for (int item : aggregate) {
|
|||
|
System.out.println(item);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
## 9.3 <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
**1. <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD>**
|
|||
|
|
|||
|
һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>νṹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> / <20><><EFBFBD>֡<EFBFBD><D6A1><EFBFBD><EFBFBD>νṹ<CEBD><E1B9B9>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿͻ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>µķ<EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϡ<EFBFBD>
|
|||
|
|
|||
|
**3. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>Composite<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Component<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>νṹ<EFBFBD><EFBFBD><EFBFBD>м䣬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f99c019e-7e91-4c2e-b94d-b031c402dcb5.jpg)
|
|||
|
|
|||
|
**4. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public abstract class Component {
|
|||
|
protected String name;
|
|||
|
|
|||
|
public Component(String name) {
|
|||
|
this.name = name;
|
|||
|
}
|
|||
|
|
|||
|
abstract public void addChild(Component component);
|
|||
|
|
|||
|
public void print() {
|
|||
|
print(0);
|
|||
|
}
|
|||
|
|
|||
|
abstract protected void print(int level);
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Leaf extends Component {
|
|||
|
public Leaf(String name) {
|
|||
|
super(name);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void addChild(Component component) {
|
|||
|
throw new UnsupportedOperationException(); // <20><><EFBFBD><EFBFBD><EFBFBD><CDB8><EFBFBD>Ի<EFBFBD>ȡ<EFBFBD><C8A1>һְ<D2BB><D6B0>ԭ<EFBFBD><D4AD> , <20><><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD>ӽڵ㻹<DAB5><E3BBB9><EFBFBD><EFBFBD><EFBFBD>Ͻڵ<CFBD>
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
protected void print(int level) {
|
|||
|
for (int i = 0; i < level; i++) {
|
|||
|
System.out.print("--");
|
|||
|
}
|
|||
|
System.out.println("left:" + name);
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
import java.util.ArrayList;
|
|||
|
import java.util.List;
|
|||
|
|
|||
|
public class Composite extends Component {
|
|||
|
|
|||
|
private List<Component> childs;
|
|||
|
|
|||
|
public Composite(String name) {
|
|||
|
super(name);
|
|||
|
childs = new ArrayList<>();
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void addChild(Component component) {
|
|||
|
childs.add(component);
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
protected void print(int level) {
|
|||
|
for (int i = 0; i < level; i++) {
|
|||
|
System.out.print("--");
|
|||
|
}
|
|||
|
System.out.println("Composite:" + name);
|
|||
|
for (Component component : childs) {
|
|||
|
component.print(level + 1);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class Client {
|
|||
|
public static void main(String[] args) {
|
|||
|
Composite root = new Composite("root");
|
|||
|
Component node1 = new Leaf("1");
|
|||
|
Component node2 = new Composite("2");
|
|||
|
Component node3 = new Leaf("3");
|
|||
|
root.addChild(node1);
|
|||
|
root.addChild(node2);
|
|||
|
root.addChild(node3);
|
|||
|
Component node21 = new Leaf("21");
|
|||
|
Component node22 = new Composite("22");
|
|||
|
node2.addChild(node21);
|
|||
|
node2.addChild(node22);
|
|||
|
Component node221 = new Leaf("221");
|
|||
|
node22.addChild(node221);
|
|||
|
root.print();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
```html
|
|||
|
Composite:root
|
|||
|
--left:1
|
|||
|
--Composite:2
|
|||
|
----left:21
|
|||
|
----Composite:22
|
|||
|
------left:221
|
|||
|
--left:3
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 10 <20><> ״̬ģʽ
|
|||
|
|
|||
|
**1. ģʽ<C4A3><CABD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD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FBD><EFBFBD><EFBFBD>ʵ<EFBFBD>ֵġ<EFBFBD>
|
|||
|
|
|||
|
**2. ģʽ<C4A3><CABD>ͼ**
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c28fd93a-0d55-4a19-810f-72652feee00d.jpg)
|
|||
|
|
|||
|
**3. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD>ǹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۻ<EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۻ<EFBFBD><EFBFBD>в<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD>Է<EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>ƣ<EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪҲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı䡣
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7d880c9-740a-4a16-ac6d-be502281b4b2.jpg)
|
|||
|
|
|||
|
**4. ֱ<>ӽ<EFBFBD><D3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>ǹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>棬<EFBFBD>жϵ<EFBFBD>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BFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>Ҫί<EFBFBD>и<EFBFBD>״̬<EFBFBD>༴<EFBFBD>ɣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ֪<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD>ʲô״̬<EFBFBD>Լ<EFBFBD>״̬ʱ<EFBFBD><EFBFBD><EFBFBD>ν<EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD>Ƶġ<EFBFBD>
|
|||
|
|
|||
|
|
|||
|
**6. <20><><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5>**
|
|||
|
|
|||
|
```java
|
|||
|
public interface State {
|
|||
|
/**
|
|||
|
* Ͷ<><CDB6>25 <20><>Ǯ
|
|||
|
*/
|
|||
|
void insertQuarter();
|
|||
|
|
|||
|
/**
|
|||
|
* <20>˻<EFBFBD>25 <20><>Ǯ
|
|||
|
*/
|
|||
|
void ejectQuarter();
|
|||
|
|
|||
|
/**
|
|||
|
* ת<><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void turnCrank();
|
|||
|
|
|||
|
/**
|
|||
|
* <20><><EFBFBD><EFBFBD><EFBFBD>ǹ<EFBFBD>
|
|||
|
*/
|
|||
|
void dispense();
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class HasQuarterState implements State{
|
|||
|
private GumballMachine gumballMachine;
|
|||
|
|
|||
|
public HasQuarterState(GumballMachine gumballMachine){
|
|||
|
this.gumballMachine = gumballMachine;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void insertQuarter() {
|
|||
|
System.out.println("You can't insert another quarter");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void ejectQuarter() {
|
|||
|
System.out.println("Quarter returned");
|
|||
|
gumballMachine.setState(gumballMachine.getNoQuarterState());
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void turnCrank() {
|
|||
|
System.out.println("You turned...");
|
|||
|
gumballMachine.setState(gumballMachine.getSoldState());
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void dispense() {
|
|||
|
System.out.println("No gumball dispensed");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class NoQuarterState implements State {
|
|||
|
GumballMachine gumballMachine;
|
|||
|
|
|||
|
public NoQuarterState(GumballMachine gumballMachine) {
|
|||
|
this.gumballMachine = gumballMachine;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void insertQuarter() {
|
|||
|
System.out.println("You insert a quarter");
|
|||
|
gumballMachine.setState(gumballMachine.getHasQuarterState());
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void ejectQuarter() {
|
|||
|
System.out.println("You haven't insert a quarter");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void turnCrank() {
|
|||
|
System.out.println("You turned, but there's no quarter");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void dispense() {
|
|||
|
System.out.println("You need to pay first");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class SoldOutState implements State {
|
|||
|
|
|||
|
GumballMachine gumballMachine;
|
|||
|
|
|||
|
public SoldOutState(GumballMachine gumballMachine) {
|
|||
|
this.gumballMachine = gumballMachine;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void insertQuarter() {
|
|||
|
System.out.println("You can't insert a quarter, the machine is sold out");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void ejectQuarter() {
|
|||
|
System.out.println("You can't eject, you haven't inserted a quarter yet");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void turnCrank() {
|
|||
|
System.out.println("You turned, but there are no gumballs");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void dispense() {
|
|||
|
System.out.println("No gumball dispensed");
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class SoldState implements State {
|
|||
|
GumballMachine gumballMachine;
|
|||
|
|
|||
|
public SoldState(GumballMachine gumballMachine) {
|
|||
|
this.gumballMachine = gumballMachine;
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void insertQuarter() {
|
|||
|
System.out.println("Please wait, we're already giving you a gumball");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void ejectQuarter() {
|
|||
|
System.out.println("Sorry, you already turned the crank");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void turnCrank() {
|
|||
|
System.out.println("Turning twice doesn't get you another gumball!");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void dispense() {
|
|||
|
gumballMachine.releaseBall();
|
|||
|
if(gumballMachine.getCount()>0){
|
|||
|
gumballMachine.setState(gumballMachine.getNoQuarterState());
|
|||
|
} else{
|
|||
|
System.out.println("Oops, out of gumballs");
|
|||
|
gumballMachine.setState(gumballMachine.getSoldOutState());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class GumballMachine {
|
|||
|
private State soldOutState;
|
|||
|
private State noQuarterState;
|
|||
|
private State hasQuarterState;
|
|||
|
private State soldState;
|
|||
|
|
|||
|
private State state;
|
|||
|
private int count = 0;
|
|||
|
|
|||
|
public GumballMachine(int numberGumballs) {
|
|||
|
count = numberGumballs;
|
|||
|
soldOutState = new SoldOutState(this);
|
|||
|
noQuarterState = new NoQuarterState(this);
|
|||
|
hasQuarterState = new HasQuarterState(this);
|
|||
|
soldState = new SoldState(this);
|
|||
|
|
|||
|
if (numberGumballs > 0) {
|
|||
|
state = noQuarterState;
|
|||
|
} else {
|
|||
|
state = soldOutState;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void insertQuarter() {
|
|||
|
state.insertQuarter();
|
|||
|
}
|
|||
|
|
|||
|
public void ejectQuarter() {
|
|||
|
state.ejectQuarter();
|
|||
|
}
|
|||
|
|
|||
|
public void turnCrank() {
|
|||
|
state.turnCrank();
|
|||
|
state.dispense();
|
|||
|
}
|
|||
|
|
|||
|
public void setState(State state) {
|
|||
|
this.state = state;
|
|||
|
}
|
|||
|
|
|||
|
public void releaseBall() {
|
|||
|
System.out.println("A gumball comes rolling out the slot...");
|
|||
|
if (count != 0) {
|
|||
|
count -= 1;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public State getSoldOutState() {
|
|||
|
return soldOutState;
|
|||
|
}
|
|||
|
|
|||
|
public State getNoQuarterState() {
|
|||
|
return noQuarterState;
|
|||
|
}
|
|||
|
|
|||
|
public State getHasQuarterState() {
|
|||
|
return hasQuarterState;
|
|||
|
}
|
|||
|
|
|||
|
public State getSoldState() {
|
|||
|
return soldState;
|
|||
|
}
|
|||
|
|
|||
|
public int getCount() {
|
|||
|
return count;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
```java
|
|||
|
public class GumballMachineTestDrive {
|
|||
|
public static void main(String[] args) {
|
|||
|
GumballMachine gumballMachine = new GumballMachine(5);
|
|||
|
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.ejectQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
gumballMachine.ejectQuarter();
|
|||
|
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
gumballMachine.insertQuarter();
|
|||
|
gumballMachine.turnCrank();
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
```html
|
|||
|
You insert a quarter
|
|||
|
You turned...
|
|||
|
A gumball comes rolling out the slot...
|
|||
|
You insert a quarter
|
|||
|
Quarter returned
|
|||
|
You turned, but there's no quarter
|
|||
|
You need to pay first
|
|||
|
You insert a quarter
|
|||
|
You turned...
|
|||
|
A gumball comes rolling out the slot...
|
|||
|
You insert a quarter
|
|||
|
You turned...
|
|||
|
A gumball comes rolling out the slot...
|
|||
|
You haven't insert a quarter
|
|||
|
You insert a quarter
|
|||
|
You can't insert another quarter
|
|||
|
You turned...
|
|||
|
A gumball comes rolling out the slot...
|
|||
|
You insert a quarter
|
|||
|
You turned...
|
|||
|
A gumball comes rolling out the slot...
|
|||
|
Oops, out of gumballs
|
|||
|
You can't insert a quarter, the machine is sold out
|
|||
|
You turned, but there are no gumballs
|
|||
|
No gumball dispensed
|
|||
|
```
|
|||
|
|
|||
|
# <20><> 11 <20><> <20><><EFBFBD><EFBFBD>ģʽ // TODO
|
|||
|
|
|||
|
# <20><> 12 <20><> <20><><EFBFBD><EFBFBD>ģʽ
|
|||
|
|
|||
|
## 12.1 MVC
|
|||
|
|
|||
|
### 12.1.1 <20><>ͳ MVC
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>ͼʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>˹۲<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4f67611d-492f-4958-9fa0-4948010e345f.jpg)
|
|||
|
|
|||
|
### 12.1.2 Web <20>е<EFBFBD> MVC
|
|||
|
|
|||
|
ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ù۲<EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD>
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1dd56e61-2970-4d27-97c2-6e81cee86978.jpg)
|
|||
|
|
|||
|
# <20><> 13 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3>ദ
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD>壺<EFBFBD><EFBFBD>ij **<EFBFBD>龳** <20>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD>ij **<EFBFBD><EFBFBD><EFBFBD><EFBFBD>** <20><>ij<EFBFBD><C4B3> **<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>**<2A><>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD>ܵ<EFBFBD><EFBFBD>´<EFBFBD><EFBFBD>뱻<EFBFBD><EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD>̻<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫģʽ<EFBFBD>ĵط<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
<EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õĽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⡣<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>˾<EFBFBD><EFBFBD>治Ҫʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
ģʽ<EFBFBD><EFBFBD><EFBFBD>ࣺ
|
|||
|
|
|||
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/524a237c-ffd7-426f-99c2-929a6bf4c847.jpg)
|
|||
|
|
|||
|
# <20><> 14 <20><> ʣ<>µ<EFBFBD>ģʽ // TODO
|