common.tex 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. \section[General Design Aspects]{General Design Decisions and Aspects of \salespoint{}}
  2. %TODO add something about early fail and null-check exceptions in ctors
  3. This chapter summarises design decisions and aspects common to all \salespoint{} subsection and details, why those decisions were made.
  4. \subsection{Notes on Interfaces ...\protect\footnote{continued in the next sub-section}}
  5. Integrating a persistence layer into the \salespoint{} Framework had a great impact on some design decisions made during the development of \salespoint{}.
  6. Early on it became obvious that necessities of JPA could dictate the design and implementations of \salespoint{}.
  7. To guard against JPA requirements influencing design decisions, \salespoint{} strongly follows the \textit{programming against interfaces} programming style.
  8. Although, creating an interface for almost every class violates the \textit{KISS} (Keep it simpe, Stupid! Also, a hard-rock band.) principle, the developers deemed programming against interfaces necessary because \salespoint{} is intrinsically tied to JPA.
  9. Using interfaces allowed us to cleanly define the behaviour of an object, without relying on a specific implementation.
  10. The classes itself are, however, not programmed against interfaces.
  11. \salespoint{} usually just implement interfaces, but refer to other classes directly.
  12. The reason for this violation of the \textit{programming against interfaces} paradigm is the generic typing, which would be required, if \salespoint{} classes would refer to interfaces instead of concrete classes.
  13. For example, the \code{PersistentOrder} class would require three (cascaded) generic type parameters alone.
  14. As a consequence, \salespoint{} classes refer to each other directly, instead of interface types.
  15. \subsection{... implementing Classes, and Naming\protect\footnote{started at the preceeding sub-section}}
  16. Objects, which need to be persisted to safe the current state of an application, are called persistence entities.
  17. Usually, a persistence entity is a \textit{Plain Old Java Object} (POJO).
  18. %Separating behaviour and implementation is employed throughout the framework.
  19. However, Java interfaces are used to separate behaviour and implementation.
  20. An interface defines only the behaviour of an object.
  21. Every persistence entity class is an implementation of a corresponding Java interface, to avoid JPA requirements to impact on design decision and, for example, influence the \salespoint{} API.
  22. Each persistence entity has an aggregating class, which also implements an interface.
  23. The interface of an aggregating class specifies its API, but not implementation details.
  24. %Each persistence entity has a manager class, which in turn is an implementation of a manager interface.
  25. %Each persistence entity has an aggregating class, which handles JPA access transparently
  26. Aggregating classes and their respective interfaces are also called manager classes (interfaces), for example \code{UserManager} and \code{OrderManager}.
  27. Sometimes, class and interface names deviate from this naming scheme, for example \code{Calendar}, \code{Inventory} or \code{Accountancy}.
  28. The reason for this break in naming scheme is clarity: because a \code{Calendar} aggregates \code{CalendarEntry}s\footnote{
  29. The correct plural form would be \code{CalendarEntries}, which is not a type name.
  30. The form "\code{CalendarEntry}s" is used to support full text search.
  31. Futhermore, "\code{CalendarEntry}s" should be read as "\code{CalendarEntry} objects" or "objects of the type \code{CalendarEntry}"},
  32. its name according to the scheme would be \code{CalendarEntryManager}.
  33. Everybody knows what a \code{Calendar} is for, but not necessarily what a \code{CalendarEntryManager} does.
  34. Therefore, a more descriptive name was chosen.
  35. The specific manager implementations included \salespoint{} facilitate storing objects to a database.
  36. %However, it is possible to implement every persistence entity and manager class in \salespoint{} non-persistent, for example collection-based.
  37. However, an implementation based on Java collections rather than a database is entirely possible.
  38. \subsection{Why \salespoint{} is so developer-friendly}
  39. \salespoint{} is designed to be developer-friendly.
  40. A crucial part of its easy-to-use feeling is the consistency of interfaces, persistence entities and managers across the framework, including, but not limited to the naming of methods and behaviour of managers.
  41. All aggregating classes share a set of methods, namely \code{add}, \code{get}, \code{contains}, \code{remove} and \code{find}.
  42. The methods have the same semantics on every manager and have a similar method signature.
  43. Concise method names speed up development by reducing typing overhead.
  44. Instead of having an \code{addUser} method for the user manager or an \code{addOrder} method for the order manager, all managers have a method just named \code{add}.
  45. Consistency in the API is achieved by similar method signature.
  46. Consider the \code{get} method: it takes only one parameter which is an identifier.
  47. The precise type of the identifier is easily guessed: the order manager requires an \code{OrderIdentifier} and the \code{Accountancy} an \code{AccountancyEntryIdentifier}.
  48. The name of the identifier type is derived from the name of class which is aggregated by the manager.
  49. This consistency allows a developer to use an unknown manager, when he is familiar with another manager.
  50. \salespoint{} does not contain checked exceptions, thus avoiding ``the handcuffs they put on programmers''~\cite{checked-exceptions}.
  51. \label{iterable}
  52. In Java 1.5 a new interface was introduced, the \code{Iterable} interface.
  53. The \code{Iterable} is implemented by classes, which aggregate objects and allow to iterate over those objects.
  54. The well known \code{Collection} interface is now a sub-interface of \code{Iterable}.
  55. An object implementing the \code{Iterable} interface is immutable in contrast to sub-interfaces of \code{Collection}, for example the \code{List} interface.
  56. \code{Iterable}s are easy to handle, because compiler support to use the \code{foreach} construct is available.
  57. Also, there is no reason for a programmer to touch an iterator by himself because it is not idiomatic language use.
  58. In \salespoint{} \code{Iterable}s are return on \code{find()} methods.
  59. Having an \code{Iterable} signals the developer, that he may not modify the object.
  60. Although an \code{Iterable} may be converted to a \code{List} or \code{Set}, changes to the resulting object, are not reflected in the original \code{Iterable} object.
  61. Thus, it has to be noted, that objects \textbf{cannot} be added by modifying a \salespoint{} return value.
  62. The proper \code{add()} or \code{addAll()} methods have to be used.
  63. \subsection{Type-based queries in JPA}
  64. \label{jpa-types}
  65. The \code{get()} and \code{find()} methods of aggregating classes in \salespoint{} have a type parameter of \code{Class<E>}.
  66. The type \code{E} is a sub-class of type \code{T}, the generic type parameter of the interface.
  67. For example, the \code{Catalog} interface is generically typed to \code{<T~extends~Product>}, where \code{Product} is an interface itself.
  68. The complete type is thus \code{Catalog<T~extends~Product>}.
  69. \code{PersistentCatalog} implements the generic \code{Catalog} interface with \code{Catalog<PersistentProduct>}.
  70. Thus, \code{PersistentCatalog} aggregates instances of \code{PersistentProduct} or sub-classes thereof.
  71. When \code{Catalog} is queried for \code{Product}s using the \code{get()} and \code{find()} methods, a type parameter has to be supplied.
  72. This type parameter has two purposes: first, it ensures type-safety, and second it narrows the result set.
  73. When a certain type, for example \code{T~extends~PersistentProduct}, is requested from an aggregating class, let's say the \code{PersistentCatalog}, the return type is \code{T} for the \code{get()} method and \code{Iterable<T>}\footnote{More on \code{Iterable}s in sub-section~\ref{iterable}.} for the \code{find()} method.
  74. Thus, the return type depends on the request-type and is not a static type, for example \code{PersistentProduct}.
  75. The dependency of the return type on the requested type avoids type casts, which can fail at run-time and therefore increases type-safety.
  76. Because JPA is aware of persistence entities types, the result is always of the correct type, or empty, if no matching object could be found.
  77. The result set determined by the requested type, because the type parameter does not request a specific type, but rather has \code{instanceof} behaviour.
  78. That means, a type parameter does not only match to objects of the same type, but also all objects which have the type of a sub-class of the requested type.
  79. It does not match super-classes of the requested type.
  80. The VideoShop-tutorial\footnote{\url{http://www.st.inf.tu-dresden.de/SalesPoint/v5.0/wiki/index.php/VideoShop}} is an example of how this functionality can be used.
  81. The \code{VideoCatalog} extends \code{PersistentCatalog}.
  82. By using the type parameters \code{Dvd.class} and \code{BluRay.class}, it requires only one line of code to find all products of a certain kind (type).
  83. \begin{figure}
  84. \centering
  85. \includegraphics[width=0.8\textwidth]{images/type_matches.eps}
  86. \label{matches}
  87. \caption[Example for a type hierarchy.]{Exemplary class hierarchy of \code{PersistentAccountancyEntry}s.}
  88. \end{figure}
  89. \code{PersistenceAccountancyEntry.class} matches all class types.
  90. Consider Figure~\ref{matches} for another example, explaining the \code{instanceof} semantics.
  91. \code{PersistentAccountancy} aggregates \code{PersistentAccountancyEntry}s and sub-classes thereof.
  92. One sub-class is already supplied by \salespoint{}: \code{ProductPaymentEntry}.
  93. \code{ProductPaymentEntry}s are automatically created, if an order is completed.
  94. If \code{ProductPaymentEntry.class} is supplied as type parameter, only objects of type \code{ProductPaymentEntry} would be returned, if any.
  95. Using \code{FooEntry.class} as type parameter would return objects of type \code{FooEntry} and objects of type \code{BarEntry}, if any.
  96. %To access entries of only one type, thus belonging to one ``account'', \code{get()} and \code{find()} methods in \code{Accountancy} have a type parameter.