From the course: Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

A simple factory

A simple factory

From the course: Design Patterns: Creational

Start my 1-month free trial

A simple factory

- [Narrator] The way we deal with situations like this is to use a factory pattern. With factory patterns, we take all the code that decides what concrete object to make and encapsulate that code in a factory. The simplest type of factory pattern we can use is the simple factory pattern. The simple factory pattern is not one of the gang of four patterns, but it can help us understand the two factory patterns that are. So, we'll begin with a simple factory and go from there. With the simple factory pattern, we move the code that's responsible for making a product out of the main part of the code and into a separate factory object. That product could be a duck object, a pizza object, or perhaps a type of document. For this duck example, we move all of the code that decides which type of duck is appropriate. A mallard duck, a decoy duck, or a rubber duck from the client code to a simple factory. Then the client uses the…

Contents