mardi 19 janvier 2016

Design Pattern for executing requests on multiple output levels

I am parsing a string like:

\img(demo.jpg,20px)


~> \method(arg, arg, ...)

into:

a requestComposite data structure that persists of 1 to n arguments, which are requestLeaf objects, which arguments again persists of the same composite class.

requestComposite Object
(
    [arguments:protected] => Array
        (
            [0] => requestLeaf Object
                (
                    [methodName:protected] => img
                    [arguments:protected] => Array
                        (
                            [0] => requestComposite Object
                                (
                                    [arguments:protected] => Array
                                        (
                                            [0] => requestLeaf Object
                                                (
                                                    [methodName:protected] => text
                                                    [arguments:protected] => demo.jpg
                                                )
                                        )
                                )
                            [1] => requestComposite Object
                                (
                                    [arguments:protected] => Array
                                        (
                                            [0] => requestLeaf Object
                                                (
                                                    [methodName:protected] => text
                                                    [arguments:protected] => 20px
                                                )
                                        )
                                )
                        )
                )
        )
)

What I ultimately want to achieve is to execute the "initital requestComposite Object" in a way so that it renders some kind of document.

Let's call this "Level 0". So every requestLeaf Object on Level 0 should print itself to the document. I want to use an external Library like TCPDF (for pdf obv.) to do this.

Everything > level 0 should be computed in my own Application.

There may be a \read(x) token which would lookup a member value X, there may be a \list() token which would loop through a specified list and would execute every other token within it: like \list(\read(x)))

x could be another y ~> \list(\read(\read(y)))

in other words: I cannot know how deep the structure will be in advance.

The computed value (END Level) should be returned as a String (text). (every \text() on level 0 should be printed to the document)

Happens that I'm fairly new to Design Patterns and OO in particular. I am fiddling around with the Chain of Responsibility Pattern right now, but I'm somewhat curious about if CoR really fullfills my needs.

I think what I want to achieve is to "climb" into the Stack, compute a value at the last level and return it to the front of the Stack, so that I can print it out.

Is this possible with the CoR? Couldn't quite figure it out yet on my own. I also thought of a combination of Mediator & Visitor pattern.

How would you solve or even call a problem like this?

Aucun commentaire:

Enregistrer un commentaire