![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan16; public abstract class AbstractCommand { public abstract int execute(int value); public abstract int undo(); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan16; public class Adder { private int num=0; public int add(int value) { num += value; return num; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan16; public class CalculatorForm { private AbstractCommand command; public void setCommand(AbstractCommand command) { this.command = command; } public void compute(int value) { int i = command.execute(value); System.out.println("执行后结果为:" + i); } public void undo() { int i = command.undo(); System.out.println("撤销后结果为:" + i); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan16; public class Client { public static void main(String args[]) { CalculatorForm form = new CalculatorForm(); AbstractCommand command; command = new ConcreteCommand(); form.setCommand(command); form.compute(1); form.compute(2); form.compute(3); form.undo(); form.undo(); form.undo(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package shiyan16; public class ConcreteCommand extends AbstractCommand { private Adder adder = new Adder(); private int value; public int execute(int value) { this.value=value; return adder.add(value); } public int undo() { return adder.add(-value); } }
实验18
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include<string> #include <sstream> #include <vector> using namespace std; /* object可以是任意类型的变量 */ typedef int object; class Iterator { public: virtual object begin() = 0; virtual void next() = 0; virtual object end() = 0; virtual object current() = 0; virtual bool IsDone() = 0; }; class ConcreteAggregate { private: vector<object> _objects; public: void AddObject(object obj) { _objects.push_back(obj); } object& operator[](int index) { return _objects[index]; } int size() { return _objects.size(); } }; class ConcreteIterator :public Iterator { public: ConcreteAggregate* agg; int _index; public: ConcreteIterator(ConcreteAggregate* agg) { this->agg = agg; _index = 0; } virtual object begin() { return (*agg)[0]; } virtual void next() { _index++; } virtual void preious() { _index--; } virtual object end() { _index = agg->size(); return (*agg)[_index - 1]; } virtual object current() { return (*agg)[_index]; } virtual bool IsDone() { return (_index == agg->size()); } virtual bool IsFirst() { return (_index == 0); } }; int main() { ConcreteAggregate* objects = new ConcreteAggregate(); cout << "信1305班同学:" << endl; for (int i = 1; i <= 44; i++) { int m = 20130000 + i; objects->AddObject(m); m = 0; } ConcreteIterator* iter = new ConcreteIterator(objects); ConcreteIterator* iter1 = new ConcreteIterator(objects); object tmp_begin = iter->begin(); cout << "从小到大输出:" << endl; while (!iter->IsDone()) { cout << iter->current() << endl; iter->next(); } cout << endl; object tmp_begin1 = iter1->end(); cout << "从大到小输出:" << endl; while (!iter1->IsFirst()) { iter1->preious(); cout << iter1->current() << endl; } cout << endl; delete objects; delete iter; system("pause"); return 0; }