Factory Factory, as the name suggests, is a processing factory that produces products. The abstract factory pattern is the most abstract and general form of all the factory patterns. The abstract factory pattern refers to a factory pattern used when there are multiple abstract roles. The abstract factory pattern can provide an interface to the client, so that the client can create product objects in multiple product families without specifying the specific product.
The factory mode is used a lot in practical applications. For example, the skin-changing function of QQ that everyone uses is realized by the application of the factory mode. As long as a skin is selected, the entire interface style is changed, including background images, buttons, interface styles, etc. Next, I will use the factory pattern to implement a simple skin change function.
If a set of skins has a background image, interface and buttons, these are called abstract products. For example, the background image has a red-style background image and a blue-style background image, and the interface also has a red-style interface and a blue-style interface, and the interface is the same. First we build these abstract products:
BackGround.java
1 public abstract class Background { 2 public abstract void showBgPic(); 3 }
Button.java
1 public abstract class Button { 2 public abstract void onclick(); 3 }
QQFrame.java
1 public abstract class QQFrame { 2 public abstract void printColor (); 3 }
Then create a specific product, such as creating a red-style skin, you need to create a red-style background image, a red-style interface, and a red-style button. code show as below
1 BlueBackground.java 2 public class BlueBackground extends Background { 3 4 @Override 5 public void showBgPic() { 6 System.out.println("Blue Background Picture...." ); 7 } 8 9 } 10 11 BlueButton .java 12 public class BlueButton extends Button { 13 14 @Override 15 public void onclick() { 16 System.out.println("The blue button is pressed"); 17 } 18 19 } 20 21 BlueFrame.java 22 public class BlueFrame extends QQFrame { 23 24 @Override 25 public void printColor() { 26 System.out.println("Blue interface style" ); 27 } 28 29 }
Then build an abstract factory SkinFactory. The abstract factory includes abstract methods for creating background images, creating interfaces, and creating buttons. The code is as follows:
1 public abstract class SkinFactory { 2 public abstract Background createBackground(); 3 public abstract Button createButton(); 4 public abstract QQFrame createFrame(); 5 }
写好抽象工厂之后就可以写具体的工厂了,比如说BlueSkinFactory,具体工厂要继承抽象工厂SkinFactory,并实现其的抽象方法。代码如下:
1 public class BlueSkinFactory extends SkinFactory { 2 3 @Override 4 public Background createBackground() { 5 // TODO Auto-generated method stub 6 return new BlueBackground(); 7 } 8 9 @Override 10 public Button createButton() { 11 // TODO Auto-generated method stub 12 return new BlueButtom(); 13 } 14 15 @Override 16 public QQFrame createFrame() { 17 // TODO Auto-generated method stub 18 return new BlueFrame(); 19 } 20 21 }
最后写一个测试类,代码如下:
1 public class Test { 2 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 SkinFactory skinFactory = new BlueSkinFactory(); 8 Background background = skinFactory.createBackground();//以后需要换皮肤只需改这一句 9 Button button = skinFactory.createButton(); 10 QQFrame frame = skinFactory.createFrame(); 11 background.showBgPic(); 12 button.onclick(); 13 frame.printColor(); 14 } 15 16 }
This makes it easy to change skins. If you need to change the skin, you just need to change the specific factory, for example, change SkinFactory skinFactory = new BlueSkinFactory() to SkinFactory skinFactory = new RedSkinFactory() (but first, you must build specific products and specific factories). Other codes There is no need to change, so the scalability is very good.