Scala model-view-presenter, traits

前端 未结 1 1429
情话喂你
情话喂你 2021-01-25 08:18

I am a fan of Martin Fowler\'s (deprecated) model-view-presenter pattern. I am writing a Scala view class containing several button classes. I would like to include methods to s

1条回答
  •  执笔经年
    2021-01-25 08:28

    Scala is less about explicit setter and getter methods than java. Instead use abstract fields to define the exposed interface. How about something like this:

    trait ActionUser {
      def setAction(action:Action):Unit
    }
    
    trait Container {
      val aButton:ActionUser
    }
    
    trait ContainerImpl {
      val aButton = new JButton with ActionUser
    }
    

    Classes operating against Container will only be able to access setAction while internal methods see it as a JButton.

    One more note: C uses macros to cut down on code duplication. Scala uses multi-inheritance of traits to accomplish the same thing.

    0 讨论(0)
提交回复
热议问题