Java and patterns 5-abstract factory pattern)

Source: Internet
Author: User

Problem:
When the system creates a group of related or interdependent objects, use the abstract factory mode.
The abstract factory mode can provide an interface to the client so that the client can create product objects in multiple product families without specifying the specific product type. This is the intention of the abstract factory.
Class diagram:

 

Source code:
Package com. designpatterns. abstractfactory;
/**
* Abstract Factory
*/
Public interfaceAbstractfactory{
Public abstractproducta createproducta ();
Public abstractproductb createproductb ();
}

Public interfaceAbstractproducta{
Public void operation ();
}

Public interfaceAbstractproductb{
Public void operation ();
}

Public classConcretefactory1Implements abstractfactory {
Public abstractproducta createproducta ()
{
Return new producta1 ();
}
 
Public abstractproductb createproductb ()
{
Return new productb1 ();
}
}

Public classConcretefactory2Implements abstractfactory {
Public abstractproducta createproducta ()
{
Return new producta2 ();
}
 
Public abstractproductb createproductb ()
{
Return new productb2 ();
}
}

Public classProducta1Implements abstractproducta {
Public producta1 ()
{
System. Out. println ("producta1 ...");
}
 
Public void operation (){}
}

Public classProducta2Implements abstractproducta {
Public producta2 ()
{
System. Out. println ("producta2 ...");
}
 
Public void operation (){}
}

Public classProductb1Implements abstractproductb {
Public productb1 ()
{
System. Out. println ("productb1 ...");
}
 
Public void operation (){}
}

Public classProductb2Implements abstractproductb {
Public productb2 ()
{
System. Out. println ("productb2 ...");
}
Public void operation (){}
}

/**
* Client class
*/
Public classClient{
Public static void main (string [] ARGs ){
Abstractfactory AF1 = new concretefactory1 ();
Af1.createproducta ();
Af1.createproductb ();

Abstractfactory af2 = new concretefactory2 ();
Af2.createproducta ();
Af2.createproductb ();
}
}

Discussion:
The abstractfactory mode provides interfaces for creating a group of (multi-class) related or dependent objects, while the factory mode provides interfaces for creating a class of objects or delays the creation of objects into subclass. the abstractfactory mode is usually implemented in the factory mode, as shown in the preceding example.

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.