/*******************************************************************************
* 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;
/**
* A CardSource decorator that logs cards drawn from the
* source.
*/
public class LoggingDecorator implements CardSource {
CardSource aSource;
public LoggingDecorator( CardSource pSource ) {
aSource = pSource;
}
@Override
public Card draw() {
Card card = aSource.;
System.out.(card);
return card;
}
@Override
public boolean isEmpty() {
return aSource.isEmpty();
}
@Override
public CardSource copy() {
return new LoggingDecorator(aSource.copy());
}
}
The field is not declared final
to allow cloning, as done
in method clone()
. Otherwise, it would be better to declare this
field final
.
The field is not declared final
to allow cloning, as done
in method clone()
. Otherwise, it would be better to declare this
field final
.
The decorator delegates the call to the draw()
method
of it's decorated source.
The decorator delegates the call to the draw()
method
of it's decorated source.
This is the decoration: the extra behavior on top of the original behavior (which was delegated back to the original source).
print(String)
and then
println()
.This is the decoration: the extra behavior on top of the original behavior (which was delegated back to the original source).
print(String)
and then
println()
.x
- The Object
to be printed.Chapter 6, insight #4
Use the Decorator when you need to add functionality to certain objects, while being able to use them in place of regular objects
Chapter 6, insight #4
Use the Decorator when you need to add functionality to certain objects, while being able to use them in place of regular objects
Console.charset()
if the Console
exists,
stdout.encoding otherwise.
Console.charset()
if the Console
exists,
stdout.encoding otherwise.
For simple stand-alone Java applications, a typical way to write a line of output data is:
System.out.println(data)
See the println
methods in class PrintStream
.
System
class contains several useful class fields
and methods. It cannot be instantiated.
Among the facilities provided by the System
class
are standard input, standard output, and error output streams;
access to externally defined properties and environment
variables; a means of loading files and libraries; and a utility
method for quickly copying a portion of an array.System
class contains several useful class fields
and methods. It cannot be instantiated.
Among the facilities provided by the System
class
are standard input, standard output, and error output streams;
access to externally defined properties and environment
variables; a means of loading files and libraries; and a utility
method for quickly copying a portion of an array.