adapter pattern

This commit is contained in:
Claudio Maggioni 2022-10-25 10:43:22 +02:00
parent a8e2eef258
commit 3db8bcca0c
2 changed files with 73 additions and 39 deletions

Binary file not shown.

View File

@ -269,39 +269,91 @@ namely:
\begin{description}
\item[TokenStreamFactory] which indeed is a factory for \textbf{JsonParser} and
\textbf{JsonGenerator} is a factory for JsonParser and JsonGenerator objects, although two overloaded factory methods exist on this class (one for each class) catering to a different combination of arguments.
A concrete implementation of this factory is included in the form of the \textbf{JsonFactory} class, although other modules may add additional implementations to cater to different encodings (like the \textit{jackson-dataformat-xml} module for XML);
\item[TSFBuilder] which is also a factory for concrete implementations of \textbf{TokenStreamFactory} allows slight changes in the serialization and deserialization rules (e.g., changing the quote character used in JSON keys from " to '). Like TokenStreamFactory, this class is only implemented by one class, JsonFactoryBuilder, within this module's scope. Moreover, as mentioned previously, this abstract factory will likely be extended by concrete implementations in other Jackson modules.
\end{description}
\textbf{JsonGenerator} is a factory for JsonParser and JsonGenerator objects,
although two overloaded factory methods exist on this class (one for each
class) catering to a different combination of arguments. A concrete
implementation of this factory is included in the form of the
\textbf{JsonFactory} class, although other modules may add additional
implementations to cater to different encodings (like the
\textit{jackson-dataformat-xml} module for XML);
\item[TSFBuilder] which is also a factory for concrete implementations of
\textbf{TokenStreamFactory} allows slight changes in the serialization and
deserialization rules (e.g., changing the quote character used in JSON keys from
" to '). Like TokenStreamFactory, this class is only implemented by one class,
JsonFactoryBuilder, within this module's scope. Moreover, as mentioned
previously, this abstract factory will likely be extended by concrete
implementations in other Jackson modules. \end{description}
\subsection{Builder Pattern}
The builder pattern does not seem to be analyzed by
\textit{Pattern4J}, as the analysis output does not mention the pattern, even to report that no instances of it have been found (as is the case with other patterns, e.g., the observer pattern).
A manual search in the source code produced the following results:
The builder pattern does not seem to be analyzed by \textit{Pattern4J}, as the
analysis output does not mention the pattern, even to report that no instances
of it have been found (as is the case with other patterns, e.g., the observer
pattern). A manual search in the source code produced the following results:
\begin{description}
\item[TSFBuilder] is also a builder other than an abstract factory. As mentioned previously, this class allows slightly altering the serialization and deserialization rules used to build outputting JsonFactory objects. Each rule is represented by an object or enum instance implementing the util.JacksonFeature interface. TSFBuilder then provides several overloaded methods to enable and disable features represented by the interface. Enabled features are stored in several bitmask-protected int fields, which are then directly accessed by the constructor of the TokenStreamFactory concrete implementation to build;
\marginpar[right text]{\color{white}\url{https://youtu.be/72b2nH-kdbU}}
\item[JsonFactoryBuilder] is a concrete factory implementation of \textbf{TSFBuilder} that builds \textbf{JsonFactory} instances;
\item[util.ByteArrayBuilder] provides facilities to build \textit{byte[]} objects
of varying length, akin to \textbf{StringBuilder} building \textbf{String}
objects. This is not a strict implementation of the builder pattern per se
(as Java arrays do not have a ``real'' constructor),
but it is nevertheless included since the features it exposes (namely
dynamic sizing while building) are decoupled by the underlying (fixed-size)
array representation.
\item[TSFBuilder] is also a builder other than an abstract factory. As mentioned
previously, this class allows slightly altering the serialization and
deserialization rules used to build outputting JsonFactory objects. Each
rule is represented by an object or enum instance implementing the
util.JacksonFeature interface. TSFBuilder then provides several overloaded
methods to enable and disable features represented by the interface. Enabled
features are stored in several bitmask-protected int fields, which are then
directly accessed by the constructor of the TokenStreamFactory concrete
implementation to build; \marginpar[right
text]{\color{white}\url{https://youtu.be/72b2nH-kdbU}}
\item[JsonFactoryBuilder] is a concrete factory implementation of
\textbf{TSFBuilder} that builds \textbf{JsonFactory} instances;
\item[util.ByteArrayBuilder] provides facilities to build \textit{byte[]}
objects of varying length, akin to \textbf{StringBuilder} building
\textbf{String} objects. This is not a strict implementation of the builder
pattern per se (as Java arrays do not have a ``real'' constructor), but it
is nevertheless included since the features it exposes (namely dynamic
sizing while building) are decoupled by the underlying (fixed-size) array
representation.
\end{description}
\section{Creational Patterns}
\subsection{Adapter Pattern}
TBD
\subsection{Adapter Pattern}
\textit{Pattern4J} found many instances of the adapter pattern, however all but
one wVere shown to be false positives by checking the documentation and the code
using each allegied adaptee. The matches found are reported and commented
below. Matches are shown in the \textit{[Adapter] $\leftarrow$ [Adaptee]}
format.
\begin{description}
\item[JsonFactory $\leftarrow \{$ sym.ByteQuadsCanonicalizer,
io.InputDecorator $\}$] false positives, by reading the documentation it is
clear the classes have different purposes and \textbf{JsonFactory} is merely
using the other classes' functionality through composition;
\item[base.ParserBase $\leftarrow$ json.JsonReadContext] false positive,
\textbf{json.JsonReadContext} is instantiated several times in
\textbf{base.ParserBase} by mutating the container field with the new
instances;
\item[base.ParserBase $\leftarrow$ json.JsonWriteContext] false positive,
like above;
\item[$\{$ util.DefaultPrettyPrinter, util.MinimalPrettyPrinter $\}
\leftarrow$ util.Separators] false positives, another example of instances
used through composition;
\item[io.SerializedString $\leftarrow$ io.JsonStringEncoder] indeed an adapter
pattern application, although the adaptee backing field is \textit{private
static final}. \textbf{io.SerializedString} is a class that wraps a String
and allows it to be encoded using the \textbf{io.JsonStringEncoder} static
instance, storing the result and re-using it in case of multiple
serialization request (a tecnique similar to memoization). Therefore, the
main purpose of this adapter is not to adapt against any interface, but to
wrap the functionality of the adaptee and store its results for re-use;
\item[util.DefaultPrettyPrinter $\leftarrow$
util.DefaultPrettyPrinter\$Indenter (2 fields)] false positives, both
enclosed fields are simply used in a composition relationship.
\end{description}
\subsection{TBD Decorator Pattern}
Decorator pattern lets you dynamically change the behavior of an object at run time by wrapping them in an object of a decorator class.
Decorator pattern lets you dynamically change the behavior of an object at run
time by wrapping them in an object of a decorator class.
(com.fasterxml.jackson.core omitted for brevity)
\begin{description}
\item[JsonGenerator]
TBD
@ -313,22 +365,7 @@ Only in Pattern4J
\subsection{Bridge Pattern}
TBD
\subsection{Composite Pattern}
None found
\subsection{Facade Pattern}
TBD -- \textit{Pattern4J} does not detect this pattern
\subsection{Proxy Pattern}
None found
\section{Behavioral Patterns}
\subsection{Command Pattern}
None found
\subsection{Observer Pattern}
None found
\subsection{State Pattern}
Among the design patterns \textit{Pattern4J} detects, the state pattern is
@ -417,8 +454,5 @@ Here the pattern is slightly modified by providing a default implementation of
override the method with a body first calling \textit{super()} and then adding
additional buffer release code after.
\subsection{Visitor Pattern}
None found
\end{document}