Juan-Carlos Gandhi (
juan_gandhi) wrote2007-05-25 05:26 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
new trick
I wonder how obvious or how stupid or how ubiquitous is the following trick:
Say, I have an enum somewhere outside of my realm:
and my method takes an instance of that enum, and I really do not like switching based on the enum; of course I can have an
(PURPLE STUFF ADDED LATER)
Is not it a poetry? :)
Say, I have an enum somewhere outside of my realm:
enum DataType {
PERSONAL, COMMUNITY, PRISON, ARMY;
};
....
and my method takes an instance of that enum, and I really do not like switching based on the enum; of course I can have an
EnumMap
, but the funny trick is that my strategies are based on this enum type, so I can do just this:
enum Strategy {
PERSONAL {
public void process(Entity entity) {...};
},
COMMUNITY {
public void process(Entity entity) {...};
},
PRISON {
public void process(Entity entity) {...};
},
ARMY {
public void process(Entity entity) {...};
}
abstract public void process(Entity entity);
DEFAULT{
public void process(Entity entity) {...};
};
abstract public void process(Entity entity);
(and so on, add functionality here )
Strategy forDataType(DataType type) {
Strategy candidate = valueOf(type.name());
return candidate == null ? DEFAULT : candidate;
}
};
....
Strategy.forDataType(myType).process(myEntity);
(PURPLE STUFF ADDED LATER)
Is not it a poetry? :)
no subject
no subject
no subject
no subject
no subject
no subject
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
no subject
return (Strategy) Enum.Parse(typeof(Strategy), type.ToString());
no subject
no subject
no subject
no subject
no subject
(no subject)
(no subject)
(no subject)
(no subject)
no subject
enum DataType { PERSONAL, COMMUNITY, PRISON, ARMY };
enum Strategy { PERSONAL, COMMUNITY, PRISON, ARMY };
Strategy ForDataType(DataType type)
{
return (Strategy)type;
}
I know it's a dirty hack but it works.
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
no subject
no subject
no subject
no subject
no subject
no subject
(no subject)
(no subject)
(no subject)
(no subject)
no subject
Recently I enjoyed learning about java.lang.reflect.Proxy that allows one to construct classes on the fly.
no subject
no subject
I can imagine a fully static class construction path, either via bytecode assembler or by full-blown java source generation and complilation, and then loading the class file. I could imagine Guice to work this way :)
But to quote project homepage, "Guice cures tight coupling," "Guice is the anti-static." This all sounds pretty to my dynamic-languages-inclined ear :)
no subject
no subject
На дадаизм похоже, да. :-)
Проблема в том, что если кто-то outside of your realm добавит новое значение в enum DataType, то статическими методами этого обнаружить нельзя, и результатом будет исключение в процессе исполнения.
"Uncaught java.lang.InvalidCast exception in class mil.NORAD.ControlLoop, commencing emergency launch."
no subject
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)
(no subject)