Posts Tagged ‘Java’

小探设计模式(三)

星期一, 10月 13th, 2008

抽象工厂

简单工厂解决了对象创建过程和创建逻辑的封装,工厂方法则采用“分而治之”的思想把规模比较大的工厂类分布到各个工厂之类解决。简单工厂和工厂方法模式都是解决了单一产品等级对象创建问题。实际解决问题的时候经常会出现需要创建一系列互相依赖的对象,如果采用工厂方法模式则可能出现创建多个产品额外需要实例化多个工厂类,抽象工厂解决这一问题。

还是使用War3举例(此问题只考虑目标对象,不考虑单位独立性问题,不考虑单位的生产成本和实战问题),NE有角鹰骑士,让AC坐在角鹰背上,一般情况下需要创建一个角鹰其实需要一棵BR然后建一个AC,还需要一棵BW,出一个角鹰,然后要让AC学会骑鸟,显然成本比较高。Orc也有个骑鸟的,双足飞龙,但是它的“驾驶员”和“飞龙”是同时在兽栏被创建,玩家不必为驾驶员和飞龙各自建立工厂,也不必介意那只兽人是如何骑到飞龙上面的,兽栏已经为你做好了一切。

Windrider在创建模式上是一个抽象工厂,但在这里同时也是一个战斗单位,所以要实现战斗单位的接口。兽栏(Beastiary)作为工厂方法引导双足飞龙创建。

战斗单位和工厂的借口就不重复写了。

  1. //Beastiary.java
  2. package warunit;
  3.  
  4. public class Beastiary implements IWarFactory {
  5.  
  6. public IWarUnit train(String type){
  7. if(type.equalsIgnoreCase("windrider")){
  8. return new Windrider();
  9. }
  10. return null;
  11. }
  12. }
  13.  
  14. //Orcman.java
  15. package warunit;
  16.  
  17. public class Orcman implements IWarUnit {
  18.  
  19. public Orcman(){
  20. this.born();
  21. }
  22.  
  23. public void born(){
  24. System.out.print("Ready to soar Master!\n");
  25. }
  26. }
  27.  
  28. //Dragon.java
  29. package warunit;
  30.  
  31. public class Dragon implements IWarUnit {
  32.  
  33. public Dragon(){
  34. this.born();
  35. }
  36.  
  37. public void born(){
  38. System.out.print("Ah~\n");
  39. }
  40. }
  41.  
  42. //AbstractFactory.java
  43. package warunit;
  44.  
  45. public class AbstractFactory {
  46.  
  47. IWarUnit rider;
  48. IWarUnit flyer;
  49. }
  50.  
  51. //Windrider.java
  52. package warunit;
  53.  
  54. public class Windrider extends AbstractFactory implements IWarUnit {
  55.  
  56. public Windrider(){
  57. this.rider=new Orcman();
  58. this.flyer=new Dragon();
  59. }
  60. }
  61.  
  62. //Main.java
  63. import warunit.*;
  64.  
  65. public class Main {
  66.  
  67. public static void main(String[] args){
  68. IWarFactory BE=new Beastiary();
  69. BE.train("windrider");
  70. }
  71. }

小探设计模式(二)

星期一, 9月 29th, 2008

工厂方法

