Created
July 23, 2014 14:23
-
-
Save tonyseek/d1ad36cdc7e35a4420b7 to your computer and use it in GitHub Desktop.
"Mixin" is a kind of composite pattern implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.ArrayList; | |
interface Entity { | |
public int getId(); | |
public int getKind(); | |
} | |
interface Taggable { | |
public void addTag(int tagId); | |
public List<Integer> getTags(); | |
} | |
class TaggableComponent implements Taggable { | |
private Entity target; | |
public TaggableComponent(Entity target) { | |
this.target = target; | |
} | |
public void addTag(int tagId) { | |
int id = target.getId(); | |
int kind = target.getKind(); | |
System.out.println("insert into ... values " | |
+ id + ", " | |
+ kind + ", " | |
+ tagId + ")"); | |
} | |
public ArrayList<Integer> getTags() { | |
// query from database | |
return new ArrayList<Integer>(); | |
} | |
} | |
class Post implements Entity, Taggable { | |
public final static int KIND = 1001; | |
private TaggableComponent taggable; | |
private int id; | |
private String title; | |
public Post(int id, String title) { | |
this.id = id; | |
this.title = title; | |
this.taggable = new TaggableComponent(this); | |
} | |
public int getId() { | |
return id; | |
} | |
public int getKind() { | |
return KIND; | |
} | |
public void addTag(int tagId) { | |
taggable.addTag(tagId); | |
} | |
public ArrayList<Integer> getTags() { | |
return taggable.getTags(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
_connection = None | |
class Storage(object): | |
"""The database gateway.""" | |
def __init__(self, location=':memory:'): | |
self.location = location | |
self._connection = None | |
def iter_ddl(self): | |
yield ('CREATE TABLE target_tagged' | |
' (target_id int, target_kind int, tag_id int,' | |
' creation_time timestamp)') | |
@property | |
def connection(self): | |
if self._connection is None: | |
self._connection = sqlite3.connect(self.location) | |
for sql in self.iter_ddl(): | |
self._connection.execute(sql) | |
return self._connection | |
def execute(self, *args, **kwargs): | |
return self.connection.execute(*args, **kwargs) | |
def commit(self): | |
return self.connection.commit() | |
storage = Storage() | |
class TagMixin(object): | |
"""The taggable features.""" | |
def add_tag(self, tag_id): | |
sql = ('insert into target_tagged' | |
' (target_id, target_kind, tag_id, creation_time) ' | |
'values (?, ?, ?, CURRENT_TIMESTAMP)') | |
params = (self.ident, self.kind, tag_id) | |
storage.execute(sql, params) | |
storage.commit() | |
def get_tags(self): | |
sql = ('select tag_id, creation_time from target_tagged ' | |
'where target_id = ? and target_kind = ?') | |
params = (self.ident, self.kind) | |
cursor = storage.execute(sql, params) | |
return cursor.fetchall() | |
class Post(TagMixin): | |
"""The post entity.""" | |
kind = 1001 | |
def __init__(self, ident, title): | |
self.ident = ident | |
self.title = title | |
def __repr__(self): | |
return 'Post(%r, %r)' % (self.ident, self.title) | |
if __name__ == '__main__': | |
post = Post(1000001, 'hello, world') | |
print('%r created' % post) | |
print('...') | |
post.add_tag(10) | |
post.add_tag(11) | |
print('Tag \t Time') | |
print('=== \t ====') | |
for tag_id, creation_time in post.get_tags(): | |
print('%d \t %s' % (tag_id, creation_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment