home > デザインパターン >

ForNext

Only Do What Only You Can Do

Composite

VBScript

JScript

Perl

PHP

Python

Ruby

PowerShell

Scala

F#

C

C++

C++Builder

VC++

C#

Java

更新日 : 2010.11.25

■ Step 1 パターン適用前

■ MyMusic
public class MyMusic {
    private String name;

    public MyMusic(String name) {
        this.name = name;
    }
    public void viewName() {
        System.out.println("        " + name);
    }
}
■ MyAlbum
import java.util.*;

public class MyAlbum {
    private String        name;
    private List musicList;

    public MyAlbum(String name) {
        this.name = name;
        musicList = new ArrayList();
    }
    public void addMusic(MyMusic music) {
        musicList.add(music);
    }
    public void viewName() {
        System.out.println("    " + name);
        for(MyMusic music : musicList) {
            music.viewName();
        }
    }
}
■ MyArtist
import java.util.*;

public class MyArtist {
    private String        name;
    private List albumList;

    public MyArtist(String name) {
        this.name = name;
        albumList = new ArrayList();
    }
    public void addAlbum(MyAlbum album) {
        albumList.add(album);
    }
    public void viewName() {
        System.out.println(name);
        for(MyAlbum album : albumList) {
            album.viewName();
        }
    }
}
■ MyMain
public class MyMain {
    public static void main(String args[]) {
        MyArtist artist = new MyArtist("矢沢永吉");

        MyAlbum album = new MyAlbum("TWIST");
        artist.addAlbum(album);
        album.addMusic(new MyMusic("サイコーな Rock You!"));
        album.addMusic(new MyMusic("Shake Me"));

        album = new MyAlbum("ROCK'N'ROLL");
        artist.addAlbum(album);
        album.addMusic(new MyMusic("トレジャー・ハンター"));
        album.addMusic(new MyMusic("コバルトの空"));

        album = new MyAlbum("ONLY ONE");
        artist.addAlbum(album);

        artist.viewName();
    }
}
L:\>java MyMain
矢沢永吉
    TWIST
        サイコーな Rock You!
        Shake Me
    ROCK'N'ROLL
        トレジャー・ハンター
        コバルトの空
    ONLY ONE

■ Step 2 Composite

■ MyNode
public interface MyNode {
    public void viewName(int level);
}
■ MyLeaf
public class MyLeaf implements MyNode {
    private String name;

    public MyLeaf(String name) {
        this.name = name;
    }
    public void viewName(int level) {
        for (int i = 0; i < level; i++)
            System.out.print("    ");
        System.out.println(name);
    }
}
■ MyComposite
import java.util.*;

public class MyComposite implements MyNode {
    private String       name;
    private List<MyNode> nodeList;

    public MyComposite(String name) {
        this.name = name;
        nodeList  = new ArrayList<MyNode>();
    }
    public void addNode(MyNode node) {
        nodeList.add(node);
    }
    public void viewName(int level) {
        for (int i = 0; i < level; i++)
            System.out.print("    ");
        System.out.println(name);

        for(MyNode node : nodeList) {
            node.viewName(level + 1);
        }
    }
}
■ MyMain
public class MyMain {
    public static void main(String args[]) {
        MyComposite artist = new MyComposite("矢沢永吉");

        MyComposite album = new MyComposite("TWIST");
        artist.addNode(album);
        album.addNode(new MyLeaf("サイコーな Rock You!"));
        album.addNode(new MyLeaf("Shake Me"));

        album = new MyComposite("ROCK'N'ROLL");
        artist.addNode(album);
        album.addNode(new MyLeaf("トレジャー・ハンター"));
        album.addNode(new MyLeaf("コバルトの空"));

        album = new MyComposite("ONLY ONE");
        artist.addNode(album);

        album = new MyComposite("EIKICHI YAZAWA CONCERT TOUR \"Z\" 2001");
        artist.addNode(album);
        MyComposite disc = new MyComposite("Disc 1");
        album.addNode(disc);
        disc.addNode(new MyLeaf("さまよい"));
        disc.addNode(new MyLeaf("ため息"));

        artist.viewName(0);
    }
}
L:\>java MyMain
矢沢永吉
    TWIST
        サイコーな Rock You!
        Shake Me
    ROCK'N'ROLL
        トレジャー・ハンター
        コバルトの空
    ONLY ONE
    EIKICHI YAZAWA CONCERT TOUR "Z" 2001
        Disc 1
            さまよい
            ため息

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system