Skip to content

Commit

Permalink
添加json 全解析 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
mahao committed Jun 7, 2016
1 parent feb09b6 commit fb38a78
Show file tree
Hide file tree
Showing 23 changed files with 565 additions and 114 deletions.

This file was deleted.

This file was deleted.

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();
}
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();
}
}
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();
}

}
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);

//这样,他们的依赖关系是在外部通过注入的方式,实现了
}
}

This file was deleted.

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();
}


}
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;
}
}
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();

}
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();
}
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;
}

}
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 => =>");
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;

import com.mahao.alex.architecture.dagger2.User;

import javax.inject.Singleton;

Expand All @@ -19,5 +18,4 @@ public interface AppComponent {

SPUtils spUtils();

User getUser();
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.android.tools.build:gradle:2.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Loading

0 comments on commit fb38a78

Please sign in to comment.