Паттерн: Хранитель (Memento)
Исходник: OrderForm.java, язык: java [code #514, hits: 6713]
автор: this [добавлен: 05.10.2007]
  1. package memento;
  2.  
  3. import java.util.Date;
  4.  
  5. public class OrderForm {
  6. private int clientID;
  7. private int productID;
  8. private String clientComment;
  9. private Date orderDate;
  10.  
  11. private OrderState state;
  12.  
  13. /**
  14. * Класс Memento применительно к данной
  15. * сущности OrderForm
  16. */
  17. private class OrderState {
  18. private String comment;
  19. private int productID;
  20.  
  21. public OrderState(String comment, int productID) {
  22. super();
  23. this.comment = comment;
  24. this.productID = productID;
  25. }
  26.  
  27. public String getComment() {
  28. return comment;
  29. }
  30.  
  31. public int getProductID() {
  32. return productID;
  33. }
  34.  
  35. }
  36.  
  37. public OrderForm(int clientID, int productID, String clientComment, Date orderDate) {
  38. super();
  39. this.clientID = clientID;
  40. this.productID = productID;
  41. this.clientComment = clientComment;
  42. this.orderDate = orderDate;
  43. }
  44.  
  45. public void UpdateOrder(String comment, int prodID) {
  46. state = new OrderState(clientComment, productID);
  47.  
  48. /* Изменения >> */
  49. setClientComment(comment);
  50. setProductID(prodID);
  51. }
  52.  
  53. /**
  54. * Восстанавливаем величины
  55. */
  56. public void RollbackChanges() {
  57. setClientComment(state.getComment());
  58. setProductID(state.getProductID());
  59. }
  60.  
  61.  
  62. /* Getters / Setters >> */
  63. public String getClientComment() {
  64. return clientComment;
  65. }
  66.  
  67. public void setClientComment(String clientComment) {
  68. this.clientComment = clientComment;
  69. }
  70.  
  71. public int getProductID() {
  72. return productID;
  73. }
  74.  
  75. public void setProductID(int productID) {
  76. this.productID = productID;
  77. }
  78.  
  79. public int getClientID() {
  80. return clientID;
  81. }
  82.  
  83. public Date getOrderDate() {
  84. return orderDate;
  85. }
  86.  
  87. }
  88.  
Сущность Originator

Форма заказа, выбранный продукт и комментарий хранятся в виде хранителя.
Тестировалось на: java 1.5.0_04

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