kevin hendrickx
kevin hendrickx

Reputation: 116

Law of demeter -- need for internal representation?

Imagine we have a class "NeuralNetwork"

NeuralNetwork has a method train() that changes its internal representation. But this means having access to the internals of the Layer objects. It needs access to the individual neurons, breaking the law of demeter. for example

layers[0].getNeuron(0).compute(input)

layer[0].getNeuron(0).changeBias(2)

The only solution I can think of is to provide extra methods in "Layer" and delegate it to the neurons. This would also allow me to use different implementations of a Layer interface. One which is more flexible and one that has better performance.

But this seems cumbersome. Isn't there a better way to model this?

enter image description here

Upvotes: 2

Views: 269

Answers (1)

kevin hendrickx
kevin hendrickx

Reputation: 116

The first possible solution is to just add some methods that delegate.

A second solution, extracted from Amr Mostafa's comments would be to send an event through the Layer object to the neurons.

Both solutions would allow us to use a different Layer object which consists out of multidimensional arrays instead of neuron objects (performance consideration).

enter image description here

Upvotes: 1

Related Questions