Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

Decorator (Chapter 27).............................................................................................................................


SOLUTION 27.1....................................................................................................................................


One answer is:


Writer out = new PrintWriter(System.out);
out = new WrapFilter(new BufferedWriter(out), 15);
((WrapFilter) out).setCenter(true);
out = new RandomCaseFilter(out);


Alternatively:


WrapFilter out =
new WrapFilter(
new BufferedWriter(
new RandomCaseFilter(
new PrintWriter(System.out))),
15);
out.setCenter(true);


The com.oozinoz.applications package includes classes ShowFilters2 and
ShowFilters3, which apply these two methods, respectively:


package com.oozinoz.applications;
import java.io.;
import com.oozinoz.io.
;
public class ShowFilters3
{
public static void main(String args[])
throws IOException
{
BufferedReader in =
new BufferedReader(new FileReader(args[0]));
WrapFilter out =
new WrapFilter(
new BufferedWriter(
new RandomCaseFilter(
new PrintWriter(System.out))),
15);
out.setCenter(true);


while (true)
{
String s = in.readLine();
if (s == null)
{
break;
}
out.write(s + "\n");
}
out.close();
in.close();
}
}

Free download pdf