「Java8」ファサード(Facade)を使うサンプル
環境
JavaSE 1.8
Eclipse 4.14.0
JavaSE 1.8
Eclipse 4.14.0
JavaSE 1.8 Eclipse 4.14.0
構文
1.Facade パターンあるいは Façade パターン(ファサード・パターン)とは、GoF(Gang of Four; 4人のギャングたち)によって定義された、コンピュータソフトウェアのデザインパターンの1つである。
2.呼び出し元は、ファサードを呼び、ファサードが個別の処理を行います。
3.ファサード(Facade)は、建物の正面部分という意味です。
使用例
package com.arkgame.study;
public class ArkgamelDemo {
public static void main(String[] args) {
// 呼び出し側のクラス
DemoFacade df = new DemoFacade();
//ファサードクラスのメソッド
df.func();
}
}
// ファサードのクラス
class DemoFacade {
void func() {
ChangA cftA = new ChangA();
ChangB cftB = new ChangB();
System.out.println(cftA.getCity() + " 11 " + cftB.getCity() + " 22 ");
}
}
// ChangeAクラスの定義
class ChangA {
String getCity() {
return "東京";
}
}
// ChangeBクラスの定義
class ChangB {
String getCity() {
return "大阪";
}
}
package com.arkgame.study;
public class ArkgamelDemo {
public static void main(String[] args) {
// 呼び出し側のクラス
DemoFacade df = new DemoFacade();
//ファサードクラスのメソッド
df.func();
}
}
// ファサードのクラス
class DemoFacade {
void func() {
ChangA cftA = new ChangA();
ChangB cftB = new ChangB();
System.out.println(cftA.getCity() + " 11 " + cftB.getCity() + " 22 ");
}
}
// ChangeAクラスの定義
class ChangA {
String getCity() {
return "東京";
}
}
// ChangeBクラスの定義
class ChangB {
String getCity() {
return "大阪";
}
}
package com.arkgame.study; public class ArkgamelDemo { public static void main(String[] args) { // 呼び出し側のクラス DemoFacade df = new DemoFacade(); //ファサードクラスのメソッド df.func(); } } // ファサードのクラス class DemoFacade { void func() { ChangA cftA = new ChangA(); ChangB cftB = new ChangB(); System.out.println(cftA.getCity() + " 11 " + cftB.getCity() + " 22 "); } } // ChangeAクラスの定義 class ChangA { String getCity() { return "東京"; } } // ChangeBクラスの定義 class ChangB { String getCity() { return "大阪"; } }
実行結果
東京 11 大阪 22
東京 11 大阪 22
東京 11 大阪 22