Concise abstract factory model (3.1)

Source: Internet
Author: User


Town Building

Abstract Factory Pattern)Which is more powerful than the factory method mode?OneFactory creation capabilityMultiple packages.

Abstract Factory patterns are everywhere in daily life. For example, a garment factory can produce matching top/tops and bottom/bottoms. Electric Appliance companies such as Midea and Haier can produce refrigerators, air conditioners, and televisions of their brands.

Abstract Factory mode has two or more 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 simple in concept, but at least 10 classes (including clients) are required to compile the demo program ).. Obviously, nikefactory only produces niketops and nikebottoms. After all, brand companies are not shanzhai or OEM companies. (Yqj2065 considers the shanzhai issue later)

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 model contains four roles. Abstract Factory roles, such as iclothingfactory; specific factory roles, subtypes of the former; abstract product roles, such as tops and bottoms; specific product roles are subtypes of abstract product roles.

From the client's point of view, in the abstract factory mode, you can specify iclothingfactory through the configuration file to obtain the top/tops produced by it. You can then specify other manufacturers to obtain their production pants/bottoms; The client only depends on the abstract factory role and the abstract Product role.Avoid newniketops () and new adidasbottoms in the code.

2. scalability

Sub-classes of iclothingfactory can be added as needed to comply with OCP.

On the other hand, it is assumed that the current garment factory/iclothingfactory not only produces matching jackets and trousers, but also produces shoes/Show. In iclothingfactoryNeed to add

Public bottoms getshow ();

Before Java 8, Apis cannot be upgraded. If an interface is defined and sent to the client programmer, the defined interface cannot be modified. If you add a method to an interface, all implementation classes of interfaces of earlier versions are interrupted.

Today, It is widely used in iclothingfactory and it is very important to add getshow ()The default method of Java 8 (Defender methods, virtualextension methods) is like a magical regret.
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 = f. getshoe (); system. out. println (shoe. getname ());}}

Nikefactory overrides getshoe (), and the client in the test code can normally obtain the shoe object. If the specified factory does not rewrite getshoe (), the shoe object is null.

Adds a default method to the interface. For subclasses that do not rewrite the default method, it is a degradation inheritance.-- Violation of LSP, like an ostrich being a bird, violates OCP.

Add the default method to the interface to ensure that the previous code can run normally-because the previous Code cannot be known or does not use getshoe (),Ensures forward compatibility. However, when writing new code, you must note that iclothingfactory has the getshow () method,NotIts implementation classes provide effective implementations. Why do you think that all instances of iclothingfactory can produce shoes?

Concise abstract factory model (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.