浏览 1267 次
锁定老帖子 主题:新人献丑讨论一下领域模式
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-17
public Game getGame(Game game){ System.out.println("人得到猎物"); return game; } } class Gun{ public Gun openingGun(Person person){ Gun gun = new Gun(); System.out.println("人在开枪"); return gun; } /* public Gun openingGun(Other other){ Gun gun = new Gun(); System.out.println("其他在开枪"); return gun; } */ } class Bullet{ public Bullet attack(Gun gun){ Bullet bullet = new Bullet(); System.out.println("枪在射击"); return bullet; } } class Knife{ public Knife attack(Person person){ Knife knife = new Knife(); System.out.println("人在用刀"); return knife; } } class Game{ public Game Hits(Bullet bullet){ Game game = new Game(); System.out.println("被子弹击中"); return game; } public Game Hits(Knife knife){ Game game = new Game(); System.out.println("被刀击中"); return game; } } class Consloe{ public void GoingHunting(){ Person person = new Person(); // 用到打猎 Gun gun = new Gun(); //人使用枪 Gun gunReturn=gun.openingGun(person); //枪使用子弹 Bullet bullet = new Bullet(); //子弹发射 Bullet bulletReturn=bullet.attack(gunReturn); //子弹击中猎物 Game game =new Game(); //猎物被击中 Game gameReturn= game.Hits(bulletReturn); person.getGame(gameReturn); } } class ConsloeEx extends Consloe{ public void GoingHunting(){ Person person = new Person(); // 用枪打猎 Knife knife = new Knife(); //人使用刀 Knife knifeReturn=knife.attack(person); //刀弹击中猎物 Game game =new Game(); //猎物被击中 Game gameReturn= game.Hits(knifeReturn); //得到猎物 person.getGame(gameReturn); } } public class Demo{ public static void main(String[] arge){ Consloe console=new Consloe(); console.GoingHunting(); Consloe consoleEx=new ConsloeEx(); consoleEx.GoingHunting(); } } //模拟了一下打猎的场景 简单设计了一个模式 望高手指出 优缺点 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |