Guest User

Untitled

a guest
Apr 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. /* GUIFactory example -- */
  2.  
  3. interface GUIFactory {
  4. public Button createButton();
  5. }
  6.  
  7. class WinFactory implements GUIFactory {
  8. public Button createButton() {
  9. return new WinButton();
  10. }
  11. }
  12. class OSXFactory implements GUIFactory {
  13. public Button createButton() {
  14. return new OSXButton();
  15. }
  16. }
  17.  
  18.  
  19. interface Button {
  20. public void paint();
  21. }
  22.  
  23. class WinButton implements Button {
  24. public void paint() {
  25. System.out.println("I'm a WinButton");
  26. }
  27. }
  28. class OSXButton implements Button {
  29. public void paint() {
  30. System.out.println("I'm an OSXButton");
  31. }
  32. }
  33.  
  34.  
  35. class Application {
  36. public Application(GUIFactory factory) {
  37. Button button = factory.createButton();
  38. button.paint();
  39. }
  40. }
  41.  
  42. public class ApplicationRunner {
  43. public static void main(String[] args) {
  44. new Application(createOsSpecificFactory());
  45. }
  46.  
  47. public static GUIFactory createOsSpecificFactory() {
  48. int sys = readFromConfigFile("OS_TYPE");
  49. if (sys == 0) {
  50. return new WinFactory();
  51. } else {
  52. return new OSXFactory();
  53. }
  54. }
  55. }
Add Comment
Please, Sign In to add comment