166 |
|
with the problem of creating objects without specifying the exact |
167 |
|
class of object that will be created. Factory Method is typically |
168 |
|
implemented by delegating the creation operation to the subclasses. |
169 |
+ |
|
170 |
+ |
Registers a creator with a type identifier. Looks up the type |
171 |
+ |
identifier in the internal map. If it is found, it invokes the |
172 |
+ |
corresponding creator for the type identifier and returns its |
173 |
+ |
result. |
174 |
|
\begin{lstlisting}[float,caption={[].},label={appendixScheme:factoryDeclaration}] |
175 |
|
class IntegratorCreator; |
176 |
|
class IntegratorFactory { |
177 |
|
public: |
178 |
|
typedef std::map<std::string, IntegratorCreator*> CreatorMapType; |
179 |
|
|
175 |
– |
/** |
176 |
– |
* Registers a creator with a type identifier |
177 |
– |
* @return true if registration is successful, otherwise return false |
178 |
– |
* @id the identification of the concrete object |
179 |
– |
* @creator the object responsible to create the concrete object |
180 |
– |
*/ |
180 |
|
bool registerIntegrator(IntegratorCreator* creator); |
181 |
|
|
183 |
– |
/** |
184 |
– |
* Looks up the type identifier in the internal map. If it is found, it invokes the |
185 |
– |
* corresponding creator for the type identifier and returns its result. |
186 |
– |
* @return a pointer of the concrete object, return NULL if no creator is registed for |
187 |
– |
* creating this concrete object |
188 |
– |
* @param id the identification of the concrete object |
189 |
– |
*/ |
182 |
|
Integrator* createIntegrator(const std::string& id, SimInfo* info); |
183 |
|
|
184 |
|
private: |
191 |
|
return creatorMap_.erase(id) == 1; |
192 |
|
} |
193 |
|
|
194 |
< |
Integrator* IntegratorFactory::createIntegrator(const std::string& id, SimInfo* info) { |
194 |
> |
Integrator* |
195 |
> |
IntegratorFactory::createIntegrator(const std::string& id, SimInfo* info) { |
196 |
|
CreatorMapType::iterator i = creatorMap_.find(id); |
197 |
|
if (i != creatorMap_.end()) { |
198 |
|
//invoke functor to create object |
208 |
|
class IntegratorCreator { |
209 |
|
public: |
210 |
|
IntegratorCreator(const std::string& ident) : ident_(ident) {} |
211 |
< |
virtual ~IntegratorCreator() {} |
211 |
> |
|
212 |
|
const std::string& getIdent() const { return ident_; } |
213 |
|
|
214 |
|
virtual Integrator* create(SimInfo* info) const = 0; |
221 |
|
class IntegratorBuilder : public IntegratorCreator { |
222 |
|
public: |
223 |
|
IntegratorBuilder(const std::string& ident) : IntegratorCreator(ident) {} |
224 |
< |
virtual Integrator* create(SimInfo* info) const {return new ConcreteIntegrator(info);} |
224 |
> |
virtual Integrator* create(SimInfo* info) const { |
225 |
> |
return new ConcreteIntegrator(info); |
226 |
> |
} |
227 |
|
}; |
228 |
|
\end{lstlisting} |
229 |
|
|