But when you make this concept your guiding principal, you tend to write code which enforces strict client behavior. Invariably, you end up writing components that are almost unusable with changing requirements. When you get hell bent on using the component, either
1. you modify existing code breaking the very first principal of object oriented programming - entities must be closed to modification but open to extension. As one guy in my team puts it, your SVN revision must only have 'A's and almost zero 'M's.
2. or write infinitely complex super-generic code which is a nightmare to maintain and is uber-hard to understand
Here's a concrete example. Assume you need to maintain a list of animals. Your clients hold another animal. They need to check if the animal is dangerous or not. Religiously following the concept of encapsulation, you might come up with -
Clearly, this models the present but not the future. Another implementation could be -
public enum Animal {
TIGER (Category.DANGEROUS);
LEOPARD (Category.DANGEROUS);
DOG (Category.FUN);
CAT (Category.DISGUSTING);
GODZILLA (Category.OH_MY_GOD);
private Category category;
public Animal (Category category) {
this.category = category;
}
// I will not allow people to see what category assignments
// I've made. It might offend them!
private Category getCategory() {
return this.category;
}
public static boolean isAnimalOfCategory(Category category) {
for (Animal a : EnumSet.allOf(Animal) {
if (a.getCategory().equals(category)) return true;
}
return false;
}
}
I might also write a fancy method which given a Category would return a List of all animals I have for that category, but I'll pass. The second method enforces a logical grouping of all animals in the sample space which is also one of the objectives of the entity. The second class breaks encapsulation but lets you answer the following easily-
public enum Animal {
TIGER (Category.DANGEROUS);
LEOPARD (Category.DANGEROUS);
DOG (Category.FUN);
CAT (Category.DISGUSTING);
GODZILLA (Category.OH_MY_GOD);
private Category category;
public Animal (Category category) {
this.category = category;
}
// But I'll let them know what all I have for a
// particular type and let them do whatever they want with it.
public static List<Animal> getAllDangerousOnes() {
return Arrays.asList(Animal.TIGER, Animal.LEOPARD);
}
}
1. Is animal of category X?
2. Give me all animals of category Y.
3. Give me all animals but not of category Z.
A quirky tidbit but I it liked so much that I wanted to put it up here. Leave a comment if you think there are better solutions.
Khuda Hafiz,
Sultan of Samarkand

2 comments:
This post should have been tagged with assholery, WTF and rants. Write something, somebody wants to read!!!
AV,無碼,a片免費看,自拍貼圖,伊莉,微風論壇,成人聊天室,成人電影,成人文學,成人貼圖區,成人網站,一葉情貼圖片區,色情漫畫,言情小說,情色論壇,臺灣情色網,色情影片,色情,成人影城,080視訊聊天室,a片,A漫,h漫,麗的色遊戲,同志色教館,AV女優,SEX,咆哮小老鼠,85cc免費影片,正妹牆,ut聊天室,豆豆聊天室,聊天室,情色小說,aio,成人,微風成人,做愛,成人貼圖,18成人,嘟嘟成人網,aio交友愛情館,情色文學,色情小說,色情網站,情色,A片下載,嘟嘟情人色網,成人影片,成人圖片,成人文章,成人小說,成人漫畫,視訊聊天室,性愛,a片,AV女優,聊天室,情色
Post a Comment