Τετάρτη, 15 Ιουλίου 2020

Composite Pattern (OOP)

The composite pattern works for structures (trees) where we have two kinds, the leaf and the composite. The composite perform an operation to all childs, If child is a leaf preform an operation for leaf, if it is a composite perform an operation for all child on that composite.




\\ Composite Pattern


Class iComponet {
      module operation {
            error "Abstract"
      }
}
Class Componet as iComponet {
Private:
      name$
Public:
      module operation {
            print "do something"
      }
      remove {
            Print "remove ";.name$
      }
}
Class Leaf as Componet {
      module operation {
            Print "do an operation to this leaf:";.name$
      }
Class:
      module Leaf (.name$) {
      }
}
Class Composite as Componet {
      m=stack
      module operation {
       k=each(.m)
       Print "["+.name$+"]"
       while k
             z=stackitem(k)
             z=>operation
       end while
      }
      module addChild (child as *Componet) {
            stack .m {data child}
      }
Class:
      module Composite (.name$) {
      }
}
M->Composite("GroupA")
M=>addChild pointer(Leaf("Shape1"))
M=>addChild pointer(Leaf("Shape2"))
M=>addChild pointer(Leaf("Shape3"))
M1->Composite("Graphic")
M1=>addChild M
M->Composite("GroupB")
M=>addChild pointer(Leaf("Shape4"))
M1=>addChild M
M->0&
M1=>operation

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου