Tag Archives: design pattern

Composite Design Pattern

Composite Design Pattern

Its a structural design pattern.

A file system is a tree structure that contains Branches which are Folders as well as Leaf nodes which are Files. A folder object usually contains one or more file or folder objects and thus is a complex object where a file is a simple object. Since files and folders have many operations and attributes in common, such as moving and copying a file or a folder, listing file or folder attributes such as file name and size, it would be easier and more convenient to treat both file and folder objects uniformly by defining a File System Resource Interface.

  • The intent of this pattern is to compose objects into tree structures to represent hierarchies.
  • Composite lets clients treat individual objects and compositions of objects uniformly.
  • Objects must be composed recursively, and there should be no distinction between individual and composed elements, and objects in the structure can be treated uniformly

Applicability:

File System implementations –

// Define a “lowest common denominator”
interface AbstractFile
{
public void ls();
}

// File implements the “lowest common denominator”
class File implements AbstractFile
{
public File(String name)
{
m_name = name;
}
public void ls()
{
System.out.println(CompositeDemo.g_indent + m_name);
}
private String m_name;
}

// Directory implements the “lowest common denominator”
class Directory implements AbstractFile
{
public Directory(String name)
{
m_name = name;
}
public void add(Object obj)
{
m_files.add(obj);
}
public void ls()
{
System.out.println(CompositeDemo.g_indent + m_name);
CompositeDemo.g_indent.append(” “);
for (int i = 0; i < m_files.size(); ++i)
{
// Leverage the “lowest common denominator”
AbstractFile obj = (AbstractFile)m_files.get(i);
obj.ls();
}
CompositeDemo.g_indent.setLength(CompositeDemo.g_indent.length() 3);
}
private String m_name;
private ArrayList m_files = new ArrayList();
}

publicclass CompositeDemo
{
public static StringBuffer g_indent = new StringBuffer();

public static void main(String[] args)
{
Directory one = new Directory(“dir111”), two = new Directory(“dir222”),
thr = new Directory(“dir333”);
File a = new File(“a”), b = new File(“b”), c = new File(“c”), d = new
File(“d”), e = new File(“e”);
one.add(a);
one.add(two);
one.add(b);
two.add(c);
two.add(d);
two.add(thr);
thr.add(e);
one.ls();
}
}

Applying different patterns to file system problem:

  • Composite for modelling directories/files
  • Decorator for allowing to associate additional properties (and possibly behaviour) to file system nodes.
  • Iterator for traversing the file system in different ways
  • Factory for creating file system nodes with different backends (e.g. a disk-based file system or a memory-based file system or a remote file system).

Graphics Drawing Editor:

In graphics editors a shape can be basic or complex. An example of a simple shape is a line, where a complex shape is a rectangle which is made of four line objects. Since shapes have many operations in common such as rendering the shape to screen,  composite pattern can be used to enable the program to deal with all shapes uniformly.

Related Posts:

Factory Design Pattern

Observer Design Pattern

Singleton