compositeパターンとは? わかりやすく解説

Weblio 辞書 > 辞書・百科事典 > 百科事典 > compositeパターンの意味・解説 

Composite パターン

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2020/10/02 08:57 UTC 版)

ナビゲーションに移動 検索に移動

Composite パターン(コンポジット・パターン)とは、GoF (Gang of Four; 4人のギャングたち) によって定義された デザインパターンの1つである。「構造に関するパターン」に属する。Composite パターンを用いるとディレクトリファイルなどのような、木構造を伴う再帰的データ構造を表すことができる。

Composite パターンにおいて登場するオブジェクトは、「枝」と「葉」であり、これらは共通のインターフェースを実装している。そのため、枝と葉を同様に扱えるというメリットがある。

クラス図

Composite パターンのクラス図を以下に挙げる。

利用例

Composite パターンを用いてディレクトリ構造を表すJavaプログラムの例を示す。 このプログラムは、

  • 枝を表すクラス : Folderクラス
  • 葉を表すクラス : Fileクラス
  • 共通インタフェース : FileInterfaceインタフェース
  • 実行例を示すためのクラス : DirectoryUserクラス

から構成される。

 import java.util.ArrayList;
 import java.util.List;
 
 interface FileInterface {
 	public void defaultMethod(int depth);
 	public List<FileInterface> getChildren();
 	public boolean addComponent(FileInterface c);
 	public boolean removeComponent(FileInterface c);
 }
 
 
 class File implements FileInterface {
 
 	private String name;
 
 	public File(String name) {
 		this.name = name;
 	}
 
 	public void defaultMethod(int depth) {
 		for (int i = 0; i < depth; i++) System.out.print("  ");
 		System.out.println("file:" + this.name);
 	}
 
 	public List<FileInterface> getChildren() { return null; }
 	public boolean addComponent(FileInterface c) { return false; }
 	public boolean removeComponent(FileInterface c) { return false; }
 
 }
  
 
 class Folder implements FileInterface {
 	private String name;
 	private List<FileInterface> fileList = new ArrayList<FileInterface>();
 	public Folder(String name) { this.name = name; }
 
 	public void defaultMethod(int depth) {
 		for (int i = 0; i < depth; i++)
 			System.out.print("  ");
 		System.out.println("folder:" + name);
 		for (FileInterface file : fileList) {
  			file.defaultMethod(depth + 1);
 		}
 	}
 
 	public List<FileInterface> getChildren() { return this.fileList; }
 	public boolean addComponent(FileInterface c) { return this.fileList.add(c); }
 	public boolean removeComponent(FileInterface c) { return this.fileList.remove(c); }
 }
 
 
 public class DirectoryUser {
 	public static void main(String [] args){
 		FileInterface root = new Folder("root");
 		FileInterface usr = new Folder("usr");
 		FileInterface var = new Folder("var");
 		FileInterface home = new Folder("home");
 		FileInterface user1 = new Folder("user1");
  		FileInterface file1 = new File("file1");
 
 		root.addComponent(usr);
 		usr.addComponent(var);
 		root.addComponent(home); 
 		home.addComponent(user1);
 		user1.addComponent(file1);
 		root.defaultMethod(0);
 
 	}
 }

実行結果

folder:root
  folder:usr
    folder:var
  folder:home
    folder:user1
      file:file1

注意事項

自分自身を参照している例
循環している例

Composite パターンを用いる際には、データ構造がきちんと木構造を保つようにしなければならない。 親子関係が循環してしまった場合、Component#operation()を実行した際に無限ループに陥るからである。

例えば、利用例のソースコードを書き換えた以下のプログラムは、処理が

dir1.defaultMethod(0);

の行に達した時に、無限に出力し続けてしまう。

 public class LoopExample {
 	public static void main(String [] args){
 		FileInterface dir1 = new Folder("dir1");
 		FileInterface dir2 = new Folder("dir2");
 		FileInterface dir3 = new Folder("dir3");
 		
 		dir1.addComponent(dir2);
 		dir2.addComponent(dir3);
 		dir3.addComponent(dir1); 
 		dir1.defaultMethod(0);
 	}
 }

関係するパターン

Interpreter パターン
文法表現が Composite パターンとなる場合が多い。

関連項目


「Composite パターン」の例文・使い方・用例・文例

Weblio日本語例文用例辞書はプログラムで機械的に例文を生成しているため、不適切な項目が含まれていることもあります。ご了承くださいませ。


英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「compositeパターン」の関連用語

compositeパターンのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



compositeパターンのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
ウィキペディアウィキペディア
All text is available under the terms of the GNU Free Documentation License.
この記事は、ウィキペディアのComposite パターン (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。 Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。
Tanaka Corpusのコンテンツは、特に明示されている場合を除いて、次のライセンスに従います:
 Creative Commons Attribution (CC-BY) 2.0 France.
この対訳データはCreative Commons Attribution 3.0 Unportedでライセンスされています。
浜島書店 Catch a Wave
Copyright © 1995-2025 Hamajima Shoten, Publishers. All rights reserved.
株式会社ベネッセコーポレーション株式会社ベネッセコーポレーション
Copyright © Benesse Holdings, Inc. All rights reserved.
研究社研究社
Copyright (c) 1995-2025 Kenkyusha Co., Ltd. All rights reserved.
日本語WordNet日本語WordNet
日本語ワードネット1.1版 (C) 情報通信研究機構, 2009-2010 License All rights reserved.
WordNet 3.0 Copyright 2006 by Princeton University. All rights reserved. License
日外アソシエーツ株式会社日外アソシエーツ株式会社
Copyright (C) 1994- Nichigai Associates, Inc., All rights reserved.
「斎藤和英大辞典」斎藤秀三郎著、日外アソシエーツ辞書編集部編
EDRDGEDRDG
This page uses the JMdict dictionary files. These files are the property of the Electronic Dictionary Research and Development Group, and are used in conformance with the Group's licence.

©2025 GRAS Group, Inc.RSS