Class hierarchy problem in this social network model

Posted by Gerenuk on Programmers See other posts from Programmers or by Gerenuk
Published on 2012-11-08T17:54:28Z Indexed on 2012/11/08 23:16 UTC
Read the original article Hit count: 266

I'm trying to design a class system for a social network data model - basically a link/object system. Now I have roughly the following structure (simplified and only relevant methods shown)

class Data:
    "used to handle the data with mongodb"
    "can link, unlink data and also return other linked data"
    "is basically a proxy object that only stores _id and accesses mongodb on requests"
    "it looks like {_id: ..., _out: [id1, id2,...], _inc: [id3, id4, ...]}"
    def get_node(self, id)
        "create a new Data object from the underlying mongodb"
        "each data object can potentially create a reference object to new mongo data"
        "this is needed when the data returns the linked objects"

class Node:
    """
    this class proxies linking calls to .data
    it includes additional network logic operations whereas Data only contains a basic
    database solution
    """
    def __init__(self, data):
        "the infrastructure realization is stored as composition by an included object data"
        "Node bascially proxies most calls to the infrastructure object data"
    def get_node(self, data):
        "creates a new object of class Object or Link depending on data"

class Object(Node):
    "can have multiple connections to Link"

class Link(Node):
    "has one 'in' and one 'out' connection to an Object"

This system is working, however maybe wouldn't work outside Python. Note that after reading links

Now I have two questions here:

1) I want to infrastructure of the data storage to be replacable. Earlier I had Data as a superclass of Node so that it provided the neccessary calls. But (without dirty Python tricks) you cannot replace the superclass dynamically. Is using composition therefore recommended? The drawback is that I have to proxy most calls (link, unlink etc). Any thoughts?

2) The class Node contains the common method .get_node which is used to built new Object or Link instances after reading out the data. Some attribute of data decided whether the object which is only stored by id should be instantiated as an Object or Link class. The problem here is that Node needs to know about Object and Link in advance, which seems dodgy. Do you see a different solution? Both Object and Link need to instantiate one of all possible types depending on what the find in their linked data.

Are there any other ideas how to implement a flexible Object/Link structure where the underlying database storage is isolated?

© Programmers or respective owner

Related posts about design

Related posts about inheritance