Skip to content

Instantly share code, notes, and snippets.

@bg1bgst333
Created May 18, 2020 16:15
Show Gist options
  • Save bg1bgst333/9b292f4ce685221173a145e198bd6463 to your computer and use it in GitHub Desktop.
Save bg1bgst333/9b292f4ce685221173a145e198bd6463 to your computer and use it in GitHub Desktop.
annotationModule#annotationModule
package com.bgstation0.dagger.sample.am_;
import javax.inject.Inject;
// コーヒーメーカー
public class CoffeeMaker {
// メンバフィールドの定義
@Inject Heater1 heater;
@Inject PumpBase pump;
// コンストラクタの定義
@Inject
CoffeeMaker(){
}
// ドリップ.
public String drip(){
String dripStr = "";
dripStr = dripStr + heater.heating();
dripStr = dripStr + pump.pumping();
return dripStr;
}
}
package com.bgstation0.dagger.sample.am_;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import dagger.Component;
// メインアクティビティ
public class MainActivity extends AppCompatActivity {
// メンバフィールドの定義
private CoffeeShop coffeeShop;
// アクティビティ生成時.
@Override
protected void onCreate(Bundle savedInstanceState) {
// 既定の処理.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// コーヒーショップの生成.
//coffeeShop = DaggerMainActivity_CoffeeShop.create(); // DaggerMainActivity_CoffeeShop.createでcoffeeShopを作成.
coffeeShop = DaggerMainActivity_CoffeeShop.builder().pumpBaseModule(new PumpBaseModule()).build(); // PumpBaseModuleに差し替え.
// ドリップ.
TextView textView1 = findViewById(R.id.textview1); // textView1を取得.
textView1.setText(coffeeShop.maker().drip()); // textView1.setTextにdrip.
}
// コーヒーメーカー(PumpBaseModuleを指定.)
@Component(modules = PumpBaseModule.class)
interface CoffeeShop{
CoffeeMaker maker();
}
}
package com.bgstation0.dagger.sample.am_;
import javax.inject.Inject;
// パンプ1
public class Pump1 implements PumpBase{
// コンストラクタ
@Inject
Pump1(){
}
// 組み上げ
@Override
public String pumping(){
return "pumb1";
}
}
package com.bgstation0.dagger.sample.am_;
// パンプベース
public interface PumpBase {
public String pumping();
}
package com.bgstation0.dagger.sample.am_;
import dagger.Module;
import dagger.Provides;
// パンプベースモジュール.
@Module
public class PumpBaseModule {
// パンプベースの提供.
@Provides static PumpBase providePumpBase(Pump1 pump1){
return pump1; // pump1を返す.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment