Паттерн: Стратегия (Strategy)
Исходник: User.java, язык: java [code #522, hits: 7533]
автор: this [добавлен: 07.10.2007]
  1. package strategy;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6.  
  7. public class User {
  8.  
  9. /**
  10. * Форматтер - статичный, ибо ни к чему дублировать его
  11. * для каждого пользователя
  12. */
  13. private static final DateFormat df = new SimpleDateFormat("dd.MM.yyyy");
  14.  
  15. private int ID;
  16. private String fio;
  17. private Date regDate;
  18.  
  19. public User(int id, String fio, Date date) {
  20. super();
  21. ID = id;
  22. this.fio = fio;
  23. this.regDate = date;
  24. }
  25.  
  26. public String getFio() {
  27. return fio;
  28. }
  29.  
  30. public int getID() {
  31. return ID;
  32. }
  33.  
  34. public Date getRegDate() {
  35. return regDate;
  36. }
  37.  
  38. public int hashCode() {
  39. final int PRIME = 31;
  40. int result = 1;
  41. result = PRIME * result + ID;
  42. result = PRIME * result + ((fio == null) ? 0 : fio.hashCode());
  43. result = PRIME * result + ((regDate == null) ? 0 : regDate.hashCode());
  44. return result;
  45. }
  46.  
  47. public boolean equals(Object obj) {
  48. if (this == obj)
  49. return true;
  50.  
  51. if (!(obj instanceof User))
  52. return false;
  53.  
  54. final User other = (User) obj;
  55. if (ID != other.ID)
  56. return false;
  57. if (fio == null) {
  58. if (other.fio != null)
  59. return false;
  60. } else if (!fio.equals(other.fio))
  61. return false;
  62. if (regDate == null) {
  63. if (other.regDate != null)
  64. return false;
  65. } else if (!regDate.equals(other.regDate))
  66. return false;
  67. return true;
  68. }
  69.  
  70. public String toString() {
  71. return super.toString() + " |ID=" + ID
  72. + "|fio=" + fio + "|regDate="
  73. + df.format(regDate);
  74. }
  75.  
  76. }
  77.  
Класс, инкапсулирующий данные пользователя.
Тестировалось на: java 1.5.0_04

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