項目 | 内容 |
---|---|
名前 | パターンの内容を的確に想起させる用語 |
状況 | 機能要求を満たすソフトウェアを開発すべき状況 |
問題 | 非機能要求を満たしたいという問題 |
解決 | ソフトウェア開発の方針と解決策 |
結果 | 作成されたソフトウェアにおいて、機能 / 非機能要求が満たされた状況 |
理由 | 方針と解決策が機能 / 非機能要求の満足に有効な理由 |
目的 | 工程 | パターンの種類 | パターン・カタログ | |
---|---|---|---|---|
汎用型 | 特化型 | |||
ソフトウェア開発 | ドメイン分析 | 分析パターン | 「もの−こと−もの」、 データモデル・パターン、 モデル・パターン・カタログ | データモデル・パターン、 アナリシス・パターン |
要求分析 | ||||
システム分析 | ||||
アーキテクチャー設計 | アーキテクチャー・パターン | アーキテクチャー・スタイル、 POSA | J2EE パターン、 PofEAA、 エンタープライズ統合パターン | |
プログラム・クラス設計 | デザイン・パターン | GoF デザイン・パターン、 POSA | J2EE パターン、 PofEAA、 マルチ・スレッド・パターン集 | |
実装 | イディオム | - | Smalltalk ベストプラクティス、 C++ プログラミング定石、 Java スタイル、 POSA | |
テスト | テスト・パターン | オブジェクト指向テスト・パターン | Java テスティング・パターン | |
保守・改訂 | リファクタリングなど | リエンジニアリング・パターン、 リファクタリング | - | |
プロジェクト管理 | 組織構造最適化 | 組織パターン | 開発工程の生成的パターン・ランゲージ | - |
プロセス改善 | プロセス・パターン | 開発工程の生成的パターン・ランゲージ | フレームワーク進化パターン |
生成 | より大きな構造を作るためのクラスとオブジェクトの組み立て方を示すパターン。 |
---|---|
構造 | オブジェクトの生成 / インスタンス化を抽象化するためのパターン。 これにより、共通のインターフェースを実装していれば、インスタンス化された目的のオブジェクトに確実に変更を加えることができます。 |
振る舞い | オブジェクト間のアルゴリズムと役割分担を扱うパターン。 |
目的 | ||||
---|---|---|---|---|
生成 | 構造 | 振る舞い | ||
範囲 | クラス | オブジェクト生成の一部をサブクラスに委ねる。 | クラスを構成するために継承を利用する。 | アルゴリズムや制御の流れを記述するために継承を利用する。 |
オブジェクト | オブジェクトの生成の一部を他のオブジェクトに委ねる | オブジェクトをまとめる方法を記述する | ひとつのオブジェクトのみでは遂行できないタスクを実行するため、オブジェクトのグループがどのように協調するかを記述する | |
(*) クラス
(**) オブジェクト
図 パターン間の関連
- package com.maruno.composite.sample;
- public class CompositeMain {
- public static void main(String[] args) {
- try {
- System.out.println("資格 SJC-P");
- Certifying sjcp = new Certifying("SJC-P");
- sjcp.add(new Subject("SJC-P 035", 24000));
- sjcp.printList();
- System.out.println("");
- System.out.println("資格 SJC-WC");
- Certifying sjcwc = new Certifying("SJC-WC");
- sjcwc.add(sjcp);
- sjcwc.add(new Subject("SJC-WC 081", 24000));
- sjcwc.printList();
- System.out.println("");
- System.out.println("資格 Oracle Silver");
- Certifying oraclesilver = new Certifying("Oracle Silver");
- oraclesilver.add(new Subject("SQL 入門", 15000));
- oraclesilver.add(new Subject("Oracle 入門", 15000));
- oraclesilver.printList();
- System.out.println("");
- System.out.println("資格 Oracle Gold");
- Certifying oraclegold = new Certifying("Oracle Gold");
- oraclegold.add(oraclesilver);
- oraclegold.add(new Subject("DBA", 15000));
- oraclegold.add(new Subject("PL/SQL", 15000));
- oraclegold.printList();
- } catch (RuntimeException e) {
- e.printStackTrace();
- }
- }
- }
- package com.maruno.composite.sample;
- public abstract class Entry {
- public abstract String getName(); // 名前を得る
- public abstract int getExamPrice(); // 受験価格を得る
- public Entry add(Entry entry) // エントリを追加する
- throws RuntimeException {
- throw new RuntimeException();
- }
- public void printList() { // 一覧を表示する
- printList("");
- }
- protected abstract void printList(String prefix); // prefixを前につけて一覧を表示する
- public String toString() { // 文字列表現
- return getName() + " (" + getExamPrice() + ")";
- }
- }
- package com.maruno.composite.sample;
- import java.util.*;
- public class Certifying extends Entry {
- private String name; // 資格の名前
- private List certifying = new ArrayList(); // 資格エントリの集合
- public Certifying(String name) { // コンストラクター
- this.name = name;
- }
- public String getName() { // 名前を得る
- return name;
- }
- public int getExamPrice() { // サイズを得る
- int examPrice = 0;
- Iterator it = certifying.iterator();
- while (it.hasNext()) {
- Entry entry = (Entry) it.next();
- examPrice += entry.getExamPrice();
- }
- return examPrice;
- }
- public Entry add(Entry entry) { // エントリの追加
- certifying.add(entry);
- return this;
- }
- protected void printList(String prefix) { // エントリの一覧
- System.out.println(prefix + " - [" + this + "]");
- Iterator it = certifying.iterator();
- while (it.hasNext()) {
- Entry entry = (Entry) it.next();
- entry.printList(prefix + " - [" + name + "]");
- }
- }
- }
- package com.maruno.composite.sample;
- public class Subject extends Entry {
- private String name;
- private int examPrice;
- public Subject(String name, int examPrice) {
- this.name = name;
- this.examPrice = examPrice;
- }
- public String getName() {
- return name;
- }
- public int getExamPrice() {
- return examPrice;
- }
- protected void printList(String prefix) {
- System.out.println(prefix + " - \"" + this + "\"");
- }
- }
□ 実行結果
$ java -cp . com.maruno.composite.sample.CompositeMain 資格 SJC-P - [SJC-P (24000)] - [SJC-P] - "SJC-P 035 (24000)" 資格 SJC-WC - [SJC-WC (48000)] - [SJC-WC] - [SJC-P (24000)] - [SJC-WC] - [SJC-P] - "SJC-P 035 (24000)" - [SJC-WC] - "SJC-WC 081 (24000)" 資格 Oracle Silver - [Oracle Silver (30000)] - [Oracle Silver] - "SQL 入門 (15000)" - [Oracle Silver] - "Oracle 入門 (15000)" 資格 Oracle Gold - [Oracle Gold (60000)] - [Oracle Gold] - [Oracle Silver (30000)] - [Oracle Gold] - [Oracle Silver] - "SQL 入門 (15000)" - [Oracle Gold] - [Oracle Silver] - "Oracle 入門 (15000)" - [Oracle Gold] - "DBA (15000)" - [Oracle Gold] - "PL/SQL (15000)"Top
□ Iterator (注: コメントなど、必要ない部分は省略しています)
- /*
- * @(#)Iterator.java 1.16 01/12/03
- *
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package java.util;
- public interface Iterator {
- boolean hasNext();
- Object next();
- void remove();
- }
□ AbstractList (注: コメントなど、必要ない部分は省略しています)
- /*
- // * @(#)AbstractList.java 1.35 01/12/03
- *
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
- package java.util;
- public abstract class AbstractList extends AbstractCollection implements List {
- public Iterator iterator() {
- return new Itr();
- }
- private class Itr implements Iterator {
- int cursor = 0;
- int lastRet = -1;
- int expectedModCount = modCount;
- public boolean hasNext() {
- return cursor != size();
- }
- public Object next() {
- try {
- Object next = get(cursor);
- checkForComodification();
- lastRet = cursor++;
- return next;
- } catch (IndexOutOfBoundsException e) {
- checkForComodification();
- throw new NoSuchElementException();
- }
- }
- public void remove() {
- if (lastRet == -1)
- throw new IllegalStateException();
- checkForComodification();
- try {
- AbstractList.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
- lastRet = -1;
- expectedModCount = modCount;
- } catch (IndexOutOfBoundsException e) {
- throw new ConcurrentModificationException();
- }
- }
- final void checkForComodification() {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- }
- }
- protected transient int modCount = 0;
- }
Collection c; ... Iterator itr = c.iterator(); while (itr.hasNext()) { itr.next(); }
- import java.util.*;
- public class Set012 {
- public static void main(String[] args) {
- Set ts = new TreeSet();
- for (int i = 0; i < args.length; i++) {
- ts.add(args[i]);
- }
- Iterator itr = ts.iterator(); // イテレーターの宣言と生成
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
□ 実行結果
$ java Set012 Hello Java World Hello Java WorldTop
Top
- import java.awt.*;
- import java.awt.event.*;
- // Command パターン
- // デザイン・パターンを使わないソース
- public class Main01 extends Frame {
- Menu mnuFile;
- MenuItem mnuOpen, mnuExit;
- Button btnRed;
- Panel p;
- Frame fr;
- public Main01() {
- super("Frame without commands");
- fr = this; //save copy of this frame
- MenuBar mbar = new MenuBar();
- setMenuBar(mbar);
- mnuFile = new Menu("File", true);
- mbar.add(mnuFile);
- mnuOpen = new MenuItem("Open...");
- mnuFile.add(mnuOpen);
- mnuExit = new MenuItem("Exit");
- mnuFile.add(mnuExit);
- mnuOpen.addActionListener(new FileOpen());
- mnuExit.addActionListener(new FileExit());
- btnRed = new Button("Red");
- p = new Panel();
- add(p);
- p.add(btnRed);
- btnRed.addActionListener(new BtnRed());
- setBounds(100, 100, 200, 100);
- setVisible(true);
- }
- private void exitClicked() {
- System.exit(0);
- }
- public static void main(String[] args) {
- new Main01();
- }
- // inner classes
- class FileOpen implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- FileDialog fDlg =
- new FileDialog(fr, "Open a file", FileDialog.LOAD);
- fDlg.show();
- }
- }
- class BtnRed implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- p.setBackground(Color.red);
- }
- }
- class FileExit implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- }
- }
□ Command.java
- import java.awt.*;
- import java.awt.event.*;
- //Command パターン
- //デザイン・パターンを使うソース
- public class Main02 extends Frame implements ActionListener {
- Menu mnuFile;
- FileOpenCommand mnuOpen;
- FileExitCommand mnuExit;
- RedButtonCommand btnRed;
- Panel p;
- public Main02() {
- super("Frame with external commands");
- MenuBar mbar = new MenuBar();
- setMenuBar(mbar);
- mnuFile = new Menu("File", true);
- mbar.add(mnuFile);
- mnuOpen = new FileOpenCommand("Open...", this);
- mnuFile.add(mnuOpen);
- mnuExit = new FileExitCommand("Exit");
- mnuFile.add(mnuExit);
- mnuOpen.addActionListener(this);
- mnuExit.addActionListener(this);
- p = new Panel();
- add(p);
- btnRed = new RedButtonCommand("Red", p);
- p.add(btnRed);
- btnRed.addActionListener(this);
- setBounds(100, 100, 200, 100);
- setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- Command obj = (Command) e.getSource();
- obj.execute();
- }
- public static void main(String[] args) {
- new Main02();
- }
- }
□ FileExitCommand.java
- interface Command {
- void execute();
- }
□ FileOpenCommand.java
- import java.awt.*;
- public class FileExitCommand extends MenuItem implements Command {
- public FileExitCommand(String caption) {
- super(caption);
- }
- public void execute() {
- System.exit(0);
- }
- }
□ RedButtonCommand.java
- import java.awt.*;
- public class FileOpenCommand extends MenuItem implements Command {
- Frame frame;
- public FileOpenCommand(String caption, Frame frame) {
- super(caption);
- this.frame = frame;
- }
- public void execute() {
- new FileDialog(frame, "Open file").show();
- }
- }
Top
- import java.awt.*;
- public class RedButtonCommand extends Button implements Command {
- Panel p;
- public RedButtonCommand(String caption, Panel p) {
- super(caption);
- this.p = p;
- }
- public void execute() {
- p.setBackground(Color.RED);
- }
- }
□ Command.java
- import java.awt.*;
- import java.awt.event.*;
- //Command パターン
- public class Main22
- extends Frame
- implements ActionListener, MouseMotionListener, WindowListener {
- // 描画履歴
- private MacroCommand history = new MacroCommand();
- // 描画領域
- private DrawCanvas canvas = new DrawCanvas(400, 400, history);
- // 消去ボタン
- private Button clearButton = new Button("clear");
- // コンストラクタ
- public Main22(String title) {
- super(title);
- this.addWindowListener(this);
- canvas.addMouseMotionListener(this);
- clearButton.addActionListener(this);
- Panel buttonPanel = new Panel();
- buttonPanel.add(clearButton);
- Panel mainPanel = new Panel();
- mainPanel.add(buttonPanel);
- mainPanel.add(canvas);
- this.add(buttonPanel, BorderLayout.NORTH);
- this.add(mainPanel, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- // ActionListener用
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == clearButton) {
- history.clear();
- canvas.repaint();
- }
- }
- // MouseMotionListener用
- public void mouseMoved(MouseEvent e) {
- }
- public void mouseDragged(MouseEvent e) {
- Command cmd = new DrawCommand(canvas, e.getPoint());
- history.append(cmd);
- cmd.execute();
- }
- // WindowListener用
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- public void windowActivated(WindowEvent e) {
- }
- public void windowClosed(WindowEvent e) {
- }
- public void windowDeactivated(WindowEvent e) {
- }
- public void windowDeiconified(WindowEvent e) {
- }
- public void windowIconified(WindowEvent e) {
- }
- public void windowOpened(WindowEvent e) {
- }
- public static void main(String[] args) {
- new Main22("Command Pattern Sample");
- }
- }
□ MacroCommand.java
- interface Command {
- void execute();
- }
□ Drawable.java
- import java.util.Stack;
- import java.util.Iterator;
- public class MacroCommand implements Command {
- // 命令の集合
- private Stack commands = new Stack();
- // 実行
- public void execute() {
- Iterator it = commands.iterator();
- while (it.hasNext()) {
- ((Command)it.next()).execute();
- }
- }
- // 追加
- public void append(Command cmd) {
- if (cmd != this) {
- commands.push(cmd);
- }
- }
- // 最後の命令を削除
- public void undo() {
- if (!commands.empty()) {
- commands.pop();
- }
- }
- // 全部削除
- public void clear() {
- commands.clear();
- }
- }
□ DrawCommand.java
- interface Drawable {
- void draw(int x, int y);
- }
□ DrawCanvas.java
- import java.awt.Point;
- public class DrawCommand implements Command {
- // 描画対象
- protected Drawable drawable;
- // 描画位置
- private Point position;
- // コンストラクタ
- public DrawCommand(Drawable drawable, Point position) {
- this.drawable = drawable;
- this.position = position;
- }
- // 実行
- public void execute() {
- drawable.draw(position.x, position.y);
- }
- }
Top
- import java.awt.*;
- public class DrawCanvas extends Canvas implements Drawable {
- // 描画色
- private Color color = Color.red;
- // 描画する点の半径
- private int radius = 6;
- // 履歴
- private MacroCommand history;
- // コンストラクタ
- public DrawCanvas(int width, int height, MacroCommand history) {
- setSize(width, height);
- setBackground(Color.white);
- this.history = history;
- }
- // 履歴全体を再描画
- public void paint(Graphics g) {
- history.execute();
- }
- // 描画
- public void draw(int x, int y) {
- Graphics g = getGraphics();
- g.setColor(color);
- g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
- }
- }
Main23.java | 色変更ボタンを追加 |
Drawable.java | 描画色を変更するメソッドsetColor()を追加 |
DrawCanvas.java | 描画色を変更するメソッドsetColor()を追加 |
ColorCommand.java | 描画色を設定するコマンドのクラスを新規追加 |
□ Drawable.java
- import java.awt.*;
- import java.awt.event.*;
- //Command パターン
- public class Main23
- extends Frame
- implements ActionListener, MouseMotionListener, WindowListener {
- // 描画履歴
- private MacroCommand history = new MacroCommand();
- // 描画領域
- private DrawCanvas canvas = new DrawCanvas(400, 400, history);
- // 消去ボタン
- private Button clearButton = new Button("clear");
- // 赤ボタン
- private Button redButton = new Button("red");
- // 緑ボタン
- private Button greenButton = new Button("green");
- // 青ボタン
- private Button blueButton = new Button("blue");
- // コンストラクタ
- public Main23(String title) {
- super(title);
- this.addWindowListener(this);
- canvas.addMouseMotionListener(this);
- clearButton.addActionListener(this);
- redButton.addActionListener(this);
- greenButton.addActionListener(this);
- blueButton.addActionListener(this);
- Panel buttonPanel = new Panel();
- buttonPanel.add(clearButton);
- buttonPanel.add(redButton);
- buttonPanel.add(greenButton);
- buttonPanel.add(blueButton);
- Panel mainPanel = new Panel();
- mainPanel.add(buttonPanel);
- mainPanel.add(canvas);
- this.add(buttonPanel, BorderLayout.NORTH);
- this.add(mainPanel, BorderLayout.SOUTH);
- pack();
- setVisible(true);
- }
- // ActionListener用
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == clearButton) {
- history.clear();
- canvas.init();
- canvas.repaint();
- } else if (e.getSource() == redButton) {
- Command cmd = new ColorCommand(canvas, Color.red);
- history.append(cmd);
- cmd.execute();
- } else if (e.getSource() == greenButton) {
- Command cmd = new ColorCommand(canvas, Color.green);
- history.append(cmd);
- cmd.execute();
- } else if (e.getSource() == blueButton) {
- Command cmd = new ColorCommand(canvas, Color.blue);
- history.append(cmd);
- cmd.execute();
- }
- }
- // MouseMotionListener用
- public void mouseMoved(MouseEvent e) {
- }
- public void mouseDragged(MouseEvent e) {
- Command cmd = new DrawCommand(canvas, e.getPoint());
- history.append(cmd);
- cmd.execute();
- }
- // WindowListener用
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- public void windowActivated(WindowEvent e) {
- }
- public void windowClosed(WindowEvent e) {
- }
- public void windowDeactivated(WindowEvent e) {
- }
- public void windowDeiconified(WindowEvent e) {
- }
- public void windowIconified(WindowEvent e) {
- }
- public void windowOpened(WindowEvent e) {
- }
- public static void main(String[] args) {
- new Main23("Command Pattern Sample");
- }
- }
□ DrawCanvas.java
- import java.awt.Color;
- interface Drawable {
- void draw(int x, int y);
- // ここにコードを書いてください。
- }
□ ColorCommand.java
- import java.awt.*;
- public class DrawCanvas extends Canvas implements Drawable {
- // 描画色
- private Color color;
- // 描画する点の半径
- private int radius;
- // 履歴
- private MacroCommand history;
- // コンストラクタ
- public DrawCanvas(int width, int height, MacroCommand history) {
- setSize(width, height);
- setBackground(Color.white);
- this.history = history;
- init();
- }
- // 履歴全体を再描画
- public void paint(Graphics g) {
- history.execute();
- }
- // 初期化
- public void init() {
- color = Color.red;
- radius = 6;
- }
- // 描画
- public void draw(int x, int y) {
- Graphics g = getGraphics();
- g.setColor(color);
- g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
- }
- public void setColor(Color color) {
- // ここにコードを書いてください。
- }
- }
- import java.awt.Color;
- public class ColorCommand implements Command {
- // 描画対象
- // ここにコードを書いてください。
- // 描画色
- private Color color;
- // コンストラクタ
- public ColorCommand(Drawable drawable, Color color) {
- // ここにコードを書いてください。
- }
- // 実行
- public void execute() {
- // ここにコードを書いてください。
- }
- }