Паттерн: Абстрактная фабрика (Abstract Factory)
Исходник: Truck.java, язык: java [code #401, hits: 8718]
автор: this [добавлен: 24.05.2007]
  1. package abstractFactory;
  2.  
  3. public abstract class Truck extends Car {
  4. private int maxLoad;
  5. private boolean trailer;
  6.  
  7. public Truck( String number,
  8. int doorNum, int wheelNum,
  9. int seetNum, int maxSpeed,
  10. int maxLoad, boolean trailer) {
  11.  
  12. super(number, doorNum, wheelNum, seetNum, maxSpeed);
  13. this.maxLoad = maxLoad;
  14. this.trailer = trailer;
  15. }
  16.  
  17. public int getMaxLoad() {
  18. return maxLoad;
  19. }
  20.  
  21. public void setMaxLoad(int maxLoad) {
  22. this.maxLoad = maxLoad;
  23. }
  24.  
  25. public boolean isTrailer() {
  26. return trailer;
  27. }
  28.  
  29. public void setTrailer(boolean trailer) {
  30. this.trailer = trailer;
  31. }
  32.  
  33.  
  34. // equals/hashCode/toString >>
  35. public boolean equals(Object o) {
  36. if (!(o instanceof Car)) {
  37. return false;
  38. }
  39.  
  40. if (!(o instanceof Truck)) {
  41. return o.equals(this);
  42. }
  43.  
  44. Truck other = (Truck) o;
  45.  
  46. return (super.equals(o) &&
  47. other.trailer == trailer &&
  48. other.maxLoad == maxLoad);
  49. }
  50.  
  51. public int hashCode() {
  52. int res = super.hashCode();
  53. res = res * 37 + (trailer ? 1 : 0);
  54. res = res * 37 + maxLoad;
  55. return res;
  56. }
  57.  
  58. public String toString() {
  59. String res = super.toString();
  60. res += ", trailer=" + trailer;
  61. res += ", maxLoad=" + maxLoad;
  62. return res;
  63. }
  64.  
  65.  
  66.  
  67. }
Сущность AbstractProduct
Абстракция грузового автомобиля.
Тестировалось на: java 1.5.0_04

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