-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mahao
committed
Jun 7, 2016
1 parent
feb09b6
commit fb38a78
Showing
23 changed files
with
565 additions
and
114 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
architecture/src/main/java/com/mahao/alex/architecture/dagger2/ActivityComponent.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
architecture/src/main/java/com/mahao/alex/architecture/dagger2/ActivityModule.java
This file was deleted.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
architecture/src/main/java/com/mahao/alex/architecture/dagger2/Coffee.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import dagger.Component; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
|
||
@Singleton | ||
@Component(modules = DripCoffeeModule.class) | ||
public interface Coffee { | ||
CoffeeMaker maker(); | ||
} |
12 changes: 12 additions & 0 deletions
12
architecture/src/main/java/com/mahao/alex/architecture/dagger2/CoffeeApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public class CoffeeApp { | ||
|
||
public static void main(String[] args){ | ||
Coffee coffee = DaggerCoffee.builder().build(); | ||
coffee.maker().brew(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
architecture/src/main/java/com/mahao/alex/architecture/dagger2/CoffeeMaker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
import javax.inject.Inject; | ||
|
||
import dagger.Lazy; | ||
|
||
/** | ||
* | ||
* 是否存在一种方法,使我们在调用时自动构造对象, | ||
* 及通过工具类的形式,让他们组装好 | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public class CoffeeMaker { | ||
|
||
//懒惰注入,使用到该对象时,才注入并创建实例 | ||
private final Lazy<Heater> heater; | ||
|
||
private final Pump pump; | ||
|
||
|
||
@Inject | ||
public CoffeeMaker(Lazy<Heater> heater,Pump pump) { | ||
this.heater = heater; // 加热器 | ||
this.pump = pump; // 构造抽水器 | ||
} | ||
|
||
public void brew() { | ||
//煮咖啡 | ||
heater.get().on(); | ||
pump.pump(); | ||
System.out.println(" [_]P coffee! [_]P "); | ||
heater.get().off(); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
architecture/src/main/java/com/mahao/alex/architecture/dagger2/CoffeeMakerOld.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
/** | ||
* | ||
* 如果有如下需求,我们有一个咖啡机,他具有加热部件和抽水部件 | ||
* 同时他具有煮咖啡的功能,我们根据描述,可构造如下对象 | ||
* | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public class CoffeeMakerOld { | ||
|
||
private final Heater heater; | ||
|
||
private final Pump pump; | ||
|
||
|
||
public CoffeeMakerOld() { | ||
this.heater = new ElectricHeater(); // 加热器 | ||
this.pump = new Thermosiphon(heater); // 构造抽水器 | ||
} | ||
|
||
public void brew() { | ||
//煮咖啡 | ||
} | ||
|
||
} | ||
|
||
//----对于上面的类,存在一个弊端,加热器和抽水器实在类内构造------- | ||
|
||
|
||
/** | ||
* 这样的优点是,我们可以根据需要组装不同的咖啡, | ||
*/ | ||
class CoffeeMakerOld2{ | ||
private final Heater heater; | ||
|
||
private final Pump pump; | ||
|
||
|
||
public CoffeeMakerOld2(Heater heater,Pump pump) { | ||
this.heater = heater; // 加热器 | ||
this.pump = pump; // 构造抽水器 | ||
} | ||
|
||
public void brew() { | ||
//煮咖啡 | ||
} | ||
|
||
|
||
public static void main(String[] args){ | ||
|
||
Heater heater = new ElectricHeater(); | ||
|
||
Pump pump = new Thermosiphon(heater); | ||
|
||
CoffeeMakerOld2 coffeeMarker = new CoffeeMakerOld2(heater,pump); | ||
|
||
//这样,他们的依赖关系是在外部通过注入的方式,实现了 | ||
} | ||
} |
40 changes: 0 additions & 40 deletions
40
architecture/src/main/java/com/mahao/alex/architecture/dagger2/Dagger2Activity.java
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
architecture/src/main/java/com/mahao/alex/architecture/dagger2/DripCoffeeModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
@Module(includes = PumpModule.class) | ||
public class DripCoffeeModule { | ||
|
||
@Provides @Singleton Heater provideHeater(){ | ||
|
||
return new ElectricHeater(); | ||
} | ||
|
||
|
||
} |
23 changes: 23 additions & 0 deletions
23
architecture/src/main/java/com/mahao/alex/architecture/dagger2/ElectricHeater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
/** | ||
* | ||
* 加热器的实现类 | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public class ElectricHeater implements Heater { | ||
boolean heating; | ||
|
||
@Override public void on() { | ||
System.out.println("~ ~ ~ heating ~ ~ ~"); | ||
this.heating = true; | ||
} | ||
|
||
@Override public void off() { | ||
this.heating = false; | ||
} | ||
|
||
@Override public boolean isHot() { | ||
return heating; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
architecture/src/main/java/com/mahao/alex/architecture/dagger2/Heater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
/** | ||
* | ||
* 咖啡的加热器 | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public interface Heater { | ||
|
||
|
||
void on(); | ||
|
||
void off(); | ||
|
||
boolean isHot(); | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
architecture/src/main/java/com/mahao/alex/architecture/dagger2/Pump.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public interface Pump { | ||
void pump(); | ||
} |
17 changes: 17 additions & 0 deletions
17
architecture/src/main/java/com/mahao/alex/architecture/dagger2/PumpModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
@Module | ||
public class PumpModule { | ||
|
||
@Provides Pump providePump(Thermosiphon pump){ | ||
|
||
return pump; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
architecture/src/main/java/com/mahao/alex/architecture/dagger2/Thermosiphon.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.mahao.alex.architecture.dagger2; | ||
|
||
import javax.inject.Inject; | ||
|
||
/** | ||
* Created by Alex_MaHao on 2016/5/19. | ||
*/ | ||
public class Thermosiphon implements Pump { | ||
|
||
private final Heater heater; | ||
|
||
|
||
@Inject | ||
public Thermosiphon(Heater heater) { | ||
this.heater = heater; | ||
} | ||
|
||
@Override public void pump() { | ||
if (heater.isHot()) { | ||
System.out.println("=> => pumping => =>"); | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
architecture/src/main/java/com/mahao/alex/architecture/dagger2/User.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.