Abstract factory pattern of design patterns

Source: Internet
Author: User

Design pattern--Abstract Factory mode

1, definition: Provide an interface that creates a series of related or interdependent objects without having to develop their specific classes;

2, the following is a schematic diagram of the abstract factory pattern:

3, the implementation code of the Abstract factory pattern:

1), Structure diagram:

 

 

2), Code

Entry Class Main.class:

public class Main {

Public static void Main (string[] args) {

Breadmaker Breadmaker;

Pizzamaker Pizzamaker;

System.out.println ("Opening of a noodle bakery");

System.out.println ("Customers want to buy black bread");

Ifactory breadfactory = new Blackbreadfactory ();

Breadmaker = Breadfactory.createbread ();

Breadmaker.getbread ();

SYSTEM.OUT.PRINTLN ("Customer wants black Chocolate pizza");

Pizzamaker = Breadfactory.createpizza ();

Pizzamaker.getpizza ();

}

}

Factory Interface Class Ifactory.class:

Public interface Ifactory {

Breadmaker Createbread ();

Pizzamaker Createpizza ();

}

Three Factory classes:

Blackbreadfactory.class,honeybreadfactory.class,whitebreadfactory.class:

public class Blackbreadfactory implements ifactory{

@Override

Public Breadmaker Createbread () {

TODO auto-generated Method Stub

return new Blackbread ();

}

@Override

Public Pizzamaker Createpizza () {

TODO auto-generated Method Stub

return new Blackpizza ();

}

}

public class Honeybreadfactory implements ifactory{

@Override

Public Breadmaker Createbread () {

TODO auto-generated Method Stub

return new Honeybread ();

}

@Override

Public Pizzamaker Createpizza () {

TODO auto-generated Method Stub

return new Honeypizza ();

}

}

public class Whitebreadfactory implements ifactory{

@Override

Public Breadmaker Createbread () {

TODO auto-generated Method Stub

return new Whiteblead ();

}

@Override

Public Pizzamaker Createpizza () {

TODO auto-generated Method Stub

return new Whitepizza ();

}

}

Two parent classes:

Breadmaker,pizzamaker:

public class Breadmaker {

public void Getbread () { }

}

public class Pizzamaker {

public void Getpizza () { }

}

Related sub-classes:

Blackbread,blackpizza,honeybread,honeypizza,whiteblead,whitepizza:

public class Blackbread extends breadmaker{

@Override

public void Getbread () {

TODO auto-generated Method Stub

Super. Getbread ();

System.out.println ("dark chocolate bread made of a facet");

}

}

public class Blackpizza extends pizzamaker{

@Override

public void Getpizza () {

TODO auto-generated Method Stub

Super. Getpizza ();

System.out.println ("Make a dark chocolate pizza");

}

}

public class Honeybread extends breadmaker{

@Override

public void Getbread () {

TODO auto-generated Method Stub

Super. Getbread ();

System.out.println ("Made of honey bread");

}

}

public class Honeypizza extends pizzamaker{

@Override

public void Getpizza () {

TODO auto-generated Method Stub

Super. Getpizza ();

System.out.println ("Baked honey pizza on a noodle");

}

}

public class Whiteblead extends breadmaker{

@Override

public void Getbread () {

TODO auto-generated Method Stub

Super. Getbread ();

System.out.println ("White chocolate bread made of a facet");

}

}

public class Whitepizza extends pizzamaker{

@Override

public void Getpizza () {

TODO auto-generated Method Stub

Super. Getpizza ();

System.out.println ("White chocolate Pizza" made from a facet);

}

}

3), operation result:

3, Abstract Factory mode application:

1), a series to be independent of its product creation, composition and representation;

2), a series to be configured due to one of multiple product families;

3) When it is necessary to emphasize the design of a series of related product objects for joint use;

4), when you want to provide a product class library, and only display their interface instead of the implementation;

Abstract factory pattern of design patterns

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.