Solution: Factory Method
Explore the factory method design pattern by learning to create cloneable objects in C++ using std::shared_ptr. Understand how to refactor code for flexible window objects and create a perfect factory method that returns clones of various types. This lesson helps you grasp object-oriented design principles critical for scalable software development.
We'll cover the following...
We'll cover the following...
Solution to the first problem
Here is the refactored code using std::shared_ptr.
Code explanation
-
Lines 4–8: We created a
Windowbase class with one purevirtualmethodclone()with astd::shared_ptrreturn type. -
Lines 11–20: We created a
DefaultWindowclass derived from theWindowparent class. We created a constructor with no parameters, ...