#format python """CompositePattern in DesignPatterns Example """ import unittest class Pipe: def __init__(self,aNumber=None): self._number=aNumber def putIn(self,aNumber): raise NotImplementedError class CompositePipe(Pipe): def setChildren(self,*aPipes): self._childrenPipes=aPipes class SequencePipe(CompositePipe): def putIn(self,aNumber): v=aNumber for eachPipe in self._childrenPipes: v=eachPipe.putIn(v) return v class ParallelPipe(CompositePipe): def putIn(self,aNumber): pass class AddPipe(Pipe): def putIn(self,aNumber): return aNumber+self._number class MultiplyPipe(Pipe): def putIn(self,aNumber): return aNumber*self._number class TestPipe(unittest.TestCase): def test1(self): addOne=AddPipe(1) self.assertEquals(0+1,addOne.putIn(0)) double=MultiplyPipe(2) self.assertEquals(1*2,double.putIn(1)) addOneAndDouble=SequencePipe() addOneAndDouble.setChildren(addOne,double) self.assertEquals((2+1)*2,addOneAndDouble.putIn(2)) addOneAndTwoDouble=SequencePipe() addOneAndTwoDouble.setChildren(addOneAndDouble,double) self.assertEquals((2+1)*2*2,addOneAndTwoDouble.putIn(2)) if __name__=='__main__': unittest.main(argv=('','-v'))