Skip to content

Instantly share code, notes, and snippets.

@peteruhnak
Last active September 20, 2017 17:28
Show Gist options
  • Save peteruhnak/8adce9166c16599e17cf9582bda61305 to your computer and use it in GitHub Desktop.
Save peteruhnak/8adce9166c16599e17cf9582bda61305 to your computer and use it in GitHub Desktop.
Slot vs accessor methods for subscribing/unsubscribing

Subscription Slot vs Accessor

Base (subscription to the model in some manner)

BaseController>>subscribeTo: aModel
  aModel announcer when: ModelRenamed do: [ :ann | self logCr: self class name, ': ' , aModel name ]

BaseController>>unsubscribeFrom: aModel
  aModel announcer unsubscribe: self

Option 1 – Accessor-based, never assign model anywhere else.

BaseController subclass: #AccessorController
	slots: { #model }

AccessorController>>model: aModel
  model ifNotNil: [ self unsubscribeFrom: model ].
  model := aModel.
  model ifNotNil: [ self subscribeTo: model ]

Option 2 – Slot-based, model can be assigned anywhere

BaseController subclass: #SlotController
	slots: { #model => SubscribedSlot }

SlotController>>model: aModel
  model := aModel

InstanceVariableSlot subclass: #SubscribedSlot

SubscribedSlot>>write: aNewModel to: aController
	(self read: aController) ifNotNil: [ :m | aController unsubscribeFrom: m ].
	super write: aNewModel to: aController.
	(self read: aController) ifNotNil: [ :m | aController subscribeTo: m ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment