Open main menu

CDOT Wiki β

Changes

Abstract Factory

425 bytes added, 20:41, 18 February 2007
Code Example
===Code Example===
'''JAVA'''
<pre>
/*
* GUIFactory example
*/
public abstract class CPU GUIFactory { public static GUIFactory getFactory() { int sys = readFromConfigFile("OS_TYPE"); if (sys == 0) { return(new WinFactory()); } else { return(new OSXFactory()); } } public abstract Button createButton();}
class WinFactory extends GUIFactory { public Button createButton() { return(new WinButton()); }} //class CPU
The concrete widget classes are simply classes that implement the widget interfaces:class OSXFactory extends GUIFactory { public Button createButton() { return(new OSXButton()); }}
public abstract class EmberCPU extends CPU Button { private String caption; public abstract void paint(); public String getCaption(){ return caption; } public void setCaption(String caption){ this...caption = caption; } } // class EmberCPU
Below is code for class WinButton extends Button { public void paint() { System.out.println("I'm a concrete factory class that creates instances of classes to test ember architecture computersWinButton:" + getCaption()); }}
class EmberToolkit OSXButton extends ArchitectureToolkit Button { public CPU createCPUvoid paint() { return new EmberCPU System.out.println("I'm a OSXButton : " + getCaption()); } // createCPU()}
public class Application { public MMU createMMUstatic void main(String[] args) { return new EmberMMUGUIFactory aFactory = GUIFactory.getFactory(); Button aButton = aFactory.createButton(); aButton.setCaption("Play"); aButton.paint(); } // createMMU()output is //I'm a WinButton: Play //or ...//I'm a OSXButton: Play} <// class EmberFactorypre>
Below is the code for the abstract factory class'''C++'''<pre>#include <memory>using std::auto_ptr;
public abstract class ArchitectureToolkit Control { private static final EmberToolkit emberToolkit = newEmberToolkit()}; private static final EnginolaToolkitenginolaToolkit = new EnginolaToolkit();...
/** * Returns a concrete factory object that is aninstance of the * concrete factory class appropriate for the givenarchitecture. */ static final ArchitectureToolkit getFactory(intarchitecture) PushControl : public Control { switch (architecture) { case ENGINOLA: &nbsp; return enginolaToolkit};
case EMBERclass Factory { public: &nbs // Returns Factory subclass based on classKey. Eachp; return emberToolkit; .. // subclass has its own getControl() implementation. } // switchThis will be implemented after the subclasses have String errMsg = // been declared.Integer.toString static auto_ptr<Factory> getFactory(architectureint classKey); throw newIllegalArgumentException virtual auto_ptr<Control> getControl(errMsg)const = 0; } // getFactory();
class ControlFactory : public abstract CPU createCPUFactory {public: virtual auto_ptr<Control> getControl() ;const { public abstract MMU createMMU return auto_ptr<Control>(new PushControl()) ; ... }} // AbstractFactory;
Client classes typically create concrete widget objects using code that looks something like thisauto_ptr<Factory> Factory: :getFactory(int classKey) {public class Client { // Insert conditional logic here. Sample: public void doIt switch(classKey) { AbstractFactory af; default: af =AbstractFactory.getFactory return auto_ptr<Factory>(new ControlFactory(AbstractFactory.EMBER); CPU cpu = af.createCPU(); ... } } // doIt} /</ class Clientpre>
===Reference===
1
edit