About Question enthuware.ocpjp.v7.2.1097 :
Posted: Mon Jan 25, 2016 4:49 am
TL;DR: The answers are phrased as if AdvancedReporter and SpecialReporter should be left unchanged, but the question explicitly asks about extracting functionality, i.e. some degree of refactoring. The example in the explanation does not take this into acount.
Full analysis of answers:
ReporterFactory: Wrong because we are not extracting functionality as requested. Also wrong because we are not using composition as requested.
On the composition side, one could argues that classes are factories and ReporterFactory composes AdvancedReporter.class and SpecialReporter.class; however, the purpose of the exam is to test the knowledge of standard definitions, not his ability to think outside the box.
CommonReporter with generateTextReport: Correct though the class name is a misnomer, it should be TextReporter. But then we are not asked to find the best design, because in that case we'd be asked about improving the names of AdvancedReporter and SpecialReporter; we are asked to apply a specific kind of refactoring.
CommonReporter with generatePDFReport and generateHTMLReport: Wrong because both AdvancedReporter and SpecialReporter have the features that distinguish them extracted out and are left with just generateTextReport, which is pure nonsense. Also, CommonReporter does not use composition for PDF and HTML generation but contains the code directly.
This answer would be correct if the question didn't talk about extracting functionality. Even then, it would be incomplete because it leaves out how to deal with generateTextReport.
Creating a common superclass fulfils the "extract functionality" requirement. What is not mentioned is that we also need a Reporter class that delegates to either CommonReporter, Reporter, AdvancedReporter, or SpecialReporter. Now one could consider this answer as too incomplete to be the correct one, but then all the other answers are either plain wrong, or incomplete in some other way.
From the PoV of "which answer is least incomplete", answer 2 would be correct.
From the PoV of "which answers describe at least a part of a working solution that involves extraction of functionality", answers 2 and 4 are correct.
Did I miss some point?
Full analysis of answers:
ReporterFactory: Wrong because we are not extracting functionality as requested. Also wrong because we are not using composition as requested.
On the composition side, one could argues that classes are factories and ReporterFactory composes AdvancedReporter.class and SpecialReporter.class; however, the purpose of the exam is to test the knowledge of standard definitions, not his ability to think outside the box.
CommonReporter with generateTextReport: Correct though the class name is a misnomer, it should be TextReporter. But then we are not asked to find the best design, because in that case we'd be asked about improving the names of AdvancedReporter and SpecialReporter; we are asked to apply a specific kind of refactoring.
CommonReporter with generatePDFReport and generateHTMLReport: Wrong because both AdvancedReporter and SpecialReporter have the features that distinguish them extracted out and are left with just generateTextReport, which is pure nonsense. Also, CommonReporter does not use composition for PDF and HTML generation but contains the code directly.
This answer would be correct if the question didn't talk about extracting functionality. Even then, it would be incomplete because it leaves out how to deal with generateTextReport.
Creating a common superclass fulfils the "extract functionality" requirement. What is not mentioned is that we also need a Reporter class that delegates to either CommonReporter, Reporter, AdvancedReporter, or SpecialReporter. Now one could consider this answer as too incomplete to be the correct one, but then all the other answers are either plain wrong, or incomplete in some other way.
From the PoV of "which answer is least incomplete", answer 2 would be correct.
From the PoV of "which answers describe at least a part of a working solution that involves extraction of functionality", answers 2 and 4 are correct.
Did I miss some point?