Abstract Factory model of Design Pattern

Source: Internet
Author: User

Abstract Factory mode: defines an interface for creating an object, so that the subclass decides which class to instantiate. One of the advantages of the abstract factory model is that it complies with the "open-closed" principle, that is, the modification is closed and the extension is open. Compared with the simple factory mode, the abstract factory mode does not place the object generation in the factory class, but in the factory subclass. To add an object, you only need to add the class of the object and add the factory of the generated object. You do not need to modify the original code. Abstract Factory class diagram: public interface Fruit {public abstract void eat (String sun, String water);} public interface Factory {public abstract Fruit getFruit ();} public class Apple implements Fruit {public void eat (String sun, String water) {}} public class AppleFactory implements Factory {public Fruit getFruit () {return new Apple ();}} public class Bananas implements Fruit {public void eat (String sun, String water) {}} public class BananasFactory implements Factory {public Fruit getFruit () {return new Bananas ();}} public class Client {public static void main (String [] args) {Fruit apple = new AppleFactory (). getFruit (); apple. eat ("Sunshine", "water"); Fruit bananas = new BananasFactory (). getFruit (); bananas. eat ("Sunshine", "water ");}}

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.