Design Patters, Java and the Factory Pattern

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s