工厂方法把构造产品对象的任务交给子类处理。还是以War3作例。

  1. //IWarFactory.java 工厂父类
  2. package warunit;
  3.  
  4. public interface IWarFactory {
  5.  
  6. public IWarUnit train(String type);
  7. }
  8.  
  9. //AncientOfWar.java 战争古树
  10. package warunit;
  11.  
  12. public class AncientOfWar implements IWarFactory {
  13.  
  14. public IWarUnit train(String type){
  15. if(type.equalsIgnoreCase("Archer")){
  16. return new Archer();
  17. }
  18. if(type.equalsIgnoreCase("Huntress")){
  19. return new Huntress();
  20. }
  21. return null;
  22. }
  23. }
  24.  
  25. //AncientOfWind.java 风之古树
  26. package warunit;
  27.  
  28. public class AncientOfWind implements IWarFactory {
  29.  
  30. public AncientOfWind(){
  31.  
  32. }
  33.  
  34. public IWarUnit train(String type){
  35. if(type.equalsIgnoreCase("hippogryph")){
  36. return new Hippogryph();
  37. }
  38. return null;
  39. }
  40. }
  41.  
  42. //IWarUnit.java 战争单位(兵)父类
  43. package warunit;
  44.  
  45. public interface IWarUnit {
  46.  
  47. abstract public void born();
  48.  
  49. abstract public void death();
  50. }
  51.  
  52. //Archer.java AC MM
  53. package warunit;
  54.  
  55. public class Archer implements IWarUnit {
  56.  
  57. public Archer(){
  58. this.born();
  59. }
  60.  
  61. public void born(){
  62. System.out.print("an Archer had been trained\n");
  63. }
  64.  
  65. public void death(){
  66. System.out.print("an Archer had been killed\n");
  67. }
  68. }
  69.  
  70. //Hippogryph 角鹰
  71. package warunit;
  72.  
  73. public class Hippogryph implements IWarUnit {
  74.  
  75. public Hippogryph(){
  76. this.born();
  77. }
  78.  
  79. public void born(){
  80. System.out.print("A Hippogryph was born");
  81. }
  82.  
  83. public void death(){
  84. System.out.print("A Hippogryph had been killed");
  85. }
  86. }
  87.  
  88. //程序入口
  89. import warunit.*;
  90.  
  91. public class Main {
  92.  
  93. public static void main(String[] args){
  94. AncientOfWar Br=new AncientOfWar();
  95. IWarUnit Ac1=Br.train("Archer");
  96. AncientOfWind Bw=new AncientOfWind();
  97. IWarUnit hp=Bw.train("hippogryph");
  98. Ac1.death();
  99. hp.death();
  100. }
  101. }

小探设计模式(一)

星期五, 9月 19th, 2008

简单工厂

使用一个类负责创建其他类的实例,被创建的实例通常都有共同的父类。借War3打个比喻,Archer 和 Huntress都是战争古书训练。AC和Huntress都是战斗单位(兵),都是“兵”的派生类。

打码:

  1. //程序入口
  2. import warunit.*;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args){
  7. AncientOfWar Br=new AncientOfWar();
  8. IWarUnit Ac1=Br.train("Archer");
  9. IWarUnit Ht1=Br.train("Huntress");
  10. Ac1.death();
  11. Ht1.death();
  12. }
  13. }
  14.  
  15. //战斗单位接口
  16. package warunit;
  17.  
  18. public interface IWarUnit {
  19.  
  20. abstract public void born();
  21.  
  22. abstract public void death();
  23. }
  24.  
  25. //AC MM
  26. package warunit;
  27.  
  28. public class Archer implements IWarUnit {
  29.  
  30. public Archer(){
  31. this.born();
  32. }
  33.  
  34. public void born(){
  35. System.out.print("an Archer had been trained\n");
  36. }
  37.  
  38. public void death(){
  39. System.out.print("an Archer had been killed\n");
  40. }
  41. }
  42.  
  43. //Huntress
  44. package warunit;
  45.  
  46. public class Huntress implements IWarUnit {
  47.  
  48. public Huntress(){
  49. this.born();
  50. }
  51.  
  52. public void born(){
  53. System.out.print("an Huntress had been trained\n");
  54. }
  55.  
  56. public void death(){
  57. System.out.print("an Huntress had been killed\n");
  58. }
  59. }
  60.  
  61. //BR
  62. package warunit;
  63.  
  64. public class AncientOfWar {
  65.  
  66. public IWarUnit train(String type){
  67. if(type.equalsIgnoreCase("Archer")){
  68. return new Archer();
  69. }
  70. if(type.equalsIgnoreCase("Huntress")){
  71. return new Huntress();
  72. }
  73. return null;
  74. }
  75. }