First launch a case. Some people use fast food and chefs, I think it is good. Question: Now I have to do two packages, the northerners of the northerners and the flavor of the South, each package must have face, soup and The meal, but the northerners eat, soups and southerners are different. The northerners want to be spicy, the South is as light, we will define these objects, in turn: Northerners package: Northnuddle, northrice Northsoap; Southerners package is: sourthnuddle, southrice, southsoap; before we didn't introduce this method, we have to get a northerner package; Northnuddle nn = new northuddle ();
Northrice nr = new northrice ();
Northsoap ns = new northsoap ();
Southern Package:
I will not write.
Here everyone discovering problems, if you write, the code of the South is almost the same.
Then we want to solve this problem now.
1. Build a method to make this method (use strings or shaping to do parameters, package these methods into a function)
Makefood (String Package)
IF package == 'North'
{northuddle nn = new northuddle ();
Northrice nr = new northrice ();
Northsoap ns = new northsoap ();
...... ..
This is what we usually think of, but this method is a simple factory processing method, using a specific parameter to pass the value, has a better way, of course.
It is to take the chef as an interface: because what we have to do is doing three, cooking, do soup, and do a nail.
This action, two flavor chefs must be done, this is common.
So there is a chef interface.
Public Interface Chef
{
Noodle: makeenoodle;
Rice: makerice;
SOAP: MAKESOAP;
Use two subclasses, northern chefs and, southern chefs to inherit it to achieve these actions.
Class northchef: Chef;
Public manetoddle ()
{RETURN = new manetoddle ();
}
Class sourthchef: chef;
Then go to the northern noodles or southern noodles in the Makenoodle function.
We will give the package (chef) this abstract factory. As for it, we don't have to manage.
Anyway, the northern chef made a northern package, the South Master's dimers made a package.
The way the call is called.
Chef northChef = new NorthChef (); northChef.MakeNoodle (); northChef.MakeRice (); northChef.MakeBread (); Chef southChef = new SouthChef (); southChef.MakeBread (); southChef.MakeNoodle (); southChef.MakeRice ( );