Паттерн: Абстрактная фабрика (Abstract Factory)
Исходник: Car.java, язык: java [code #400, hits: 11184]
автор: this [добавлен: 24.05.2007]
  1. package abstractFactory;
  2.  
  3. public abstract class Car {
  4. public final static int LEFT = 0; // side
  5. public final static int RIGHT = 1; // side
  6. public final static int FRONT = 2; // position
  7. public final static int BACK = 3; // position
  8.  
  9. public final static int RED = 0;
  10. public final static int BLUE = 1;
  11. public final static int WHITE = 2;
  12. public final static int METALLIC = 3;
  13.  
  14. private String number;
  15. private int doorNum;
  16. private int wheelNum;
  17. private int seatNum;
  18. private int maxSpeed;
  19.  
  20. private int engine = -1;
  21.  
  22. private int color;
  23.  
  24. public Car(String number, int doorNum, int wheelNum, int seetNum, int maxSpeed) {
  25. super();
  26. this.number = number;
  27. this.doorNum = doorNum;
  28. this.wheelNum = wheelNum;
  29. this.seatNum = seetNum;
  30. this.maxSpeed = maxSpeed;
  31. this.color = Car.WHITE;
  32. }
  33.  
  34. public int getDoorNum() {
  35. return doorNum;
  36. }
  37. public void setDoorNum(int doorNum) {
  38. this.doorNum = doorNum;
  39. }
  40. public int getMaxSpeed() {
  41. return maxSpeed;
  42. }
  43. public void setMaxSpeed(int maxSpeed) {
  44. this.maxSpeed = maxSpeed;
  45. }
  46. public int getSeatNum() {
  47. return seatNum;
  48. }
  49. public void setSeatNum(int seetNum) {
  50. this.seatNum = seetNum;
  51. }
  52. public int getWheelNum() {
  53. return wheelNum;
  54. }
  55. public void setWheelNum(int wheelNum) {
  56. this.wheelNum = wheelNum;
  57. }
  58.  
  59. public String getNumber() {
  60. return number;
  61. }
  62.  
  63. public void setNumber(String number) {
  64. this.number = number;
  65. }
  66.  
  67. // equals/hashCode/toString >>
  68. public String toString() {
  69. String res = "";
  70. res += getClass().getName() + "@" + hashCode() + " : ";
  71. res += "number=" + number + ", ";
  72. res += "doorNum=" + doorNum + ", ";
  73. res += "wheelNum=" + wheelNum + ", ";
  74. res += "seatNum=" + seatNum + ", ";
  75. res += "maxSpeed=" + maxSpeed + ", ";
  76. res += "enginePosition=" + engine + ", ";
  77. res += "color=" + color + "";
  78. return res;
  79. }
  80.  
  81. public int hashCode() {
  82. int result = 17;
  83. result = 37 * result + number.hashCode();
  84. result = 37 * result + doorNum;
  85. result = 37 * result + wheelNum;
  86. result = 37 * result + maxSpeed;
  87. result = 37 * result + seatNum;
  88. result = 37 * result + engine;
  89. result = 37 * result + color;
  90. return result;
  91. }
  92.  
  93. public boolean equals(Object o) {
  94. if (!(o instanceof Car)) return false;
  95.  
  96. Car other = (Car) o;
  97.  
  98. return (other.number == number &&
  99. other.doorNum == doorNum &&
  100. other.maxSpeed == maxSpeed &&
  101. other.seatNum == seatNum &&
  102. other.wheelNum == wheelNum &&
  103. other.engine == engine &&
  104. other.color == color);
  105. }
  106.  
  107. public void InstallEngine(int position) {
  108. if (position < 0) return;
  109.  
  110. engine = position;
  111. }
  112.  
  113. public void setColor(int color) {
  114. this.color = color;
  115. }
  116.  
  117. public Object clone() {
  118. try {
  119. return super.clone();
  120. throw new Error(getClass().getName() + " clone error!");
  121. }
  122. }
  123. }
  124.  
Сущность AbstractProduct
Абстракция легкового автомобиля
Тестировалось на: java 1.5.0_04

+добавить реализацию