mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 12:54:36 +03:00
code refactoring: fix memory leaks, code style, etc.
This commit is contained in:
@@ -16,9 +16,10 @@
|
||||
* declares an interface through which flyweights can receive
|
||||
* and act on extrinsic state
|
||||
*/
|
||||
class Flyweight {
|
||||
class Flyweight
|
||||
{
|
||||
public:
|
||||
virtual ~Flyweight() { /* ... */ }
|
||||
virtual ~Flyweight() {}
|
||||
virtual void operation() = 0;
|
||||
// ...
|
||||
};
|
||||
@@ -27,16 +28,20 @@ public:
|
||||
* UnsharedConcreteFlyweight
|
||||
* not all subclasses need to be shared
|
||||
*/
|
||||
class UnsharedConcreteFlyweight : public Flyweight {
|
||||
class UnsharedConcreteFlyweight : public Flyweight
|
||||
{
|
||||
public:
|
||||
UnsharedConcreteFlyweight(int intrinsic_state) :
|
||||
state(intrinsic_state) {}
|
||||
|
||||
void operation() {
|
||||
UnsharedConcreteFlyweight( const int intrinsic_state ) :
|
||||
state( intrinsic_state ) {}
|
||||
|
||||
~UnsharedConcreteFlyweight() {}
|
||||
|
||||
void operation()
|
||||
{
|
||||
std::cout << "Unshared Flyweight with state " << state << std::endl;
|
||||
}
|
||||
// ...
|
||||
|
||||
|
||||
private:
|
||||
int state;
|
||||
// ...
|
||||
@@ -47,16 +52,20 @@ private:
|
||||
* implements the Flyweight interface and adds storage
|
||||
* for intrinsic state
|
||||
*/
|
||||
class ConcreteFlyweight : public Flyweight {
|
||||
class ConcreteFlyweight : public Flyweight
|
||||
{
|
||||
public:
|
||||
ConcreteFlyweight(int all_state) :
|
||||
state(all_state) {}
|
||||
|
||||
void operation() {
|
||||
ConcreteFlyweight( const int all_state ) :
|
||||
state( all_state ) {}
|
||||
|
||||
~ConcreteFlyweight() {}
|
||||
|
||||
void operation()
|
||||
{
|
||||
std::cout << "Concrete Flyweight with state " << state << std::endl;
|
||||
}
|
||||
// ...
|
||||
|
||||
|
||||
private:
|
||||
int state;
|
||||
// ...
|
||||
@@ -67,27 +76,32 @@ private:
|
||||
* creates and manages flyweight objects and ensures
|
||||
* that flyweights are shared properly
|
||||
*/
|
||||
class FlyweightFactory {
|
||||
class FlyweightFactory
|
||||
{
|
||||
public:
|
||||
virtual ~FlyweightFactory() {
|
||||
for (auto it = flies.begin(); it != flies.end(); it++) {
|
||||
~FlyweightFactory()
|
||||
{
|
||||
for ( auto it = flies.begin(); it != flies.end(); it++ )
|
||||
{
|
||||
delete it->second;
|
||||
}
|
||||
flies.clear();
|
||||
}
|
||||
|
||||
Flyweight *getFlyweight(int key) {
|
||||
if (flies.find(key) != flies.end()) {
|
||||
return flies[key];
|
||||
|
||||
Flyweight *getFlyweight( const int key )
|
||||
{
|
||||
if ( flies.find( key ) != flies.end() )
|
||||
{
|
||||
return flies[ key ];
|
||||
}
|
||||
Flyweight *fly = new ConcreteFlyweight(key);
|
||||
flies.insert(std::pair<int, Flyweight *>(key, fly));
|
||||
Flyweight *fly = new ConcreteFlyweight( key );
|
||||
flies.insert( std::pair<int, Flyweight *>( key, fly ) );
|
||||
return fly;
|
||||
}
|
||||
// ...
|
||||
|
||||
private:
|
||||
std::map<int, Flyweight *> flies;
|
||||
std::map<int, Flyweight*> flies;
|
||||
// ...
|
||||
};
|
||||
|
||||
@@ -97,6 +111,6 @@ int main()
|
||||
FlyweightFactory *factory = new FlyweightFactory;
|
||||
factory->getFlyweight(1)->operation();
|
||||
factory->getFlyweight(2)->operation();
|
||||
|
||||
delete factory;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user