0

i have been trying to get my head round factory patterns and dependency injection and i understand the concepts and basics behind both patterns and that there can be a slight cross over. But before i start coding it up, i just want to check my flow method would be correct.

My intended flow would be...

  1. create a config file with all the properties needed for my 'core classes' in the format $config['core.classname']['property_name']=$value;

  2. create a factory class that will create an instance of all my core classes and run through the config file injecting the properties in to each class

  3. when my app needs an instance of a class, it uses the factory class to clone the required class which has had its dependencies injected.

As i understand it this would decouple my core classes, allowing for them to be swapped in and out of my code easier.

1 Answer 1

2

What you are calling a 'factory' is really more of a dependency injection container. A factory traditionally only creates one type of object.

In general you should avoid creating any core instances until your app actually needs one. You may have 100 core classes defined of which any given app request might only need a couple.

In many cases your app will want to share the same instance or a core class so automatic cloning is probably not quite what you want.

Consider reading through the Service (what you call core) chapter in the Symfony2 framework package for ideas: http://symfony.com/doc/current/book/service_container.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.