Last active
September 25, 2017 21:49
-
-
Save ericmaxwell2003/6b579240901bb2c0c919476bc39e2a22 to your computer and use it in GitHub Desktop.
Using composition over inheritance with Realm
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
interface Animal { | |
String getColor(); | |
void setColor(String color); | |
int getLegCount(); | |
void setLegCount(int legCount); | |
} |
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
@RealmClass | |
public class AnimalEntity implements RealmModel, Animal { | |
private String color; | |
private int legCount; | |
@Override | |
public String getColor() { | |
return null; | |
} | |
@Override | |
public void setColor(String color) { | |
} | |
@Override | |
public int getLegCount() { | |
return 0; | |
} | |
@Override | |
public void setLegCount(int legCount) { | |
} | |
} |
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
interface Cat extends Animal { | |
boolean isDeclawed(); | |
void setDeclawed(boolean declawed); | |
} |
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
@RealmClass | |
public class CatEntity implements RealmModel, Cat { | |
private boolean isDeclawed; | |
private AnimalEntity commonAttributes; | |
// In real app, need to check for null here when referencing commonAttributes. | |
@Override | |
public String getColor() { return commonAttributes.getColor(); } | |
@Override | |
public void setColor(String color) { commonAttributes.setColor(color); } | |
@Override | |
public int getLegCount() { return commonAttributes.getLegCount(); } | |
@Override | |
public void setLegCount(int legCount) { commonAttributes.setLegCount(legCount); } | |
@Override | |
public boolean isDeclawed() { return isDeclawed; } | |
@Override | |
public void setDeclawed(boolean declawed) { this.isDeclawed = declawed; } | |
} |
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
interface Duck extends Animal { | |
String getBillType(); | |
void setBillType(String billType); | |
} |
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
@RealmClass | |
public class DuckEntity implements RealmModel, Duck { | |
private String billType; | |
private AnimalEntity commonAttributes; | |
// In real app, need to check for null here when referencing commonAttributes. | |
@Override | |
public String getColor() { return commonAttributes.getColor(); } | |
@Override | |
public void setColor(String color) { commonAttributes.setColor(color); } | |
@Override | |
public int getLegCount() { return commonAttributes.getLegCount(); } | |
@Override | |
public void setLegCount(int legCount) { commonAttributes.setLegCount(legCount); } | |
@Override | |
public String getBillType() { return billType;} | |
@Override | |
public void setBillType(String billType) { this.billType = billType; } | |
} |
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
List<? extends Duck> ducks = db.where(DuckEntity.class).findAll(); | |
for(Duck duck : ducks) { | |
System.out.println("A duck has " + duck.getLegCount() + " legs, and this one has a " + duck.getBillType() + " type bill"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment