fbpx
วิกิพีเดีย

คอมโพสิตแพตเทิร์น

คอมโพสิตแพตเทิร์น (Composite pattern) เป็นดีไซน์แพตเทิร์น ที่จัดโครงสร้างของอ็อบเจกต์ที่มีลักษณะการทำงานคล้ายกันและเป็นส่วนประกอบย่อยๆ ของระบบใหญ่ให้อยู่ในรูปแบบโครงสร้างต้นไม้ คลาสผู้ใช้สามารถปฏิบัติต่อส่วนต่างๆ ของคอมโพสิตในแบบเดียวกันโดยไม่ต้องคำนึงว่าเป็นส่วนปลายหรือส่วนกลางของแผนผังต้นไม้ ตัวอย่างเช่นโครงสร้างของบทความหนึ่งๆ แบ่งเป็นหลายๆ ย่อหน้า แต่ละย่อหน้ายังแบ่งย่อยต่อไปได้เป็นประโยค คำ และตัวอักษรในที่สุด

การนำไปใช้งาน

องค์ประกอบของคอมโพสิตแพตเทิร์นมีสามส่วนคือ

  • อินเตอร์เฟสหรือคลาสแบบแอ็บสแตรคเป็นแม่แบบที่นิยามพฤติกรรมที่แต่ละอ็อบเจกต์ของคอมโพสิตต้องมี
  • คลาสที่สามารถมีองค์ประกอบอื่นของคอมโพสิตได้
  • คลาสที่เป็นส่วนปลาย ไม่สามารถมีองค์ประกอบอื่นได้
 
UML คลาสไดอะแกรมของคอมโพสิตแพตเทิร์น

ตัวอย่างโปรแกรม

ภาษาจาวา

ตัวอย่างโปรแกรมเพื่อแสดงโครงสร้างของ File System โดยแบ่งเป็นแฟ้มและเอกสารที่อยู่ภายในแฟ้ม และการแสดงรายชื่อของแฟ้มและเอกสารภายใน

อินเตอร์เฟส File เป็นแม่แบบของส่วนประกอบต่างๆ ของคอมโพสิต นิยามเมธอด list() เพื่อให้แต่ละองค์ประกอบพิมพ์ชื่อของตัวเองและองค์ประกอบย่อยที่อยู่ภายใต้

public interface File { public void list(); public void addFile(File file); public void removeFile(File file); } 

คลาส Folder สามารถมีอ็อบเจกต์ชนิด File อื่นๆ อยู่ภายใต้ได้ คือมีได้ทั้ง Folder ย่อยและ Document เมธอด list() จะพิมพ์ชื่อของตนเองและตามด้วยการเรียกเมธอด list() ของทุก File ที่อยู่ภายใต้ Folder

เมธอด addFile() และ removeFile() ทำหน้าที่จัดการกับ File ย่อยๆ ภายใต้ Folder

public class Folder implements File { private String name; private ArrayList<File> files = new ArrayList<File>(); public Folder(String name) { this.name = name; } public void addFile(File file) { files.add(file); } public void removeFile(File file) { files.remove(file); } public void list() { System.out.println("Folder: " + name); for (File file : files) {  file.list(); } } } 

คลาส Document เป็นส่วนปลายสุดหรือส่วนย่อยที่สุดของคอมโพสิต ไม่สามารถมีส่วนอื่นๆ ภายใต้ ดังนั้นเมธอด list() จึงมีหน้าที่เพียงพิมพ์ชื่อของตนเองและเมธอด addFile() และ removeFile() ไม่ต้องทำอะไร

public class Document implements File { private String name; public Document(String name) { this.name = name; } public void list() { System.out.println("Document: " + name); } public void addFile(File file) { // do nothing } public void removeFile(File file) { // do nothing } } 

การเรียกใช้งาน

File root = new Folder("root"); root.addFile(new Document("one.doc")); File sub = new Folder("sub"); sub.addFile(new Document("two.doc")); sub.addFile(new Document("three.doc")); root.addFile(sub); root.list(); 

อ้างอิง

