Factory Method (Chapter 16)
SOLUTION 16.1 | Many answers are possible, but toString() is probably the most commonly used method that creates a new object. For example, the following code creates a new String object:
String s = new java.util.Date().toString(); The creation of strings often happens behind the scenes. Consider: System.out.print(new Date()); This method creates a String object from the Date object, ultimately by calling the toString() method of class Date. It is interesting to walk through this in debugger, to see the details of what happens within a common print() method. Another method that creates a new object is clone(). You may discover other methods that are “factories” for creating objects without necessarily exemplifying ... |
Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.