Concise Abstract Factory mode (3.1)

Source: Internet
Author: User


Zhen Lou

In some books, two-dimensional graphs are used to illustrate the abstract factory pattern. Very good, but yqj2065 not like their product family/product grade, changed to brand/product type .


Abstract Factory pattern is more powerful than the factory method pattern. Is a factory can create multiple ancillary products.

In everyday life. Abstract factory patterns abound. For example, clothing factory can produce matching tops/tops, under the installation of/bottoms. Electrical companies such as the United States, Haier can produce its brand of refrigerators, air conditioners, televisions and so on.

The abstract factory model is characterized by more than two factory methods in the factory interface.

Routine 2-6 Brand Company Package Creational.factory.abstractfactory;public class Nikefactory implements iclothingfactory{    @ Override public Tops Gettops () {        return new niketops ();    }    @Override public Bottoms Getbottoms () {        return new nikebottoms ();}    }
Abstract Factory mode is a simple concept, but writing a demo program isat least 10 classes to write(including client). What you see.

Obviously, Nikefactory will only produce niketops and nikebottoms, after all, the brand is not a cottage or OEM companies.

(yqj2065 in the back to consider the cottage problem)

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvexfqmja2nq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Package Creational.factory.abstractfactory;public class client{public    static void Test () {        iclothingfactory F = (iclothingfactory) tool. God.create ("2-6-factory");        Tops Tops = F.gettops ();        Bottoms BT =f.getbottoms ();        System.out.println (Tops.getname ());        System.out.println (Bt.getname ());}    }

The abstract factory pattern consists of 4 roles. Abstract factory roles, such as Iclothingfactory. Detailed factory roles, sub-types of the former, abstract product roles, such as Tops and bottoms, and detailed product roles, which are subtypes of abstract product roles.

From the client's point of view, in the abstract Factory mode, the iclothingfactory is specified through the configuration file, thus obtaining the top/tops of its production. Other manufacturers can be designated to obtain their production of trousers/bottoms. client relies only on abstract factory roles and abstract product roles; avoid Newniketops () and new adidasbottoms in your code.

2. Extensibility

The subclass of Iclothingfactory, can be added according to need. Meet OCP.

On the one hand, if today's clothing factory/iclothingfactory in addition to the production of matching tops, trousers. also produces shoes/shoe.

Need to join in Iclothingfactory

Public Bottoms Getshoe ();

Before Java8. interface cannot be upgraded. Assume that an interface is defined. Send the interface to the client the ape is used, then the defined interface cannot be changed. The addition of a method to the interface will result in the interruption of all implementation classes of the old version number interface.

Right now widely used in iclothingfactory and it is necessary to join Getshoe () with the TimesWhen The default method of Java8 (defender Methods,virtualextension methods) is like a wonderful remorse medicine.
Routine 2-7 default method package Creational.factory.abstractfactory;public interface iclothingfactory{public    Tops gettops ();    Public Bottoms getbottoms ();    Default public Shoe Getshoe () {        return null;    }} Class client{public    static void Test () {        iclothingfactory f = (iclothingfactory) tool. God.create ("2-6-factory-nike");                f = (iclothingfactory) tool. God.create ("2-6-factory-ad");        Shoe Shoe =f.getshoe ();        System.out.println (Shoe.getname ());}    }

Nikefactory rewritten Getshoe (), the client in the test code can obtain shoe object normally, assuming that the specified detailed factory did not overwrite Getshoe (). The shoe object is null.

The default method is added to the interface, which is a degenerate inheritance for subclasses that do not overwrite the default method-Like an ostrich is a bird, violating the LSP thus violating the OCP.

The default method is added to the interface to ensure that the previous code can be executed properly-because the previous code could not know or exist using Getshoe (). Thus the forward compatibility is ensured .

But. When writing new code, it is important to note that Iclothingfactory has a method Getshoe (). But it is not the fact that the present class gives an effective implementation. What makes you think you can make shoes with iclothingfactory instances?

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvexfqmja2nq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Concise Abstract Factory mode (3.1)

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.