001package template.series3; 002 003interface Series { 004 void next(); 005 void print(); 006} 007class SeriesFactory { 008 private SeriesFactory() {} 009 public static Series newArith() { return new ConcreteSeries(new OpAdd()); } 010 public static Series newGeom() { return new ConcreteSeries(new OpMul()); } 011} 012class ConcreteSeries implements Series { 013 int x; 014 int y = 1; 015 public void next() { x++; y = op.eval(y,2); } 016 public void print() { System.out.println("x=" + x + "; y=" + y); } 017 Op op; 018 ConcreteSeries(Op op) { this.op = op; } 019} 020interface Op { public int eval(int x, int y); } 021class OpAdd implements Op { public int eval(int x, int y) { return x+y; } } 022class OpMul implements Op { public int eval(int x, int y) { return x*y; } } 023 024public class Main { 025 public static void main(String[] args) { 026 Series x = SeriesFactory.newGeom(); 027 x.next(); 028 x.next(); 029 x.print(); 030 } 031}