MI is not evil - it is just a different concept...
bearophile
bearophileHUGS at lycos.com
Fri Dec 26 12:18:38 PST 2008
The User:
This is the wikipedia page you refer to:
http://en.wikipedia.org/wiki/Policy-based_design
The signal in that C++ code is almost lost in syntactic noise.
This is a D version, I hope the original meaning isn't lost:
import std.stdio: writefln;
struct HelloLanguagePolicyEnglish {
static string message() {
return "Hello, World!";
}
}
struct HelloLanguagePolicyGerman {
static string message() {
return "Hallo Welt!";
}
}
struct HelloOutputPolicy {
static void print(T)(T message) {
writefln(message);
}
}
struct Hello(OutputPolicy, LanguagePolicy) {
// behaviour method
static public void run() {
// two policy methods
OutputPolicy.print(LanguagePolicy.message());
}
}
void main() {
// prints Hello World!
Hello!(HelloOutputPolicy, HelloLanguagePolicyEnglish).run;
// does the same but uses another policy, the language has changed
// prints Hallo Welt!
Hello!(HelloOutputPolicy, HelloLanguagePolicyGerman).run;
}
An alternative D version:
import std.stdio: writefln;
template HelloLanguagePolicyEnglish() {
string message() {
return "Hello, World!";
}
}
template HelloLanguagePolicyGerman() {
string message() {
return "Hallo Welt!";
}
}
template HelloOutputPolicy() {
void print(T)(T message) {
writefln(message);
}
}
struct Hello(alias OutputPolicy, alias LanguagePolicy) {
// behaviour method
static void run() {
// two policy methods
OutputPolicy!().print(LanguagePolicy!().message());
}
}
void main() {
// prints Hello World!
Hello!(HelloOutputPolicy, HelloLanguagePolicyEnglish).run;
// does the same but uses another policy, the language has changed
// prints Hallo Welt!
Hello!(HelloOutputPolicy, HelloLanguagePolicyGerman).run;
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list