コンポジット構造図とは、クラスやコンポーネントの内部構造を表現するダイアグラムである。コンポジット構造図は次のことを表現できる。
- クラスやコンポーネントの内部と外部の境界を表現できる。
- クラスやコンポーネント内のクラスやコンポーネントの結びつきや役割を表現できる。
名称 | 図 | 意味 |
クラス |
|
内部構造を表現したいクラスやコンポーネントはこの図で表す。
- クラス
- オブジェクトを抽象化したもの。
- コンポーネント
- インタフェースを持つクラス。
|
コンポーネント |
パート |
|
クラスやコンポーネントを構成する内部要素。部品のこと。パートもまたクラスやコンポーネントである。ロール名や多重度を表現可能。
|
コネクタ |
|
パート間に関係があることを示す。
|
ポート |
|
クラス(またはコンポーネント)の内部と外部の境界を表す。
|
インタフェース |
提供インタフェース
| クラスやコンポーネントが外部に公開している機能を表す。 |
要求インタフェース
| クラスやコンポーネントが必要とする外部機能を表す。 |
例えばMP3とWMA形式、生の音楽ファイル(例えばWAV)を再生する音楽プレイヤーを表現したものが、以下のコンポジット図である。
前述のサンプルコードの全体像は次の通りである(曲の制御部分については省略)。
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
/* 音楽 */
class Audio {
public final String name;
public final String path;
Audio(String n, String p){
name = n;
path = p;
}
}
/* コントローラ */
class Controller {
private ArrayList<Audio> playlist;
private int current_idx = 0;
public Controller(ArrayList<Audio> pl){
playlist = pl;
}
public void addAudio(Audio audio){
playlist.add(audio);
}
public void start(){
playlist.get(current_idx);
/* 再生処理 */
}
public void stop(){
/* 停止処理 */
}
public void next(){
/* 次の曲へ */
}
public void prev(){
/* 前の曲へ */
}
}
/* インタフェース */
class GInterface extends JFrame {
Controller controller;
JButton start;
JButton stop;
JButton prev;
JButton next;
JButton adda;
class StartListener implements ActionListener {
public void actionPerformed(ActionEvent e){
controller.start();
}
}
class StopListener implements ActionListener {
public void actionPerformed(ActionEvent e){
controller.stop();
}
}
class NextListener implements ActionListener {
public void actionPerformed(ActionEvent e){
controller.next();
}
}
class PrevListener implements ActionListener {
public void actionPerformed(ActionEvent e){
controller.prev();
}
}
class AddListener implements ActionListener {
public void actionPerformed(ActionEvent e){
JFileChooser filechooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("mp3 & wma & wav audio file", "mp3", "wma", "wav");
filechooser.setFileFilter(filter);
if (filechooser.showOpenDialog(GInterface.this)==JFileChooser.APPROVE_OPTION)
controller.addAudio(
new Audio( filechooser.getSelectedFile().getName(),
filechooser.getSelectedFile().getPath()));
}
}
GInterface(Controller ctr){
super();
controller = ctr;
start = new JButton("再生");
start.addActionListener(new StartListener());
stop = new JButton("停止");
stop.addActionListener(new StopListener());
prev = new JButton("前へ");
prev.addActionListener(new PrevListener());
next = new JButton("次へ");
next.addActionListener(new NextListener());
adda = new JButton("曲を追加");
adda.addActionListener(new AddListener());
Container c = this.getContentPane();
c.setLayout(new GridLayout(1,5));
c.add(prev);
c.add(start);
c.add(stop);
c.add(next);
c.add(adda);
this.setSize(500, 100);
}
}
/* 音楽プレイヤー */
class MusicPlayer {
private ArrayList<Audio> playlist;
private Controller my_controller;
public GInterface my_interface;
MusicPlayer(){
playlist = new ArrayList<Audio>();
my_controller = new Controller(playlist);
my_interface = new GInterface(my_controller);
my_interface.setVisible(true);
}
}