0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Composite Patterの要素を取得する

Design Pattarm MENU

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

クラス 説明
interface
face
型とaccept()を定義する
faceImple.class faceを実装する
faceList.class faceを実装する
Listを持つ
Listのiteratorを発行するメソッドを持つ
Visitor.class Visitor Patternを提供する
user(Main.class) 動作を確認する

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

interface_face
interface face{
  void accept(Visitor visit);
}
faceImple.class
class faceImple implements face{
  public void accept(Visitor visitor){
         System.out.println("."+this);}
}
faceList.class
class faceList implements face{
  List ls = new ArrayList();
  void      add( face fc){ls.add(fc);}
  public    void accept(Visitor visit){
            visit.visitor(this);}
  Iterator  iterate(){return ls.iterator();}
}
Visitor.class
class Visitor{
      void visitor( faceImple imple ){
           imple.accept(this);}
      void visitor( faceList list ){
           Iterator it = list.iterate();
           while(it.hasNext()){
                 face fc = (face) it.next();
                 System.out.println("/"+fc);
                 fc.accept(this);
           }
      }
}
user(Main.class)
public static void main(String[] args){
       faceList f1 = new faceList();
       faceList f2 = new faceList();
       faceList f3 = new faceList();
       f3.add(new  faceImple());
       f3.add(new  faceImple());
       f2.add(new  faceImple());
       f2.add(f3);
       f1.add(f2);
       f1.accept(new Visitor());
}
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?