132 |
|
\begin{lstlisting}[float,caption={[A classic Singleton design pattern implementation(I)] Declaration of {\tt IntegratorFactory} class.},label={appendixScheme:singletonDeclaration}] |
133 |
|
|
134 |
|
class IntegratorFactory { |
135 |
< |
public: |
136 |
< |
static IntegratorFactory* getInstance(); |
137 |
< |
protected: |
138 |
< |
IntegratorFactory(); |
139 |
< |
private: |
140 |
< |
static IntegratorFactory* instance_; |
135 |
> |
public: |
136 |
> |
static IntegratorFactory* |
137 |
> |
getInstance(); |
138 |
> |
protected: |
139 |
> |
IntegratorFactory(); |
140 |
> |
private: |
141 |
> |
static IntegratorFactory* instance_; |
142 |
|
}; |
143 |
|
|
144 |
|
\end{lstlisting} |
177 |
|
\begin{lstlisting}[float,caption={[The implementation of Factory pattern (I)].},label={appendixScheme:factoryDeclaration}] |
178 |
|
|
179 |
|
class IntegratorFactory { |
180 |
< |
public: |
181 |
< |
typedef std::map<string, IntegratorCreator*> CreatorMapType; |
180 |
> |
public: |
181 |
> |
typedef std::map<string, IntegratorCreator*> CreatorMapType; |
182 |
|
|
183 |
< |
bool registerIntegrator(IntegratorCreator* creator) { |
184 |
< |
return creatorMap_.insert(creator->getIdent(), creator).second; |
185 |
< |
} |
183 |
> |
bool registerIntegrator(IntegratorCreator* creator) { |
184 |
> |
return creatorMap_.insert(creator->getIdent(), creator).second; |
185 |
> |
} |
186 |
|
|
187 |
< |
Integrator* createIntegrator(const string& id, SimInfo* info) { |
188 |
< |
Integrator* result = NULL; |
189 |
< |
CreatorMapType::iterator i = creatorMap_.find(id); |
190 |
< |
if (i != creatorMap_.end()) { |
191 |
< |
result = (i->second)->create(info); |
191 |
< |
} |
192 |
< |
return result; |
187 |
> |
Integrator* createIntegrator(const string& id, SimInfo* info) { |
188 |
> |
Integrator* result = NULL; |
189 |
> |
CreatorMapType::iterator i = creatorMap_.find(id); |
190 |
> |
if (i != creatorMap_.end()) { |
191 |
> |
result = (i->second)->create(info); |
192 |
|
} |
193 |
+ |
return result; |
194 |
+ |
} |
195 |
|
|
196 |
< |
private: |
197 |
< |
CreatorMapType creatorMap_; |
196 |
> |
private: |
197 |
> |
CreatorMapType creatorMap_; |
198 |
|
}; |
199 |
|
\end{lstlisting} |
200 |
|
\begin{lstlisting}[float,caption={[The implementation of Factory pattern (III)]Souce code of creator classes.},label={appendixScheme:integratorCreator}] |
201 |
|
|
202 |
|
class IntegratorCreator { |
203 |
< |
public: |
203 |
> |
public: |
204 |
|
IntegratorCreator(const string& ident) : ident_(ident) {} |
205 |
|
|
206 |
|
const string& getIdent() const { return ident_; } |
207 |
|
|
208 |
|
virtual Integrator* create(SimInfo* info) const = 0; |
209 |
|
|
210 |
< |
private: |
210 |
> |
private: |
211 |
|
string ident_; |
212 |
|
}; |
213 |
|
|
214 |
|
template<class ConcreteIntegrator> |
215 |
|
class IntegratorBuilder : public IntegratorCreator { |
216 |
< |
public: |
217 |
< |
IntegratorBuilder(const string& ident) : IntegratorCreator(ident) {} |
218 |
< |
virtual Integrator* create(SimInfo* info) const { |
219 |
< |
return new ConcreteIntegrator(info); |
220 |
< |
} |
216 |
> |
public: |
217 |
> |
IntegratorBuilder(const string& ident) |
218 |
> |
: IntegratorCreator(ident) {} |
219 |
> |
virtual Integrator* create(SimInfo* info) const { |
220 |
> |
return new ConcreteIntegrator(info); |
221 |
> |
} |
222 |
|
}; |
223 |
|
\end{lstlisting} |
224 |
|
|
244 |
|
\begin{lstlisting}[float,caption={[The implementation of Visitor pattern (I)]Source code of the visitor classes.},label={appendixScheme:visitor}] |
245 |
|
|
246 |
|
class BaseVisitor{ |
247 |
< |
public: |
248 |
< |
virtual void visit(Atom* atom); |
249 |
< |
virtual void visit(DirectionalAtom* datom); |
250 |
< |
virtual void visit(RigidBody* rb); |
247 |
> |
public: |
248 |
> |
virtual void visit(Atom* atom); |
249 |
> |
virtual void visit(DirectionalAtom* datom); |
250 |
> |
virtual void visit(RigidBody* rb); |
251 |
|
}; |
252 |
|
|
253 |
|
\end{lstlisting} |
255 |
|
\begin{lstlisting}[float,caption={[The implementation of Visitor pattern (II)]Source code of the element classes.},label={appendixScheme:element}] |
256 |
|
|
257 |
|
class StuntDouble { |
258 |
< |
public: |
259 |
< |
virtual void accept(BaseVisitor* v) = 0; |
258 |
> |
public: |
259 |
> |
virtual void accept(BaseVisitor* v) = 0; |
260 |
|
}; |
261 |
|
|
262 |
|
class Atom: public StuntDouble { |
263 |
< |
public: |
264 |
< |
virtual void accept{BaseVisitor* v*} { |
265 |
< |
v->visit(this); |
266 |
< |
} |
263 |
> |
public: |
264 |
> |
virtual void accept{BaseVisitor* v*} { |
265 |
> |
v->visit(this); |
266 |
> |
} |
267 |
|
}; |
268 |
|
|
269 |
|
class DirectionalAtom: public Atom { |
270 |
< |
public: |
271 |
< |
virtual void accept{BaseVisitor* v*} { |
272 |
< |
v->visit(this); |
273 |
< |
} |
270 |
> |
public: |
271 |
> |
virtual void accept{BaseVisitor* v*} { |
272 |
> |
v->visit(this); |
273 |
> |
} |
274 |
|
}; |
275 |
|
|
276 |
|
class RigidBody: public StuntDouble { |
277 |
< |
public: |
278 |
< |
virtual void accept{BaseVisitor* v*} { |
279 |
< |
v->visit(this); |
280 |
< |
} |
277 |
> |
public: |
278 |
> |
virtual void accept{BaseVisitor* v*} { |
279 |
> |
v->visit(this); |
280 |
> |
} |
281 |
|
}; |
282 |
|
|
283 |
|
\end{lstlisting} |