This confuses me, in the most simplest terms what does it do? Pretend you are explaining to your mother or someone almost please.
Answers
If you have the Martin Fowler's Refactoring book, just follow the "Change Unidirectional Association to Bidirectional" refactoring.
In case you don't have it, here's how your classes will look like after the refactoring:
class C
{
// Don't to expose this publicly so that
// no one can get behind your back and change
// anything
private List<W> contentsW;
public void Add(W theW)
{
theW.Container = this;
}
public void Remove(W theW)
{
theW.Container = null;
}
#region Only to be used by W
internal void RemoveW(W theW)
{
// do nothing if C does not contain W
if (!contentsW.Contains(theW))
return; // or throw an exception if you consider this illegal
contentsW.Remove(theW);
}
internal void AddW(W theW)
{
if (!contentW.Contains(theW))
contentsW.Add(theW);
}
#endregion
}
class W
{
private C containerC;
public Container Container
{
get { return containerC; }
set
{
if (containerC != null)
containerC.RemoveW(this);
containerC = value;
if (containerC != null)
containerC.AddW(this);
}
}
}
Take note that I've made the List<W>
private. Expose the list of Ws via an enumerator instead of exposing the list directly.
e.g. public List GetWs() { return this.ContentW.ToList(); }
The code above handles transfer of ownership properly. Say you have two instances of C -- C1 and C2 - and the instances of W -- W1 and W2.
W1.Container = C1;
W2.Container = C2;
In the code above, C1 contains W1 and C2 contains W2. If you reassign W2 to C1
W2.Container = C1;
Then C2 will have zero items and C1 will have two items - W1 and W2. You can have a floating W
W2.Container = null;
In this case, W2 will be removed from C1's list and it will have no container. You can also use the Add and Remove methods from C to manipulate W's containers - so C1.Add(W2) will automatically remove W2 from it's original container and add it to the new one.
Factories should be simple classes, usually static. They can also be implemented as static methods on the entity or value object they create. Factories should create domain objects directly and only domain objects. Moreover, factories should not be tied with dependency injection because domain objects shouldn't have dependencies injected into them.
Domain objects should not implement interfaces - that is a needless abstraction.
Services and repository implementations on the other hand do have dependencies and should be created by the DI container.
Never used any of those, but they look interesting..
Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..
A factory creates an object. So, if you wanted to build
You wouldn't want to rely on having to do the following code everytime you create the object
That is where the factory would come in. We define a factory to take care of that for us:
Now all we have to do is
The real advantage is when you want to change the class. Lets say we wanted to pass in a different ClassC:
or a new ClassB:
Now we can use inheritance to easily modify how the class is created, to put in a different set of classes.
A good example might be this user class:
In this class
$data
is the class we use to store our data. Now for this class, lets say we use a Session to store our data. The factory would look like this:Now, lets say instead we want to store all of our data in the database, it is really simple to change it:
Factories are a design pattern we use to control how we put objects together, and using correct factory patterns allows us to create the customized objects we need.