Have you ever looked at a piece of code, project or application and thought “what on earth is going on here”? I have many times and so have many other developers, so ages ago, Design Patterns were introduced to keep coding clean, and organised.
You probably organise your code already closely to a few design patterns without even realising. Today, I’ll introduce the Factory Pattern as a method of structuring your code.
The Factory Pattern allows objects of similar logic to be created to a common interface.
Tutorialspoint has a great example involving shapes.
Summarising the example, you can have a shape factory that creates triangles, squares, circles.
This may seem rather abstract, but for example, you may want to make different types of database connection for customers and want to get their details, which they would have a Customer interface, but a Customer Factory for all the individual details so that they are not exposed to the client.