提供一個接口以提供一系列相關或相互依賴的對象,而無須知道指定它們的具體的類java
Kitapp
抽象工廠具備如下優勢和缺點:ide
1) 分離了具體的類 客戶經過它們的抽象對象接口操縱實例。產品的類名也在具體工廠的實現中被隔離,即它們不出如今客戶代碼中ui
2) 易於交換產品系列 抽象工廠建立了完整的產品系列,只須要改變具體工廠便可使用不一樣產品的配置spa
3) 利於產品的統一性 當一個系列中的產品對象被設計成一塊兒工做時,一個應用一次只能使用同一系列中的對象設計
4) 難以支持新種類的產品 抽象工廠接口肯定了能夠被建立的產品集合。支持新種類的產品就須要擴展該工廠接口,這將設計抽象工廠類及其子類的改變code
buttons: 第一個產品層次結構 orm
Button.javaxml
package abstract_factory; /** * @author GaoMing * @date 2021/7/17 - 15:37 * */ public interface Button { void paint(); }
MacOSButton.java對象
package abstract_factory;
/**
* @author GaoMing
* @date 2021/7/17 - 15:38
*/
public class MacOSButton implements Button{
@Override
public void paint(){
System.out.println("You have created MacOSButton.");
}
}
WindowsButton.java
package abstract_factory; /** * @author GaoMing * @date 2021/7/17 - 15:39 */ public class WindowsButton implements Button{ @Override public void paint(){ System.out.println("You have created WindowButton."); } }
Checkbox: 第二個產品結構
Checkbox.java
package abstract_factory; /** * @author GaoMing * @date 2021/7/17 - 15:40 */ public interface Checkbox { void paint(); }
MacOSCheckbox.java
package abstract_factory; /** * @author GaoMing * @date 2021/7/17 - 15:41 */ public class MacOSCheckbox implements Checkbox{ @Override public void paint(){ System.out.println("You have created MacOSCheckbox."); } }
WindowsCheckbox.java
package abstract_factory;
/**
* @author GaoMing
* @date 2021/7/17 - 15:42
*/
public class WindowsCheckbox implements Checkbox{
@Override
public void paint(){
System.out.println("You have created WindowCheckbox.");
}
}
Factories
GUIFactories.java: 抽象工廠
package abstract_factory.factories; import abstract_factory.Button; import abstract_factory.Checkbox; /** * @author GaoMing * @date 2021/7/17 - 15:43 */ public interface GUIFactory { Button createButton(); Checkbox createCheckbox(); }
MacOSFactory.java: 具體工廠 ( macOS)
package abstract_factory.factories; import abstract_factory.Button; import abstract_factory.Checkbox; import abstract_factory.MacOSButton; import abstract_factory.MacOSCheckbox; /** * @author GaoMing * @date 2021/7/17 - 15:46 */ public class MacOSFactory implements GUIFactory{ @Override public Button createButton(){ return new MacOSButton(); } @Override public Checkbox createCheckbox(){ return new MacOSCheckbox(); } }
WindowsFactory.java: 具體工廠 (Windows)
package abstract_factory.factories; import abstract_factory.Button; import abstract_factory.Checkbox; import abstract_factory.WindowsButton; import abstract_factory.WindowsCheckbox; /** * @author GaoMing * @date 2021/7/17 - 15:49 */ public class WindowsFactory implements GUIFactory{ @Override public Button createButton(){ return new WindowsButton(); } @Override public Checkbox createCheckbox(){ return new WindowsCheckbox(); } }
Application.java: 客戶端代碼
package abstract_factory; import abstract_factory.Button; import abstract_factory.Checkbox; import abstract_factory.factories.GUIFactory; /** * @author GaoMing * @date 2021/7/17 - 15:52 */ public class Application { private Button button; private Checkbox checkbox; public Application(GUIFactory factory){ button = factory.createButton(); checkbox = factory.createCheckbox(); } public void paint(){ button.paint(); checkbox.paint(); } }
Demo.java: 程序配置
package abstract_factory; import abstract_factory.factories.GUIFactory; import abstract_factory.factories.MacOSFactory; import abstract_factory.factories.WindowsFactory; import abstract_factory.Application; import java.util.Locale; /** * @author GaoMing * @date 2021/7/17 - 15:57 */ public class Demo { private static Application configureApplication(){ Application app; GUIFactory factory; String osName = System.getProperty("os.name").toLowerCase(); if(osName.contains("mac")){ factory = new MacOSFactory(); app = new Application(factory); }else{ factory = new WindowsFactory(); app = new Application(factory); } return app; } public static void main(String[] args){ Application app = configureApplication(); app.paint(); } }
運行結果
You create WindowsButton.
You created WindowsCheckbox.
javax.xml.parsers.DocumentBuilderFactory#newInstance()
javax.xml.transform.TransformerFactory#newInstance()
javax.xml.xpath.XPathFactory#newInstance()