Created
April 4, 2011 23:38
-
-
Save vsavkin/902719 to your computer and use it in GitHub Desktop.
A small chunk of code showing how to use mixins to write code in DCI style
This file contains 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
//Syntax Sugar------------------------------------------- | |
Object.metaClass.addRole = {role-> | |
delegate.metaClass.mixin role | |
} | |
//Model classes and the database------------------------- | |
class User { | |
int id | |
String name | |
int age | |
List followers | |
} | |
class Company { | |
int id | |
String name | |
String country | |
List followers | |
} | |
class Database { | |
private users = [ | |
1: new User(id: 1, name: 'Johh', age: 25, followers: []), | |
2: new User(id: 2, name: 'Piter', age: 25, followers: []) | |
] | |
private companies = [ | |
1: new Company(id: 1, name: 'BigCompany', country: 'Canada', followers: []) | |
] | |
User findUserById(int id){ | |
users[id] | |
} | |
Company findCompanyById(int id){ | |
companies[id] | |
} | |
void updateUser(User user){ | |
users[user.id] = user | |
} | |
void updateCompany(Company company){ | |
companies[company.id] = company | |
} | |
} | |
//Roles-------------------------------------------------- | |
class Follower { | |
} | |
class Following { | |
void addFollower(follower){ | |
followers << follower | |
} | |
} | |
//Context------------------------------------------------ | |
class FollowersListContext { | |
Database db | |
void addFollowerToUser(int followingUserId, int followerUserId){ | |
def following = db.findUserById(followingUserId) | |
def follower = db.findUserById(followerUserId) | |
following.addRole Following | |
follower.addRole Follower | |
following.addFollower follower | |
db.updateUser following | |
} | |
void addFollowerToCompany(int followingCompanyId, int followerUserId){ | |
def following = db.findCompanyById(followingCompanyId) | |
def follower = db.findUserById(followerUserId) | |
following.addRole Following | |
follower.addRole Follower | |
following.addFollower follower | |
db.updateCompany following | |
} | |
} | |
//Performing a use case----------------------------------- | |
Database db = new Database() | |
assert db.findUserById(1).followers.isEmpty() | |
assert db.findUserById(2).followers.isEmpty() | |
def context = new FollowersListContext(db: db) | |
context.addFollowerToUser(1, 2) | |
assert db.findUserById(1).followers?.first() == db.findUserById(2) | |
assert db.findUserById(2).followers.isEmpty() | |
//Checking mixins------------------------------------------ | |
def userA = new User(id: 1) | |
userA.addRole Following | |
def userB = new User(id: 1) | |
assert userA.metaClass.respondsTo(userA, 'addFollower') | |
assert !userB.metaClass.respondsTo(userB, 'addFollower') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment