| |
34. 4. 2. 适配器变化模式。 |
|
class Programming {
public void code() {
}
public void debug() {
}
}
interface ModernProgramming {
void ide();
}
class SurrogateAdapter implements ModernProgramming {
Programming programming;
public SurrogateAdapter(Programming wih) {
programming = wih;
}
public void ide() {
programming.code();
programming.debug();
}
}
class DailyJob {
public void work(ModernProgramming wiw) {
wiw.ide();
}
}
class Coder extends DailyJob {
public void op(Programming wih) {
new SurrogateAdapter(wih).ide();
}
}
class Coder2 extends Programming implements ModernProgramming {
public void ide() {
code();
debug();
}
}
class Coder3 extends Programming {
private class InnerAdapter implements ModernProgramming {
public void ide() {
code();
debug();
}
}
public ModernProgramming whatIWant() {
return new InnerAdapter();
}
}
public class AdapterVariations {
public static void main(String args[]) {
DailyJob whatIUse = new DailyJob();
Programming whatIHave = new Programming();
ModernProgramming adapt = new SurrogateAdapter(whatIHave);
Coder coder = new Coder();
Coder2 coder2 = new Coder2();
Coder3 coder3 = new Coder3();
whatIUse.work(adapt);
// Approach 2:
coder.op(whatIHave);
// Approach 3:
whatIUse.work(coder2);
// Approach 4:
whatIUse.work(coder3.whatIWant());
}
}
|
|
|