SlideServe
Menu
Préférences
© SlideServe · v2026

Group4 Composite Pattern Overview

smaureen avatar
19 diapositives · 224 Views · Dec 20, 2019

Learn about the Composite Pattern, a structural design pattern that allows grouping objects into tree structures. Ideal for managing complex part-whole hierarchies. Understand its motivation, structure, and implementation with examples in Java.

Group4 Composite Pattern Overview - slide 1
1 / 19

Télécharger la présentation

Group4 Composite Pattern Overview
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Presented by Group4 Composite Pattern

  2. What is Composite Pattern • Structural Pattern • Allows a group of objects to be treated as single instance of object • Intend to compose objects into tree structures to represent part-whole hierarchies

  3. Motivation • Complexity in tree-structured data • Interface to treat complex objects uniformly • Design to “has-a” relationship

  4. When to use • Supposed you are a commander • You’re giving command “fire” to your soldiers • If you has 3 soldiers, that’s fine, right?

  5. When to use (Contd.) • What if you have 10 soldiers? Not going fine…

  6. When to use (Contd.) • What if you have 100 or 1,000 soldiers? • I bet you’ll be shot before giving your command to all of them…

  7. When to use (Contd.) • How to make task simplier? • Assign an officer to give command to their soldiers • Now you have to command only 1-3 officers

  8. Structure

  9. Structure (Contd.) • Component • is the abstraction for all components • declares the interface for objects in the composition • implements default behavior for the interface • declares an interface for accessing and managing its child components • (optional) defines an interface for accessing a components's parent in the recursive structure, and implements it if that's appropriate

  10. Structure (Contd.) • Leaf • represents leaf objects in the composition • implements all Component methods

  11. Structure (Contd.) • Composite • represents a composite Component (component having children) • implements methods to manipulate children • implements all Component methods, generally by delegating them to its children

  12. Examples import java.util.ArrayList; interface Commander { //Fire command public void fire(); }

  13. Examples (Contd.) class Officer implements Commander { //Collection of soldiers. private ArrayList<Commander> soldiers = new ArrayList<Commander>(); //Group fire command public void fire() { for (Commander soldier : soldiers) { soldier.fire(); } }

  14. Examples (Contd.) //Adds the soldier to officer's control. public void add(Commander soldier) { soldiers.add(soldier); } //Removes the soldier from officer's control. public void remove(Commander soldier) { soldiers.remove(soldier); } }

  15. Examples (Contd.) class Soldier implements Commander { //Fire command. public void fire() { System.out.println("Fire!"); } }

  16. Examples (Contd.) public class Command { public static void main(String[] args) { //Initialize four soldiers Soldier soldier1 = new Soldier(); Soldier soldier2 = new Soldier(); Soldier soldier3 = new Soldier(); Soldier soldier4 = new Soldier();

  17. Examples (Contd.) //Initialize three officers Officer officer = new Officer(); Officer officer1 = new Officer(); Officer officer2 = new Officer(); //Composes the officers officer1.add(soldier1); officer1.add(soldier2); officer1.add(soldier3); officer2.add(soldier4);

  18. Examples (Contd.) officer.add(officer1); officer.add(officer2); //Prints the complete fire command (four times the string "Fire!"). officer.fire(); } }

  19. Questions?