您的位置:首页 > 其它

想要成为高薪程序猿的看这里!

2015-12-10 15:23 253 查看
/* GUIFactory example --
The output should be either "I'm a WinButton" or "I'm an OSXButton"
depending on which kind of factory was used. Note that the Application
has no idea what kind of GUIFactory it is given or even what kind of
Button that factory creates.*/

interface GUIFactory {
public Button createButton();
}

class WinFactory implements GUIFactory {
public Button createButton() {
return new WinButton();
}
}

class OSXFactory implements GUIFactory {
public Button createButton() {
return new OSXButton();
}
}

interface Button {
public void paint();
}

class WinButton implements Button {
public void paint() {
System.out.println("I'm a WinButton");
}
}

class OSXButton implements Button {
public void paint() {
System.out.println("I'm an OSXButton");
}
}

class Application {
public Application(GUIFactory factory){
Button button = factory.createButton();
button.paint();
}
}

public class ApplicationRunner {
public static void main(String[] args) {
new Application(createOsSpecificFactory());
}

public static GUIFactory createOsSpecificFactory() {
int sys = readFromConfigFile("OS_TYPE");
if (sys == 0) {
return new WinFactory();
} else {
return new OSXFactory();
}
}
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: