Abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode is an improvement to the factory method pattern: In the factory method mode, if new products are added, then it is necessary to go into the factory to add new methods to produce new products, which is not very friendly to code development. The abstract factory model is different, after the addition of new products, only need to write a corresponding factory.

Package mode.abstract_factory;/* Abstract Fruit Factory */public interface Abstractfruitfactory {public Fruit getfruit ();}

Package mode.abstract_factory;/* Abstract Fruit Factory */public interface Abstractfruitfactory {public Fruit getfruit ();}

Package mode.abstract_factory;/* Apple */public class Apple implements Fruit {@Overridepublic void Eat () {System.out.println ("Eat Apples");}}

Package mode.abstract_factory;/** Apple Factory */public class Applefactory implements Abstractfruitfactory {@Overridepublic Fruit Getfruit () {return new Apple ();}}

With this Apple factory, we can produce our apples, but now we have introduced an orange product.

Package mode.abstract_factory;/** Orange */public class Orange implements Fruit {@Overridepublic void eat () { System.out.println ("Eat Oranges");}}

To produce oranges, we build an orange factory.

Package mode.abstract_factory;/** Orange Factory */public class Orangefactory implements Abstractfruitfactory {@Overridepublic Fruit Getfruit () {return new Orange ();}}

With this orange factory we can produce oranges, and here we do not modify the original factory, but add new factories. This makes it much better to create new independent classes with a schema than to modify other classes in the source code.

Abstract Factory mode

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.