Java design pattern--creation pattern--abstract factory

Source: Internet
Author: User

What is abstract factory, learn again.

1 Abstract Factory2 Overview3 provides an interface to create a series of related or interdependent objects without specifying their specific classes. 4     5 Applicability61a system is independent of the creation, composition, and presentation of its products. 7 82. When a system is to be configured by one of several product families. 9 Ten3when you want to emphasize the design of a series of related product objects for joint use.  One  A4when you provide a Product class library, and you want to display only their interfaces instead of implementing them.  - participants -1. Abstractfactory the declares an operation interface that creates an abstract product object.  -  -2. Concretefactory - implement actions to create specific product objects.  +  -3. Abstractproduct + declares an interface for a class of product objects.  A  at4. Concreteproduct - define a Product object that will be created by the corresponding specific factory.  - implements the Abstractproduct interface.  -  -5. Client - use only interfaces declared by the Abstractfactory and Abstractproduct classes in  

Test class:

1  Public classTest {2      Public Static voidMain (string[] args) {3Ianimalfactory blackanimalfactory =Newblackanimalfactory ();4ICat Blackcat =Blackanimalfactory.createcat ();5 blackcat.eat ();6Idog Blackdog =Blackanimalfactory.createdog ();7 blackdog.eat ();8 9Ianimalfactory whiteanimalfactory =Newwhiteanimalfactory ();TenICat Whitecat =Whiteanimalfactory.createcat (); One whitecat.eat (); AIdog Whitedog =Whiteanimalfactory.createdog (); - whitedog.eat (); -     } the  -}
1  Public Interface ianimalfactory {23    ICat createcat (); 4     5     idog createdog (); 6 }
1  Public classBlackanimalfactoryImplementsIanimalfactory {2 3      PublicICat Createcat () {4         return NewBlackcat ();5     }6 7      Publicidog Createdog () {8         return NewBlackdog ();9     }Ten  One}
1  Public Interface ICat {23     void eat (); 4 }
 1  public  class  Blackcat implements   ICat { 2  public  void   Eat () { 4  System.out.printl N ("The Black Cat is eating!"  5   6  7 } 
1  Public class Implements ICat {23      Public void eat () {4         System.out.println ("The White Cat is eating!" ); 5     }67 }

Java design pattern--creation pattern--abstract factory

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.