  • Design Patterns: Elements of Reusable Object-Oriented Software (ISBN 0-201-63361-2) โดย Erich Gamma, Richard Helm, Ralph Johnson และ John Vlissides (Gang of four: GoF)

แหล่งข้อมูลอื่น

คอมโพส, ตแพตเท, composite, pattern, เป, นด, ไซน, แพตเท, ดโครงสร, างของอ, อบเจกต, กษณะการทำงานคล, ายก, นและเป, นส, วนประกอบย, อยๆ, ของระบบใหญ, ให, อย, ในร, ปแบบโครงสร, างต, นไม, คลาสผ, ใช, สามารถปฏ, อส, วนต, างๆ, ของคอมโพส, ตในแบบเด, ยวก, นโดยไม, องคำน, งว, าเป. khxmophsitaephtethirn Composite pattern epndiisnaephtethirn thicdokhrngsrangkhxngxxbecktthimilksnakarthangankhlayknaelaepnswnprakxbyxy khxngrabbihyihxyuinrupaebbokhrngsrangtnim khlasphuichsamarthptibtitxswntang khxngkhxmophsitinaebbediywknodyimtxngkhanungwaepnswnplayhruxswnklangkhxngaephnphngtnim twxyangechnokhrngsrangkhxngbthkhwamhnung aebngepnhlay yxhna aetlayxhnayngaebngyxytxipidepnpraoykh kha aelatwxksrinthisud enuxha 1 karnaipichngan 2 twxyangopraekrm 2 1 phasacawa 3 xangxing 4 aehlngkhxmulxunkarnaipichngan aekikhxngkhprakxbkhxngkhxmophsitaephtethirnmisamswnkhux xinetxrefshruxkhlasaebbaexbsaetrkhepnaemaebbthiniyamphvtikrrmthiaetlaxxbecktkhxngkhxmophsittxngmi khlasthisamarthmixngkhprakxbxunkhxngkhxmophsitid khlasthiepnswnplay imsamarthmixngkhprakxbxunid UML khlasidxaaekrmkhxngkhxmophsitaephtethirntwxyangopraekrm aekikhphasacawa aekikh twxyangopraekrmephuxaesdngokhrngsrangkhxng File System odyaebngepnaefmaelaexksarthixyuphayinaefm aelakaraesdngraychuxkhxngaefmaelaexksarphayinxinetxrefs File epnaemaebbkhxngswnprakxbtang khxngkhxmophsit niyamemthxd list ephuxihaetlaxngkhprakxbphimphchuxkhxngtwexngaelaxngkhprakxbyxythixyuphayit public interface File public void list public void addFile File file public void removeFile File file khlas Folder samarthmixxbecktchnid File xun xyuphayitid khuxmiidthng Folder yxyaela Document emthxd list caphimphchuxkhxngtnexngaelatamdwykareriykemthxd list khxngthuk File thixyuphayit Folderemthxd addFile aela removeFile thahnathicdkarkb File yxy phayit Folder public class Folder implements File private String name private ArrayList lt File gt files new ArrayList lt File gt public Folder String name this name name public void addFile File file files add file public void removeFile File file files remove file public void list System out println Folder name for File file files file list khlas Document epnswnplaysudhruxswnyxythisudkhxngkhxmophsit imsamarthmiswnxun phayit dngnnemthxd list cungmihnathiephiyngphimphchuxkhxngtnexngaelaemthxd addFile aela removeFile imtxngthaxair public class Document implements File private String name public Document String name this name name public void list System out println Document name public void addFile File file do nothing public void removeFile File file do nothing kareriykichngan File root new Folder root root addFile new Document one doc File sub new Folder sub sub addFile new Document two doc sub addFile new Document three doc root addFile sub root list xangxing aekikhDesign Patterns Elements of Reusable Object Oriented Software ISBN 0 201 63361 2 ody Erich Gamma Richard Helm Ralph Johnson aela John Vlissides Gang of four GoF aehlngkhxmulxun aekikhComposite ody Vince Huston xngkvs A look at the Composite design pattern Archived 2013 10 31 thi ewyaebkaemchchin ody David Geary xngkvs ekhathungcak https th wikipedia org w index php title khxmophsitaephtethirn amp oldid 9561859, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

, อ่าน, ดาวน์โหลด, ฟรี, ดาวน์โหลดฟรี, mp3, วิดีโอ, mp4, 3gp, jpg, jpeg, gif, png, รูปภาพ, เพลง, เพลง, หนัง, หนังสือ, เกม, เกม