/*******************************************************************************
* 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;
import java.util.Optional;
/**
* Represents an abstract command in the Command design pattern.
*/
public interface Command
{
execute();
void ();
}
Chapter 6, insight #10
Polymorphic copying can also be used as a way to create fresh instances of objects whose type is not known at compile time, a technique captured by the Prototype pattern
Chapter 6, insight #10
Polymorphic copying can also be used as a way to create fresh instances of objects whose type is not known at compile time, a technique captured by the Prototype pattern
If possible, a void
return type is simple. However, if need be,
an optional is a way to manage potential return values by commands. However,
the choice of a return type constrains the behavior of commands, as in this case
they must either return a Card
or nothing (as opposed to returning a different type
of object).
null
value.
If a value is present, isPresent()
returns true
. If no
value is present, the object is considered empty and
isPresent()
returns false
.
If possible, a void
return type is simple. However, if need be,
an optional is a way to manage potential return values by commands. However,
the choice of a return type constrains the behavior of commands, as in this case
they must either return a Card
or nothing (as opposed to returning a different type
of object).
null
value.
If a value is present, isPresent()
returns true
. If no
value is present, the object is considered empty and
isPresent()
returns false
.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse()
(returns a default value if no value is present) and
ifPresent()
(performs an
action if a value is present).
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Optional
is primarily intended for use as a method return type where
there is a clear need to represent "no result," and where using null
is likely to cause errors. A variable whose type is Optional
should
never itself be null
; it should always point to an Optional
instance.This method is an illustration of extra methods than can be declared in
command interfaces. It should not be seen as a hard requirement on the same level
as having an execute
method.
This method is an illustration of extra methods than can be declared in
command interfaces. It should not be seen as a hard requirement on the same level
as having an execute
method.