Design Patterns-Abstract Factory

Source: Internet
Author: User

Design Patterns-Abstract Factory
AbstractFactory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes. Applicable scenarios: 1. A system must be independent of its product creation, combination, and representation. 2. A system must be configured by one of multiple product generations. 3. When you emphasize the design of a series of related product objects for joint use. 4. When you provide a product library, you only want to display their interfaces instead of implementations. Code abstraction factory abstractFactory code: public interface AbstractFactory {public ProductA factoryA (); public ProductB factoryB ();} The specific factory class implements AbstractFactory for creating objects. Copy the public class ConcreteFactory1 implements AbstractFactory {@ Override public ProductA factoryA () {return new ProductA1 () ;}@ Override public ProductB factoryB () {return new ProductB1 ();}} copy code copy code public class ConcreteFactory2 implements AbstractFactory {@ Override public ProductA factoryA () {return new ProductA2 () ;}@ Override public ProductB factoryB () {return new ProductB2 () ;}} copy Abstract interface (same as product B): public interface ProductA {public void method1 (); public void method2 ();} specific implementation of product A (same as product B ): copy the public class ProductA1 implements ProductA {@ Override public void method1 () {System. out. println ("ProductA1 ----> method1") ;}@ Override public void method2 () {System. out. println ("ProductA1 ----> method2") ;}} copy the code and copy the public class ProductA2 implements ProductA {@ Override public void method1 () {System. out. println ("ProductA2 ----> method1") ;}@ Override public void method2 () {System. out. println ("ProductA2 ----> method2") ;}copy the application code in the Code abstraction factory mode: copy the code public class Main {public static void main (String [] args) {AbstractFactory abf1 = new ConcreteFactory1 (); AbstractFactory abf2 = new ConcreteFactory2 (); ProductA a1 = inline (); ProductA a2 = abf2.factoryA (); ProductB b1 = abf1.factoryB (); ProductB b2 = abf2.factoryB (); a1.method1 (); a1.method2 (); a2.method1 (); a2.method2 (); b1.method1 (); b1.method2 (); b2.method1 (); b2.method2 () ;}} the difference between copying a code factory method and an abstract factory. The factory method mode is an abstract product class that can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. Each factory class can only create one instance of a specific product class. Abstract Factory mode multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. You can create multiple product instances for each specific factory class. The difference between the factory method mode is that there is only one abstract product class, while the abstract factory mode has multiple. The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances. Abstract Factory application Abstract Factory use scenario definition is very simple: an object family (or a group of objects without any relationship) have the same constraints, the abstract factory model can be used. What does it mean? For example, a text editor and an image processor are both software entities, but the text editor in * nix and the text editor in WINDOWS have the same functions and interfaces, but the code implementation is different, the image processor is similar, that is, it has a common constraint: the operating system type, so we can use the abstract factory mode to generate editors and image processors in different operating systems. Note the implementation of the abstract factory model in the shortcomings of the abstract factory model, we mentioned that the product family expansion of the abstract factory model is difficult, but it must be clear that it is difficult to expand the product family, rather than the product level, in this mode, the product level is very easy to expand. To add a product level, you only need to add a factory class to take charge of the newly added product production tasks, that is, horizontal scaling is easy, it is difficult to expand vertically. Best practices: When can a model be used? Many readers are confused. abstract factory models are a simple model and many scenarios are used, when different operating systems are involved in software product development, you can consider using the abstract factory model. For example, an application needs to run on three different platforms: on Windows, Linux, and Android, how do you design it? How to design three sets of different applications? The abstract factory mode is used to shield the impact of the operating system on applications. Software functions, application logic, and UI on the three different operating systems should be very similar. The only difference is that different factory methods are called, different product categories process the interaction information with the operating system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.