簡體   English   中英

Pharo:如何比較不同檢查員的對象?

[英]Pharo: How to compare objects in different inspectors?

我正在使用Pharo 4.假設我的對象已經實現了很好的相等和哈希方法。

如何直觀地比較不同檢查員中的兩個或N個物體? 在視覺上我的意思是並排比較,我可以很容易地看出差異。

我嘗試了所有實例,但一段時間后變得乏味。

通過查看在GLMBasicExamples open時可以選擇的diff瀏覽器,您可能會受到啟發。 使用Glamour,您可以輕松創建自定義瀏覽器來幫助您。 diff示例本身是15行代碼:

| browser | 
browser := GLMTabulator new.
browser 
    row: [:r | r column: #one; column: #two];
    row: #diff.
browser transmit to: #one; andShow: [ :a |
    a list 
        display: #first ].
browser transmit to: #two; andShow: [ :a | 
    a list
        display: #second ].
browser transmit to: #diff; from: #one; from: #two; andShow: [ :a | 
    a diff
        display: [ :one :two | {one asString . two asString}] ].
browser openOn: #(#(abc def ghi) #(abc xyz))

魅力的差異視圖使用DiffMorph來顯示差異。 它需要兩個對象的字符串表示來進行比較

我建議創建一個為您要比較的實例定制的DifferenceFinder類。 例如,假設您想要比較點。 然后你會得到PointDifferenceFinder類, PointDifferenceFinder有三個實例變量p, q, difference和一行協議

compare: aPoint with: anotherPoint
  p := aPoint.
  q := anotherPoint.
  self compareClass ifFalse: [^self].
  self compareX ifFalse: [^self].
  self compareY ifFalse: [^self].

哪里

compareClass
  ^p class == q class
    ifFalse: [difference := 'not of the same class'];
    yourself

compareX
  ^p x = q x
    ifFalse: [difference := 'not with the same x'];
    yourself

compareY
  ^p y = q y
    ifFalse: [difference := 'not with the same y'];
    yourself

當然, Point的情況很簡單,但這應該給你和想法。 根據您的需要,您可以只使用一個簡單的查找器,或者更復雜的一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM