2018-02-20 10:40:05 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* [?? 1 ?? ?????????](#??-1-??-?????????)
|
|
|
|
|
* [?? 2 ?? ???????](#??-2-??-???????)
|
|
|
|
|
* [?? 3 ?? ?????](#??-3-??-?????)
|
|
|
|
|
* [?? 4 ?? ??????](#??-4-??-??????)
|
|
|
|
|
* [4.1 ?????](#41-?????)
|
|
|
|
|
* [4.2 ??????????](#42-??????????)
|
|
|
|
|
* [4.3 ???????](#43-???????)
|
|
|
|
|
* [?? 5 ?? ??????](#??-5-??-??????)
|
|
|
|
|
* [?? 6 ?? ??????](#??-6-??-??????)
|
|
|
|
|
* [?? 7 ?? ???????????????](#??-7-??-???????????????)
|
|
|
|
|
* [7.1 ????????](#71-????????)
|
|
|
|
|
* [7.2 ?????](#72-?????)
|
|
|
|
|
* [?? 8 ?? ??<3F><>????](#??-8-??-??<3F><>????)
|
|
|
|
|
* [?? 9 ?? ?????????????](#??-9-??-?????????????)
|
|
|
|
|
* [9.1 ????????](#91-????????)
|
|
|
|
|
* [9.2 Java ??????????](#92-java-??????????)
|
|
|
|
|
* [9.3 ?????](#93-?????)
|
|
|
|
|
* [?? 10 ?? ????](#??-10-??-????)
|
|
|
|
|
* [?? 11 ?? ?????? // TODO](#??-11-??-??????--todo)
|
|
|
|
|
* [?? 12 ?? ??????](#??-12-??-??????)
|
2018-02-20 10:40:05 +08:00
|
|
|
|
* [12.1 MVC](#121-mvc)
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* [12.1.1 ??? MVC](#1211-???-mvc)
|
|
|
|
|
* [12.1.2 Web ?<3F><>? MVC](#1212-web-?<3F><>?-mvc)
|
|
|
|
|
* [?? 13 ?? ?????????](#??-13-??-?????????)
|
|
|
|
|
* [?? 14 ?? ?????? // TODO](#??-14-??-??????--todo)
|
2018-02-20 10:40:05 +08:00
|
|
|
|
<!-- GFM-TOC -->
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 1 ?? ?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ?????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????<3F><>?????????????????<3F>`?<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????<3F><>???????????<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??<3F><>?<3F><>?????????????????????????????????????????????????????<3F><>????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/144d28a0-1dc5-4aba-8961-ced5bc88428a.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**????<3F><>**??????<3F><>???????<3F><>???<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**??????????????????????** ?????????????????????????????????????????<3F><>????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????<3F><>???<3F><>???????????????????????<3F><>???<3F><>???????????????????<3F><>???<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1c8ccf5c-7ecd-4b8a-b160-3f72a510ce26.png)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**???????????<3F><>??** ???????? has-a ????????????????????????????????????????<3F><>???????????????????<3F><>?????<3F><>??????????<3F><>??????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????? Duck ??????? FlyBehavior ?? QuackBehavior ??performQuack() ?? performFly() ??????<3F><>???????????????????????????? Duck ??????????????????? FlyBehavior ?? QuackBehavior ???????????????????????<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/29574e6f-295c-444e-83c7-b162e8a73a83.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/e13833c8-e215-462e-855c-1d362bb8d4a0.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**??????** ???????????<3F><>???????????????????????????<3F>I????????????<3F><>?????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**7. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
```html
|
|
|
|
|
QuackBehavior.Quack
|
|
|
|
|
FlyBehavior.FlyWithWings
|
|
|
|
|
FlyBehavior.FlyNoWay
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 2 ?? ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????????????????????????????????????????????Subject??????????????????????????Observer???????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/26cb5e7e-6fa3-44ad-854e-fe24d1a5278c.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????<3F><>??????????????????????????????????????????????????????<3F><>????????<3F><>???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????<3F><>??????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5c558190-fccd-4b5e-98ed-1896653fc97f.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????????????????<3F><>???????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/760a5d63-d96d-4dd9-bf9a-c3d126b2f401.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**?????????????????????????** ????????????????????????????????????????????????????????????????????????<3F><>????????????????????<3F><>??????????<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
```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
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 3 ?? ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????<3F><>??????????????????????????????????<3F><>??<3F><>?????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????? DarkRoast ???? Mocha ??????Mocha ??????? Whip ??????????????????????????????? cost() ????????????????? cost() ??????????????????? cost() ???????????????? DarkRoast ????? Mocha??????????? Mocha ???? DarkRoast?????????? Whip ?????? Whip ???? Mocha???????? cost() ?????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/41a4cb30-f393-4b3b-abe4-9941ccf8fa1f.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????<3F><>?????????????????????????????????????????????????????????????????????????????????????????<3F><>??<3F><>?????????????????????<3F><>??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>?<3F><>??????????<3F><>??????????????????????<3F><>??????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/3dc454fb-efd4-4eb8-afde-785b2182caeb.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ???????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/9c997ac5-c8a7-44fe-bf45-2c10eb773e53.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**??????????????????????**???????????????????????????????????<3F><>??????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. Java I/O ?<3F><>????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/2a40042a-03c8-4556-ad1f-72d89f8c555c.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**7. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
3.0
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 4 ?? ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 4.1 ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?<3F><>???? Pizza??????????????<3F><>???????????????? Pizza ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>??<3F><>????????????????????????????????????????????????????????????????????????????????<3F><>????????????????????????????????<3F><>???????????<3F><>????????????<3F><>????????????????????????????????????????????????????<3F><>?????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c470eb9b-fb05-45c5-8bb7-1057dc3c16de.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/dc3e704c-7c57-42b8-93ea-ddd068665964.jpg)
|
|
|
|
|
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
CheesePizza
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 4.2 ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????? Pizza ???????????????????????????<3F><>?????????????????<3F><>?????????????? cheese ????? Pizza ?????????????????? Pizza ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????<3F><>???????????????????????????????<3F><>???????????????????????????????????????????????????????????????????????????????????????????<3F><>????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/903093ec-acc8-4f9b-bf2c-b990b9a5390c.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/664f8901-5dc7-4644-a072-dad88cc5133a.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
NYStyleCheesePizza is making..
|
|
|
|
|
ChicagoStyleCheesePizza is making..
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 4.3 ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**???????????**??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>????? PizzaStore ???????????????????????????? Pizza ?????????????????? Pizza ?????????????????<3F><>??? Pizza ????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ddf72ca9-c0be-49d7-ab81-57a99a974c8e.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????<3F><>???????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????<3F><>???????????????????????????<3F><>??????????????????????????????????????????????????????????????????<3F><>?????????????????????????????????????????????????AbstractFactory ?<3F><>? CreateProductA ?? CreateProductB ????????????????????????????????????????????????????????????????????<3F>^?????????????????????????? Client ?????Client ???? AbstractFactory ????????????????????????????????????????????????<3F><>?????????Client ??????????????<3F><>??????????????????????????????????????? Cilent ????? AbstractFactory ????????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d301774f-e0d2-41f3-95f4-bfe39859b52e.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8785dabd-1285-4bd0-b3aa-b05cc060a24a.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
ThickCrustDough
|
|
|
|
|
MarinaraSauce
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 5 ?? ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????? Java ??????????<3F><>????????????<3F><>???????????????<3F><>?????????<3F><>?????????<3F><>????????????????<3F><>?????????????????<3F><>?????<3F><>????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/59aff6c1-8bc5-48e4-9e9c-082baeb2f274.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????<3F><>???<3F><>???????????????????????????????????????????????????????????<3F><>??????????????????????????????????????????????????????????????? if(uniqueInstance == null) ??????<3F><>????????????? uniqueInstance ??<3F><>????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public class Singleton {
|
|
|
|
|
|
|
|
|
|
private static Singleton uniqueInstance;
|
|
|
|
|
|
|
|
|
|
private Singleton() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Singleton getUniqueInstance() {
|
|
|
|
|
if (uniqueInstance == null) {
|
|
|
|
|
uniqueInstance = new Singleton();
|
|
|
|
|
}
|
|
|
|
|
return uniqueInstance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ???????????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????? getUniqueInstance() ????????????????<3F><>????????????????????????????? uniqueInstance ???????<3F><>?????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public static synchronized Singleton getUniqueInstance() {
|
|
|
|
|
if (uniqueInstance == null) {
|
|
|
|
|
uniqueInstance = new Singleton();
|
|
|
|
|
}
|
|
|
|
|
return uniqueInstance;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ????????????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private static Singleton uniqueInstance = new Singleton();
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. ????????????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????? getUniqueInstance() ???????<3F><>?????????????????? uniqueInstance = new Singleton(); ?????????????<3F><>??????????????????<3F><>? uniqueInstance ???????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 6 ?? ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????<3F><>??????????????????????????????????????????????<3F><>??????<3F><>????????????<3F><>
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](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)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????<3F><>????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1e09d75f-6268-4425-acf8-8ecd1b4a0ef3.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
Invoker ?????????????????????????????????????? execute() ??????Receiver ???????????????????????ConcreteCommand ?????????? Receiver ??????????????<3F><>? Receiver ??????????????? LightOnCommand ????? excute ??????<3F><>? Light ??????????Light ??????????? on() ??????????????Invoker ???? Client ??????????????????????? Invoker ?????????????????? Client ????????????<3F><>??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/5ef94f62-98ce-464d-a646-842d9c72c8b8.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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
|
|
|
|
|
/**
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Light is on!
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 7 ?? ???????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 7.1 ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/8e8ba824-7a9e-4934-a212-e6a41dcc1602.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????????????????????????????????????????????Adapter??????????????????Adaptee???????????????????<3F><>??????????????????????<3F><>?Adapter ??????? Target ?? Adaptee ?????????????? Adaptee ??????????????? Adapter ????????????? Target ?????????? Client ?????????????? Target ???????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](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)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????Duck????????Turkey????Duck ?? quack() ???????? Turkey ??? gobble() ?????????????? Turkey ??? Duck ?? quack() ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1a511c76-bb6b-40ab-b8aa-39eeb619d673.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
```html
|
|
|
|
|
gobble!
|
|
|
|
|
fly!
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 7.2 ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????<3F><>??????????????????????????????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/78f2314e-2643-41df-8f3d-b7e28294094b.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????<3F><>???????????????????<3F><>???????????<3F><>????????<3F><>???????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/106f5585-b2e7-4718-be5d-3b322d1ef42a.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/25387681-89f8-4365-a2fa-83b86449ee84.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**?????????**????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 8 ?? ??<3F><>????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????<3F><>??????????????????<3F><>?????????????<3F><>?
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??<3F><>????????????????????????????????????????<3F><>??<3F><>???<3F><>
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??<3F><>?? templateMethod() ?????????????????? primitiveOperation1() ?? primitiveOperation2() ??????<3F><>????? primitiveOperation1() ?? primitiveOperation2() ???????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/ed62f400-192c-4185-899b-187958201f0c.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?<3F><>????<3F><>?????????????????<3F><>??????<3F><>??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/d8f873fc-00bc-41ee-a87c-c1b4c0172844.png)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ??????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/aa20c123-b6b5-432a-83d3-45dc39172192.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**?????????**??????????<3F><>????????????????????<3F><>??????????????????????????????????????????????????????????????????????????????<3F><>?????????????<3F><>???????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. ????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????hock?????<3F><>????????????<3F><>??<3F><>????????????????????????????????????????<3F><>?? templteMethod() ?<3F><>??????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**7. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
boilWater
|
|
|
|
|
Coffee.brew
|
|
|
|
|
pourInCup
|
|
|
|
|
Coffee.addCondiments
|
|
|
|
|
-----------
|
|
|
|
|
boilWater
|
|
|
|
|
Tea.brew
|
|
|
|
|
pourInCup
|
|
|
|
|
Tea.addCondiments
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 9 ?? ?????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 9.1 ????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????<3F><>????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/439deca7-fed0-4c89-87e5-7088d10f1fdb.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
```html
|
|
|
|
|
0
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 9.2 Java ??????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
Java ????????? Iterator ????????? Java ??????????????????? Iterable ????????????? iterator() ??????????? Iterator ??????? Java ??????????????????????????? foreach ????????????????<3F><>????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
## 9.3 ?????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????<3F><>???????????? / ???????<3F><>???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????Composite???????????????Component???????????????<3F><>?????<3F><>????<3F><>???????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f99c019e-7e91-4c2e-b94d-b031c402dcb5.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```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) {
|
2018-02-22 14:47:22 +08:00
|
|
|
|
throw new UnsupportedOperationException(); // ???????????????????? , ??????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
Composite:root
|
|
|
|
|
--left:1
|
|
|
|
|
--Composite:2
|
|
|
|
|
----left:21
|
|
|
|
|
----Composite:22
|
|
|
|
|
------left:221
|
|
|
|
|
--left:3
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 10 ?? ????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**1. ??????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????<3F><>????????????????????????????????????????????????<3F><>???????????????????????<3F><>???????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**2. ?????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/c28fd93a-0d55-4a19-810f-72652feee00d.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**3. ????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????<3F><>????????????????????<3F><>???????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/f7d880c9-740a-4a16-ac6d-be502281b4b2.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**4. ?????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????<3F><>?<3F><>??????????????????????<3F><>????????????????????????????????????????<3F><>???????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/62ebbb63-8fd7-4488-a866-76a9dc911662.png)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**5. ???????????????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????????????????<3F><>???????????????????<3F><>???????????????????????????????????<3F><>????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
**6. ???????**
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
public interface State {
|
|
|
|
|
/**
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* ???25 ???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void insertQuarter();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* ???25 ???
|
2018-02-20 10:40:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void ejectQuarter();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
*/
|
|
|
|
|
void turnCrank();
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-22 14:47:22 +08:00
|
|
|
|
* ???????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
```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
|
|
|
|
|
```
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 11 ?? ?????? // TODO
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 12 ?? ??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
## 12.1 MVC
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 12.1.1 ??? MVC
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/4f67611d-492f-4958-9fa0-4948010e345f.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
### 12.1.2 Web ?<3F><>? MVC
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
????????<3F><>????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/1dd56e61-2970-4d27-97c2-6e81cee86978.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 13 ?? ?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???<3F><>??? **??** ??????? **????** ????? **???????**??
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
?????????????????????????????????????????????????????????????????????????????????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
???????????????????????????????????????????<3F><>??????<3F><>?????????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
??????
|
2018-02-20 10:40:05 +08:00
|
|
|
|
|
|
|
|
|
![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/524a237c-ffd7-426f-99c2-929a6bf4c847.jpg)
|
|
|
|
|
|
2018-02-22 14:47:22 +08:00
|
|
|
|
# ?? 14 ?? ?????? // TODO
|