#$Id: s.composite.xotcl 1.4 01/03/23 22:24:35+01:00 neumann@somewhere.wu-wien.ac.at $
Class Composite -superclass Class
Composite metadata add description
@ @File {
  description {
  Simple composite pattern meta-class taken from the paper 
  'Filters as a Language Support for Design Patterns in
  Object-Oriented Scripting Languages'. 
  }
}

Composite instproc addOperations args {
    [self] instvar operations
    foreach op $args {
	if {![info exists operations($op)]} {
	    set operations($op) $op
	}
    }
} 

Composite instproc removeOperations args {
    [self] instvar operations
    foreach op $args {
	if {![info exists operations($op)]} {
	    unset operations($op)
	}
    }
}

Composite instproc compositeFilter args {
  # get the operations class variable from the object's class
  [self regclass] instvar operations
  # get the request
  set r [self calledproc]

  # check if the request is a registered operation 
  if {[info exists operations($r)]} {
    foreach object [[self] info children] {
      # forward request
      eval [self]::$object $r $args
    }
  }
  return [next]    
}


Composite instproc init {args} {
  [self] array set operations {}
  next
  [self] filterappend Composite::compositeFilter 
}


