mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 04:44:36 +03:00
code refactoring: fix memory leaks, code style, etc.
This commit is contained in:
@@ -20,8 +20,11 @@ class ConcreteAggregate;
|
||||
* defines an interface for aggregates and it decouples your
|
||||
* client from the implementation of your collection of objects
|
||||
*/
|
||||
class Aggregate {
|
||||
class Aggregate
|
||||
{
|
||||
public:
|
||||
virtual ~Aggregate() {}
|
||||
|
||||
virtual Iterator *createIterator() = 0;
|
||||
// ...
|
||||
};
|
||||
@@ -32,25 +35,30 @@ public:
|
||||
* that returns an Iterator for its collection
|
||||
*
|
||||
*/
|
||||
class ConcreteAggregate : public Aggregate {
|
||||
class ConcreteAggregate : public Aggregate
|
||||
{
|
||||
public:
|
||||
ConcreteAggregate(const unsigned int size) {
|
||||
ConcreteAggregate( const unsigned int size )
|
||||
{
|
||||
list = new int[size]();
|
||||
count = size;
|
||||
}
|
||||
|
||||
Iterator *createIterator();
|
||||
|
||||
~ConcreteAggregate() {
|
||||
|
||||
~ConcreteAggregate()
|
||||
{
|
||||
delete[] list;
|
||||
}
|
||||
|
||||
unsigned int size() const {
|
||||
|
||||
Iterator *createIterator();
|
||||
|
||||
unsigned int size() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
int at(unsigned int index) {
|
||||
return list[index];
|
||||
|
||||
int at( unsigned int index )
|
||||
{
|
||||
return list[ index ];
|
||||
}
|
||||
// ...
|
||||
|
||||
@@ -65,10 +73,11 @@ private:
|
||||
* provides the interface that all iterators must implement and
|
||||
* a set of methods for traversing over elements
|
||||
*/
|
||||
class Iterator {
|
||||
class Iterator
|
||||
{
|
||||
public:
|
||||
virtual ~Iterator() { /* ... */ }
|
||||
|
||||
|
||||
virtual void first() = 0;
|
||||
virtual void next() = 0;
|
||||
virtual bool isDone() const = 0;
|
||||
@@ -81,25 +90,33 @@ public:
|
||||
* implements the interface and is responsible for managing
|
||||
* the current position of the iterator
|
||||
*/
|
||||
class ConcreteIterator : public Iterator {
|
||||
class ConcreteIterator : public Iterator
|
||||
{
|
||||
public:
|
||||
ConcreteIterator(ConcreteAggregate *l)
|
||||
: list(l), index(0) {}
|
||||
|
||||
void first() {
|
||||
ConcreteIterator( ConcreteAggregate *l ) :
|
||||
list( l ), index( 0 ) {}
|
||||
|
||||
~ConcreteIterator() {}
|
||||
|
||||
void first()
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
void next() {
|
||||
|
||||
void next()
|
||||
{
|
||||
index++;
|
||||
}
|
||||
|
||||
bool isDone() const {
|
||||
return (index >= list->size());
|
||||
|
||||
bool isDone() const
|
||||
{
|
||||
return ( index >= list->size() );
|
||||
}
|
||||
|
||||
int currentItem() const {
|
||||
if (isDone()) {
|
||||
|
||||
int currentItem() const
|
||||
{
|
||||
if ( isDone() )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return list->at(index);
|
||||
@@ -112,21 +129,23 @@ private:
|
||||
// ...
|
||||
};
|
||||
|
||||
Iterator *ConcreteAggregate::createIterator() {
|
||||
return new ConcreteIterator(this);
|
||||
Iterator *ConcreteAggregate::createIterator()
|
||||
{
|
||||
return new ConcreteIterator( this );
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned int size = 5;
|
||||
ConcreteAggregate list = ConcreteAggregate(size);
|
||||
|
||||
ConcreteAggregate list = ConcreteAggregate( size );
|
||||
|
||||
Iterator *it = list.createIterator();
|
||||
for ( ; !it->isDone(); it->next()) {
|
||||
for ( ; !it->isDone(); it->next())
|
||||
{
|
||||
std::cout << "Item value: " << it->currentItem() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
delete it;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user