Common Creational Patterns - Quiz Explanation
The correct answers are indicated below, along with the text that explains the correct answers.
1.
Suppose you need to create new objects by copying an existing object. Which of the following patterns would you most likely use?
Please select the best answer.
A.
Abstract Factory
B.
Builder
C.
Prototype
D.
Singleton
The correct answer is C.
The pattern you would most likely use to create new objects by copying an existing object is the Prototype pattern. This is the definition of the Prototype pattern.
2.
Suppose you need to define an interface for creating instances of several related abstract classes without specifying their concrete subclasses. Which of the following patterns would you most likely use?
Please select the best answer.
A.
Abstract Factory
B.
Builder
C.
Prototype
D.
Factory
The correct answer is A.
To define an interface for creating instances of several related abstract classes without specifying their concrete subclasses you would most likely use the Abstract Factory pattern. This is the definition of the Abstract Factory pattern.
3.
Suppose you need to define an abstract interface for creating a complex object from its component parts, but allow different subclasses to create different representations of the parts? Which of the following patterns would you most likely use?
Please select the best answer.
A.
Abstract Factory
B.
Builder
C.
Prototype
D.
Factory
The correct answer is B.
To define an abstract interface for creating a complex object from its component parts, but allow different subclasses to create different representations of the parts, the pattern you would most likely use is the Builder pattern. This is the definition of the Builder pattern.
4.
The
java.awt.Toolkit
class is an example of which pattern:
Please select the best answer.
A.
Abstract Factory
B.
Builder
C.
Prototype
D.
Factory Method
The correct answer is A.
The
java.awt.Toolkit
class is an Abstract Factory class that is used to create native peers for graphical user interface components.