0

我解析像字符串請求:設計模式,用於執行對不同的「輸出電平」

\read(customer) hello world 


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

成到Ñ參數,這些參數是其仍然存在一個複合數據結構 requestLeaf對象,其參數可以是requestLeaf對象(如果它只是純文本,應該像上面那樣返回(「hello world」))或另一個requestComposite對象(如果有一些計算正在進行(如read-> customer),直到它再次爲純文本。

requestComposite Object 
(
    [requests:protected] => Array 
     (
      [0] => commandRequest Object 
       (
        [methodName:protected] => read 
        [arguments:protected] => Array 
         (
         [0] => commandRequest Object 
          (
          [methodName:protected] => text 
          [arguments:protected] => customer 
         ) 
        ) 
      ) 
      [1] => commandRequest Object 
       (
        [methodName:protected] => text 
        [arguments:protected] => hello world 
      ) 
    ) 
) 

我想實現的是循環遍歷整個複合體,以便呈現某種文檔。

的參數或葉子的非常第一複合是表示用於打印到該文檔的或級別0。
我想爲此使用一個庫。這個庫可以處理打印文本,圖像等,但無法評估任何東西!

全部>等級0應計算在另一個庫。
該庫可以讀取成員值,執行一些數學運算等,並將其最終值(僅限於字符串)返回至要打印的
其他所有內容均通過例如\ IMG()或\文本()

可以有

\read(x) 

令牌以查找一個成員值X,
偉馳值應打印隱含的,(不包成另一個\文本())!

可以有\列表()令牌環它的參數通過諸如指定的成員列表:

\list(\read(x))) 

X可以是另一種ý

\list(\read(\read(y))). 

換句話說:我不知道結構會有多深。

發生在設計模式和麪向對象方面的新特性。
我與鏈責任的擺弄周圍模式爲「執行」,現在,這裏的渲染/輸出庫和計算庫正在建設的處理程序鏈。
但我有點好奇,如果CoR真的滿足我的需求:

你會如何解決這樣的問題?

編輯:我的做法至於現在

  1. 通過複合材料進行迭代。
    沿中保對象
    含有輸出庫計算庫傳遞。

  2. 如果當前葉是文本
    檢查,如果當前是根
    如果NO 訪問計算庫評估的實際值,並將其傳遞到人可能關注(如\閱讀() \ varput(),...)
    如果是訪問輸出庫將其打印出來

什麼我想到的是,我要實現每requestMethod在兩個庫中實現自動根打印。即
\ read()輸出應將文本打印到文檔中,
\ read()在計算庫中應查​​找成員值。

我在這裏過於複雜嗎?

+0

什麼是你想這樣做,不能做只有簡單的舊的https://en.wikipedia.org/wiki/Composite_pattern?也許你只需要先在樹上跑一下呢? –

+0

1.我的問題可能不夠明確。我更新了問題描述。 2.對我來說如此令人困惑和不清楚的是: 如何確定不同結構級別的正確處理程序(如果root〜>打印爲pdf,如果不是〜>將計算值返回給root,那麼它可以是打印 – lance

+0

這就是遞歸的方法,方法接受一個輸入,確定下一個位是否有效,如果是,則返回一些值,如果否,則將處理後的位從輸入中剝離,並將已刪除的輸入傳遞給自己 – dbugger

回答

0

從意見繼續進行,並假設您已經構建了合成樹從你的輸入,你需要什麼,不能做這樣的:

import java.util.ArrayList; 
import java.util.List; 
interface Component { 
    void add(Component component); 
    List<Component> children(); 
} 
class Composite implements Component { 
    @Override public void add(Component component) { 
     children.add(component); 
    } 
    @Override public List<Component> children() { 
     return children; 
    } 
    List<Component> children=new ArrayList<>(); 
} 
class Leaf implements Component { 
    @Override public void add(Component component) { 
     throw new UnsupportedOperationException(); 
    } 
    @Override public List<Component> children() { 
     return null; 
    } 
} 
public class So34886186 { 
    static String indent(int n) { 
     String s=""; 
     for(int i=0;i<n;i++) 
      s+=" "; 
     return s; 
    } 
    static void print(Component root,Component component,int indent) { 
     if(component instanceof Leaf) 
      if(component.equals(root)) 
       System.out.println(indent(indent)+" root: leaf: "+component); 
      else System.out.println(indent(indent)+"not root: leaf: "+component); 
     else { 
      if(component.equals(root)) { 
       System.out.println(indent(indent)+" root: composite: "+component+": ("); 
       for(Component component2:((Composite)component).children) 
        print(root,component2,indent+4); 
       System.out.println(indent(indent)+")"); 
      } else { 
       System.out.println(indent(indent)+"not root: composite: "+component+": ("); 
       for(Component component2:((Composite)component).children) 
        print(root,component2,indent+4); 
       System.out.println(indent(indent)+")"); 
      } 
     } 
    } 
    public static void main(String[] args) { 
     Component root=new Composite(); 
     root.add(new Leaf()); 
     Component one=new Composite(); 
     root.add(one); 
     one.add(new Leaf()); 
     Component two=new Composite(); 
     root.add(two); 
     print(root,root,0); 
    } 
} 
相關問題