Design Pattern: Composite

Định nghĩa: the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to “compose” objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.[1]

<pre>
public class Directory {
   private Directory directory;
   private File[] files;

   public Directory(File[] files) {
      this.files = files;
   }

   public Directory(Directory directory, File[] files) {
      this.directory = directory;
      this.files = files;
   }
   boolean isDirectory() {
      return files != null;
   }
   boolean isFile() {
      return files ==null;
   }

   public Directory getDirectory() {
      return directory;
   }

   public void setDirectory(Directory directory) {
      this.directory = directory;
   }

   public File[] getFiles() {
      return files;
   }

   public void setFiles(File[] files) {
      this.files = files;
   }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s