Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Abstract Factory Class - that return factory type
- public class ComplexInstallFactory extends InstallFactory {
- throws BadInstallException {
- public Install getInstall(int InstallTypeId) {
- Install Install = null;
- if (InstallTypeId == Const.Install_on_Phone) {
- //complex Install (lets say - installing on phone)
- Install = new on_Phone();
- } else throw new BadInstallException(“InstallTypeId = ” + InstallTypeId);
- return Install;
- }
- }
- //Factory Type class to define what factory will be chosen
- public class InstallFactoryType
- throws BadInstallFactoryException {
- public static final int TYPE_SIMPLE = 1;
- public static final int TYPE_COMPLEX = 2;
- public InstallFactory getInstallFactory(int type) {
- InstallFactory sf = null;
- if (type == TYPE_SIMPLE) {
- sf = new SimpleInstallFactory();
- } else if (type == TYPE_COMPLEX) {
- sf = new ComplexInstallFactory();
- } else throw new BadInstallFactoryException(“No factory !! ”);
- return sf;
- }
- }
- //------------------------------------------------------------------------------
- //Now let’s look at the calling code, which uses the factory:
- //------------------------------------------------------------------------------
- InstallFactoryType abFac = new InstallFactoryType();
- InstallFactory factory = null;
- Install s = null;
- //returns a InstallFactory but whether it is a //SimpleInstallFactory or a ComplexInstallFactory is not //known to the caller.
- factory = abFac.getInstallFactory(1); //returns SimpleInstallFactory
- //returns a Install but whether it is a Onsite or a Pentagon is //not known to the caller.
- s = factory.getInstall(2); //returns Remote. s.Install(); //Installs a Remote
- //returns a InstallFactory but whether it is a //SimpleInstallFactory or a ComplexInstallFactory is not //known to the caller.
- factory = abFac.getInstallFactory(2);
- //returns a Install but whether it is a Onsite or a Pentagon is //not known to the caller.
- s = factory.getInstall(3); //returns a pentagon.
- s.Install(); //Installs a pentagon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement