Design-pattern-facade-pattern
提供:Dev Guides
デザインパターン-ファサードパターン
ファサードパターンは、システムの複雑さを隠し、クライアントがシステムにアクセスできるクライアントへのインターフェイスを提供します。 このタイプのデザインパターンは、複雑さを隠すために既存のシステムにインターフェイスを追加するため、構造パターンの下にあります。
このパターンには、クライアントが必要とする単純化されたメソッドを提供する単一のクラスが含まれ、既存のシステムクラスのメソッドへの呼び出しを委任します。
実装
_Shape_インターフェイスと、_Shape_インターフェイスを実装する具体的なクラスを作成します。 ファサードクラス_ShapeMaker_は、次のステップとして定義されます。
_ShapeMaker_クラスは、具象クラスを使用して、ユーザー呼び出しをこれらのクラスに委任します。 デモクラス_FacadePatternDemo_は、_ShapeMaker_クラスを使用して結果を表示します。
ステップ1
インターフェイスを作成します。
Shape.java
public interface Shape {
void draw();
}
ステップ2
同じインターフェースを実装する具体的なクラスを作成します。
Rectangle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
Square.java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
Circle.java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
ステップ3
ファサードクラスを作成します。
ShapeMaker.java
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
ステップ4
ファサードを使用して、さまざまな種類の図形を描画します。
FacadePatternDemo.java
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
ステップ5
出力を確認します。
Circle::draw()
Rectangle::draw()
Square::draw()