Introduction to OOP | Chapter 21: Software Frameworks : | next | previous | audio | real | text |
class InsertionSorter { public: void sort () { int n = size(); for (int i = 1; i < n; i++) { int j = i - 1; while (j >= 0 && lessThan(j+1, j)) { swap(j, j+1); j = j - 1; } } } private: virtual int size() = 0; // abstract methods virtual boolean lessThan(int i, int j) = 0; virtual void swap(int i, int j) = 0; }The part that is common in made into a foundation method, the part that changes are made into deferred methods.