Open main menu

CDOT Wiki β

Changes

Abstract Factory

813 bytes added, 20:41, 18 February 2007
Code Example
===Code Example===
'''JAVA'''
<pre>
/*
//or
//I'm a OSXButton: Play
}
</pre>
 
'''C++'''
<pre>
#include <memory>
using std::auto_ptr;
 
class Control { };
 
class PushControl : public Control { };
 
class Factory {
public:
// Returns Factory subclass based on classKey. Each
// subclass has its own getControl() implementation.
// This will be implemented after the subclasses have
// been declared.
static auto_ptr<Factory> getFactory(int classKey);
virtual auto_ptr<Control> getControl() const = 0;
};
 
class ControlFactory : public Factory {
public:
virtual auto_ptr<Control> getControl() const {
return auto_ptr<Control>(new PushControl());
}
};
 
auto_ptr<Factory> Factory::getFactory(int classKey) {
// Insert conditional logic here. Sample:
switch(classKey) {
default:
return auto_ptr<Factory>(new ControlFactory());
}
}
</pre>
1
edit