/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java",
* 2nd edition by Martin P. Robillard.
*
* Copyright (C) 2022 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*
*******************************************************************************/
package e2.chapter6;
/**
* Stub of an object that represents the state of a card game. This
* class is used to illustrate the use of the Prototype design pattern.
*/
public class GameModel {
private final CardSource aCardSourcePrototype;
// In a complete code base this field would be used.
@SuppressWarnings("unused")
private CardSource aCardSource;
public GameModel(CardSource pCardSourcePrototype) {
aCardSourcePrototype = pCardSourcePrototype;
newGame();
}
public void newGame() {
aCardSource = aCardSourcePrototype.copy();
}
}
Support for polymorphic copying is necessary to be able to apply the Prototype design pattern.
Support for polymorphic copying is necessary to be able to apply the Prototype design pattern.
Chapter 6, insight #9
Be careful when using Java's cloning mechanism, as it involves many error-prone steps
Chapter 6, insight #9
Be careful when using Java's cloning mechanism, as it involves many error-prone steps
The SuppressWarnings
annotation interface is applicable
in all declaration contexts, so an @SuppressWarnings
annotation can be used on any element. As a matter of style,
programmers should always use this annotation on the most deeply
nested element where it is effective. For example, if you want to
suppress a warning in a particular method, you should annotate that
method rather than its class.
The set of warnings suppressed in a given element is a union of
the warnings suppressed in all containing elements. For example,
if you annotate a class to suppress one warning and annotate a
method in the class to suppress another, both warnings will be
suppressed in the method. However, note that if a warning is
suppressed in a module-info
file, the suppression applies
to elements within the file and not to types contained
within the module. Likewise, if a warning is suppressed in a
package-info
file, the suppression applies to elements
within the file and not to types contained within the
package.
Java compilers must recognize all the kinds of warnings defined in the Java Language Specification (JLS section 9.6.4.5) which include:
"unchecked"
.
"deprecation"
.
"removal"
.
"preview"
.
javac
reference implementation recognizes compilation-related warning
names documented in its --help-lint
output.