上一篇《坦克大战游戏-工厂模式实现(c++)》
需求:坦克大战
创建两种坦克
坦克类型 | 射程 | 速度 |
b70 | 70米 | 时/70公里 |
b50 | 50米 | 时/70公里 |
类图
代码
class Function{ public String mStr; Function(String str){ mStr = str; exe(); } public void exe() { System.out.println(mStr); } }; // interface interface Itank{ void exe(); } interface IFactory{ Itank create(); } // Concrete class B70tank implements Itank{ public void exe() { new Function("B70 tank"); } } class B50tank implements Itank{ public void exe() { new Function("B50 tank"); } } class B70Factory implements IFactory{ public Itank create() { return new B70tank(); } } class B50Factory implements IFactory{ public Itank create() { return new B50tank(); } } public class Client { public static void main(String[] args) { System.out.println("hello worldff !"); IFactory f = new B70Factory(); Itank t = f.create(); t.exe(); } }