Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. //Abstract Factory Class - that return factory type
  2. public class ComplexInstallFactory extends InstallFactory {
  3. throws BadInstallException {
  4. public Install getInstall(int InstallTypeId) {
  5. Install Install = null;
  6. if (InstallTypeId == Const.Install_on_Phone) {
  7. //complex Install (lets say - installing on phone)
  8. Install = new on_Phone();
  9. } else throw new BadInstallException(“InstallTypeId = ” + InstallTypeId);
  10. return Install;
  11. }
  12. }
  13.  
  14. //Factory Type class to define what factory will be chosen
  15. public class InstallFactoryType
  16. throws BadInstallFactoryException {
  17. public static final int TYPE_SIMPLE = 1;
  18. public static final int TYPE_COMPLEX = 2;
  19. public InstallFactory getInstallFactory(int type) {
  20. InstallFactory sf = null;
  21. if (type == TYPE_SIMPLE) {
  22. sf = new SimpleInstallFactory();
  23. } else if (type == TYPE_COMPLEX) {
  24. sf = new ComplexInstallFactory();
  25. } else throw new BadInstallFactoryException(“No factory !! ”);
  26. return sf;
  27. }
  28. }
  29.  
  30. //------------------------------------------------------------------------------
  31. //Now let’s look at the calling code, which uses the factory:
  32. //------------------------------------------------------------------------------
  33.  
  34. InstallFactoryType abFac = new InstallFactoryType();
  35. InstallFactory factory = null;
  36. Install s = null;
  37. //returns a InstallFactory but whether it is a //SimpleInstallFactory or a ComplexInstallFactory is not //known to the caller.
  38. factory = abFac.getInstallFactory(1); //returns SimpleInstallFactory
  39. //returns a Install but whether it is a Onsite or a Pentagon is //not known to the caller.
  40. s = factory.getInstall(2); //returns Remote. s.Install(); //Installs a Remote
  41. //returns a InstallFactory but whether it is a //SimpleInstallFactory or a ComplexInstallFactory is not //known to the caller.
  42. factory = abFac.getInstallFactory(2);
  43. //returns a Install but whether it is a Onsite or a Pentagon is //not known to the caller.
  44. s = factory.getInstall(3); //returns a pentagon.
  45. s.Install(); //Installs a pentagon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement