LoginSignup
1
0

More than 3 years have passed since last update.

Bridge Pattern

Last updated at Posted at 2020-11-11

メソッドを実装する側と、メソッドを利用する側を分離し、双方を実装クラスのインスタンスで関連づける
・実装側はメソッドの定義と具象化
・利用側は実装側インスタンスをフィールドで所持し、インスタンス経由で実装側のメソッドを利用

Design Pattarm MENU

以下のクラス構成で確認します

クラス 説明
実装側 abstract
class superSam
メソッドを定義
実装側 sam.class superSamを実装
利用側 useSam.class 実装側のメソッドを利用

*user 他の開発者がこのパターンを利用する、という意味合いを含みます

下記にクラスを作成します

superSam.class
abstract class superSam{
  String str;
  superSam(String str){this.str=str;}
  abstract void todo();
}
sam.class
class sam extends superSam{
  sam(String str){super(str);}
  void todo(){System.out.println(super.str);}
}
useSam.class
class useSam{
  sam sam;
  useSam(sam sam){this.sam=sam;}
  void exec(){sam.todo();}
}
user(Main.class)
public static void main(String[] args){
  useSam use1 = new useSam (new sam("Hello java"));
    use1.exec();
}}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0