Паттерн: Абстрактная фабрика (Abstract Factory)
Исходник: GeneralMotorsTruck.java, язык: java [code #409, hits: 8449]
автор: this [добавлен: 24.05.2007]
  1. package abstractFactory.americanFleet;
  2.  
  3. import abstractFactory.Car;
  4. import abstractFactory.Truck;
  5.  
  6. public class GeneralMotorsTruck extends Truck implements Cloneable {
  7. private boolean diesel;
  8.  
  9. public GeneralMotorsTruck(String number,
  10. int wheelNum, boolean trailer, boolean diesel) {
  11. super(number, 2, wheelNum, 3, 160, 15000, trailer);
  12. this.diesel = diesel;
  13. }
  14.  
  15. public boolean isDiesel() {
  16. return diesel;
  17. }
  18.  
  19. //equals/hashCode/toString >>
  20. public boolean equals(Object o) {
  21. if (!(o instanceof Car)) {
  22. return false;
  23. }
  24.  
  25. if (!(o instanceof GeneralMotorsTruck)) {
  26. return o.equals(this);
  27. }
  28.  
  29. GeneralMotorsTruck other = (GeneralMotorsTruck) o;
  30.  
  31. return (super.equals(o) &&
  32. other.diesel == diesel);
  33. }
  34.  
  35. public int hashCode() {
  36. int res = super.hashCode();
  37. res = res * 37 + (diesel ? 1 : 0);
  38. return res;
  39. }
  40.  
  41. public String toString() {
  42. String res = super.toString();
  43. res += ", diesel=" + diesel;
  44. return res;
  45. }
  46. }
Сущность ConcreteProduct
Продукт, конкретная реализация: американский грузовик.
Тестировалось на: java 1.5.0_04

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