A simple factory pattern
Create a factory class, which write method is responsible for creating objects. Provided to create an instance of the function, without concern for implementation. Pot is responsible for the plant and the caller together.
Product Interface:
package create.simplefactory; public interface Food { public void cook(); }
Specific products 1:
create.simplefactory Package; public class Jiucaijidan the implements Food { public void Cook () { the System. OUT .println ( " leek egg well " ); } }
Specific products 2:
create.simplefactory Package Penalty for; public class Xihongshijidan the implements Food { public void Cook () { System. OUT .println ( " tomato and egg well " ); } }
Factory class:
package create.simplefactory; public class FoodFactory { public Food createByName(String name) { if ("Jiucaijidan".equals(name)) { return new Jiucaijidan(); } else if ("Xihongshijidan".equals(name)) { return new Xihongshijidan(); } return null; } public Food createByClassname(String name) { try { if (name != null && !"".equals(name)) { return (Food) Class.forName(name).newInstance(); } } catch (Exception e) { e.printStackTrace(); } return null; } public Food createByClass(Class<? extends Food> clazz) { try { if (clazz != null) { return clazz.newInstance(); } } catch (Exception e) { e.printStackTrace(); } return null; } }
Test categories:
package create.simplefactory; public class Test { public static void main(String[] args) { Food f1 = new FoodFactory().createByName("Jiucaijidan"); f1.cook(); Food f2 = new FoodFactory().createByClassname("create.simplefactory.Xihongshijidan"); f2.cook(); Food f3 = new FoodFactory().createByClass(Xihongshijidan.class); f3.cook(); } }
Second, the factory pattern
The packaging of the above, the same type, associated together, create the interface above, use the factory outside through the interface. Let the plant completely become scapegoat Man.
Product Interface:
package create.factory; public interface Food { public void cook(); }
Specific products 1:
create.factory Package; public class Jiucaijidan the implements Food { public void Cook () { the System. OUT .println ( " leek egg well " ); } }
Specific products 2:
create.factory Package Penalty for; public class Xihongshijidan the implements Food { public void Cook () { System. OUT .println ( " tomato and egg well " ); } }
Factory interface:
package create.factory; public interface FoodFactory { public Food create(); }
Concrete Factory 1:
package create.factory; public class JiucaiFactory implements FoodFactory { public Food create() { return new Jiucaijidan(); } }
Concrete Factory 2:
package create.factory; public class XihongshiFactory implements FoodFactory { public Food create() { return new Xihongshijidan(); } }
Test categories:
package create.factory; public class Test { public static void main(String[] args) { FoodFactory ff1 = new XihongshiFactory(); FoodFactory ff2 = new JiucaiFactory(); Food f1 = ff1.create(); Food f2 = ff2.create(); f1.cook(); f2.cook(); } }
Third, the abstract factory pattern
Consider the emergence of the plant, which will replace the simple factory by factory, it is the abstract factory. And multiple levels of abstraction plant can occur.
If the factory back pan, there are some good quality plants will have different views, so the internal split, were thrown inside the pot, that is, the abstract factory. The more stable the total factory interfaces, the more detailed the internal back-pot rules, then dumped the pot of design is even more perfect, the more successful the abstract factory.
Specific products 1:
create.abstractfactory Package; public class XiangyouJiucaijidan the implements Condiment, Food { @Override public void the Add () { the System. OUT .println ( " add sesame oil " ); } @Override public void Cook () { . the System OUT .println ( " leek eggs done " ); } }
Specific products 2:
create.abstractfactory Package; public class XiangyouXihongshijidan the implements Condiment, Food { @Override public void the Add () { the System. OUT .println ( " add sesame oil " ); } @Override public void Cook () { . the System OUT .println ( " tomato and egg. " ); } }
3 specific products:
create.abstractfactory Package; public class JijingJiucaijidan the implements Food, Condiment { public void Cook () { the System. OUT .println ( " leek egg well " ); } @Override public void the Add () { . the System OUT .println ( " Add chicken. " ); } }
Specific products 4:
create.abstractfactory Package; public class JijingXihongshijidan the implements Food, Condiment { public void Cook () { the System. OUT .println ( " tomato and egg well " ); } @Override public void the Add () { . the System OUT .println ( " Add chicken. " ); } }
Product Category 1:
package create.abstractfactory; public interface Condiment { void add(); }
Product Category 2:
package create.abstractfactory; public interface Food { public void cook(); }
Total product interfaces:
package create.abstractfactory; public interface CookFactory { public Food createXihongshijidan() ; public Food createJiucaijidan() ; }
Categories Products Factory 1:
create.abstractfactory Package; public class JijingFactory the implements CookFactory { public Food createXihongshijidan () { return new new JijingXihongshijidan (); } public Food createJiucaijidan () { return new new JijingJiucaijidan (); } }
Category Products Factory 2:
create.abstractfactory Package; public class XiangyouFactory the implements CookFactory { public Food createXihongshijidan () { return new new XiangyouXihongshijidan (); } public Food createJiucaijidan () { return new new XiangyouJiucaijidan (); } }
Test categories:
package create.abstractfactory; public class Test { public static void main(String[] args) { CookFactory ff1 = new JijingFactory(); CookFactory ff2 = new XiangyouFactory(); Food f1 = ff1.createXihongshijidan(); Food f2 = ff2.createXihongshijidan(); f1.cook(); f2.cook(); } }
Fourth, multi-level abstract factory pattern
Consider the factory mode, simply by replacing the abstract factory plant.
If the factory interface higher level, need to be considered if the above change as little as possible interface interface stratified, random stratified go wrong. By changes in the level of frequencies should be lower.
For the above series of abstract factory, it may be simply formed inside the factory for the Categories, the plant may be a plurality of channels together with a plant may be split. The level of detail to see if it is split split, determine the level of abstraction. Whether the merger or split, in fact, need reconstruction. I want to start design perfection is impossible .
Fifth, the abstract factory implementation of step (abstract factory in the above example)
1 , analysis of specific product features, find 2 characteristics of different categories.
For example: sesame oil leek eggs, chicken eggs, leeks, tomatoes, eggs, sesame oil, chicken eggs, tomatoes.
So you can have a dish Ingredients and tune in two parts.
2 , consider the 2 class characteristics that the probability of a change is relatively low.
I personally think that the emergence of new varieties of seasoning will be a higher frequency, then that change Ingredients relatively fixed.
3 , to create a total change in the interface according to low frequency
Interface: dishes
There are two ways: to be a tomato and egg
Make a leek egg
If you think a seasoning stable, then there should be two ways out of two: food add sesame oil, add chicken food.
4 , the interface and the total specific dishes, create different classes of plants
The first factory entered, add sesame oil
The second factory entered, add chicken
5 , this time to consider change , we believe that seasoning is changed, there will be new spices, such as soy sauce.
Then we change the total interface does not change! ! And add a new factory class.
If the total change to the interface, then that design errors.
ps: Project Address svn: //47.105.188.20/kitty/2%E3%80%81code/pattern Username Password: reader / reader