Skip to content

Commit

Permalink
ch25 commit
Browse files Browse the repository at this point in the history
  • Loading branch information
realfirst committed May 3, 2011
1 parent 9f14490 commit 8431690
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions ch25/design_pattern.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <cstdlib>

using namespace std;

class IntIterator {
public:
virtual bool hasNext() = 0;
virtual int next() = 0;
};

class IntSet {
public:
virtual void add(int k) = 0;
virtual IntIterator *interator() const = 0;
};

int size(const InSet &set) {
int s = 0;
IntIterator *i = set.interator();
while (i->hasNext) {
i->next();
s++;
}
delete i;
return s;
}

class IntSetArrayIterator : public IntIterator {
public:
IntSetArrayIterator(int *v, int s) : values(v), current(0), size(s) {}
bool hasNext() {
return current < size;
}
int next() {
return values[current++];
}
private:
int *values;
int current, size;
};

class IntSetArray : public IntSet {
public:
IntSetArray(int sm) : values(new int[sm]), size(0), sizemax(sm) {}
~IntSetArray() {
delete values;
}
void add(int k) {
if (size >= sizemx) {
abort();
values[size++] = k;
}
}
IntIterator *iterator() const {
return new IntSetArrayIterator(values, size);
}
private:
int *values;
int size, sizemax;
};


0 comments on commit 8431690

Please sign in to comment.