05- SW Design - Design Patterns - Part2
05- SW Design - Design Patterns - Part2
▪ Summary
▪ Then you have to go through all the fields of the original object and
copy their values over to the new object
▪ Some of the object’s fields may be private and not visible from
outside of the object itself.
▪ This interface lets you clone an object without coupling your code
to the class of that object.
▪ To make it special, they request the seller to write, “Happy Birthday Ron” on the cake.
▪ He already defined the model and produces many cakes (which all look the same)
every day by following the same process,
▪ The constructor must copy the values of all fields defined in the class
from the passed object into the newly created instance.
▪ Having this code in a regular constructor is safer because the resulting object is
returned fully configured right after you call the new operator.
▪ The cloning method usually consists of just one line: running a new
operator with the prototypical version of the constructor.
▪ The Factory Method separates product construction code from the code
that actually uses the product. Therefore it’s easier to extend the product
construction code independently from the rest of the code.
▪ For example, to add a new product type to the app, you’ll only need to
create a new creator subclass and override the factory method in it.
▪ Vendors update their catalogs very often, and you wouldn’t want to change the core
code each time it happens.
▪ Moreover, it’s your job to make sure that the UI elements match the style of the
current operating system.
▪ You wouldn’t want your program to render macOS controls when it’s executed in Windows.
▪ The Abstract Factory interface declares a set of creation methods that the client code
can use to produce different types of UI elements.
▪ When an application launches, it checks the type of the current operating system.
▪ The app uses this information to create a factory object from a class that matches the operating system.
▪ The code uses this factory to create UI elements. This prevents the wrong elements from being created.
▪ The client code doesn’t depend on concrete classes of factories and UI elements as long as it works with
these objects via their abstract interfaces.
▪ This also lets the client code support other factories or UI elements that you might add in the future.