简体   繁体   中英

Regarding undrestanding of abstract factory pattern

I was going through abstract factory design pattern below is the UML diagram for that.. 在此处输入图片说明

please suggest me the best examples for this paatern as I have tried best but I am in search of that example that is easy to learn and want to make my understanding for abstract factory pattern 100% clear.Please advise

Have you looked at DocumentBuilderFactory class that's built into the JDK? It does exactly this, with it's target item being a Document object.

The jdk has the DocumentBuilderFactory class, which uses a service locator strategy to find aa concrete implementation of the DocumentBuilderFactory class (ie. xerces or some other parser).

// Uses service locator approach to find an implementor like xerces
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(...);

java.awt.Toolkit is another great example of this. This time, it uses the JVM implementation itself to provide the instance:

Toolkit toolkit = Toolkit.getDefaultToolkit();

The actual concrete class is different based on what OS you're on, and whether you're running in headless mode or not.

Recall that the Factory Pattern is used to create object without specifying the exact class of the object, thus decreasing the coupling between those two components. The Abstract Factory Pattern even decreases the amount of de-coupling by defining an interface which all factories must implement. Thus the caller of the abstract factory doesn't know anything about the implementation of factory and how objects are created. The caller just nows, calling a method on the factory will yield a certain object instance of interface X.

The XML Library example from Matt is actually a good example. The abstract factory is the entity creating the XML parser which creates a Document object representing the actual document you parse. Actually, for you as a caller, it is most of the time completely irrelevant which Parser is used, as long as you get a Document object out of it. Therefore you can simply use the abstract factory. Which creates you a valid parser (most of the times ;))

The Toolkit example (also mentioned by Matt) is a more school-book like example. Imagine you just want to populate a window on the user screen. As you want to do it in a platform independent way, you define an abstract class Window with which you can do certain operations. Then you create an object which creates these windows, let's say Win32WindowsFactory . However, as your code shall be platform-independet, you define an interface WindowsFactory which provides you a method createWindow() . When the Win32WindowsFactory is used, a Win32Window is returned, when a LinuxGTKWindowsFactory is used, a GTKWindow is returned.

Its most common usecase is Dependency Injection. You can find details in some of the these threads What is dependency injection?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM