monica
monica

Reputation: 1065

How to abstract the data and control to support easy extensibility?

I have a case: There is SomeNode, which is composed by different basic parts: say some types of A,B,C. There is also a transformation function that will transform an instance of SomeNode to another SomeNode.

However, there can be some other parts added to SomeNode, in addition to A,B,C, so say there might be D as the fourth part of the SomeNode. and So, the transformation function's interface might also need to change accordingly for the newly added component SomeNode, but there might be some same logic shared.

Then I have been wondering, what's a good design to abstract SomeNode and its transformation function for easy extensibility? Using trait? how? Some inspiration examples? Thanks,

Upvotes: 1

Views: 91

Answers (3)

Josh Campbell
Josh Campbell

Reputation: 458

I'm hesitant to write this at the expense of showing my noob identity, but based on keywords used in the question, it sounds like 'OOP fundamentals' are what is being asked for.

"How to abstract the data and control to support easy extensibility?" (Generalization)

"There is SomeNode, which is composed by different basic parts:" (Aggregation)

"There is also a transformation function that will transform an instance of SomeNode to another SomeNode." (Polymorphism)

"However, there can be some other parts added to SomeNode, in addition to A,B,C, ... function's interface might also need to change accordingly for the newly added component SomeNode, but there might be some same logic shared. " (Interface)

"Then I have been wondering, what's a good design to abstract SomeNode and its transformation function for easy extensibility?" (Inheritance)

Based on my interpretation of the question, I would recommend researching S.O.L.I.D. design principles http://www.youtube.com/watch?NR=1&v=05jVWgKZ6MY&feature=endscreen

Upvotes: 1

Eugene Yokota
Eugene Yokota

Reputation: 95654

For managing modularity, check out Cake pattern (aka Bakery of Doom pattern). There's been some interesting articles on the topic:

For managing transformations and traits, Scala compiler is the best example I can think of.

Upvotes: 2

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340923

Maybe I'm missing something, but aren't you looking for inheritance?

class SomeNode(val a: Int, val b: String, val c: Char)

class SpecialNode(from: SomeNode, val d: List[Int]) extends 
         SomeNode(from.a, from.b, from.c)

SpecialNode shares fields of SomeNode and adds new d field. The transformation looks something like this (of course it can be more complex:

def transform(node: SomeNode) = new SpecialNode(node, Nil)

Upvotes: 1

Related Questions