Java design pattern-Abstract Factory pattern (Abstract Factory)

Source: Internet
Author: User

Java design pattern-Abstract Factory pattern (Abstract Factory)
Purpose: provide an interface to create a family of mutually dependent objects without explicitly specifying the object class.

When to use: A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.
This system has more than one product family, and the system only consumes one of them.
Products belonging to the same product family are used together. This constraint must be reflected in the system design.
The system provides a product library. All products use the same interface, so that the client does not rely on implementation.
Actual Example: javax. xml. parsers. DocumentBuilderFactory
Sample Code:

public class App {public static void main(String[] args) {createKingdom(new ElfKingdomFactory());createKingdom(new OrcKingdomFactory());}public static void createKingdom(KingdomFactory factory) {King king = factory.createKing();Castle castle = factory.createCastle();Army army = factory.createArmy();System.out.println(The kingdom was created.);System.out.println(king);System.out.println(castle);System.out.println(army);}}
public interface King {}public interface Castle {}public interface Army {}public interface Army {}
 
public interface KingdomFactory { Castle createCastle(); King createKing(); Army createArmy(); }
 
public class ElfKingdomFactory implements KingdomFactory {public Castle createCastle() {return new ElfCastle();}public King createKing() {return new ElfKing();}public Army createArmy() {return new ElfArmy();}}
public class OrcKingdomFactory implements KingdomFactory {public Castle createCastle() {return new OrcCastle();}public King createKing() {return new OrcKing();}public Army createArmy() {return new OrcArmy();}}
public class ElfArmy implements Army {@Overridepublic String toString() {return This is the Elven Army!;}}
public class ElfKing implements King {@Overridepublic String toString() {return This is the Elven king!;}}
public class ElfCastle implements Castle {@Overridepublic String toString() {return This is the Elven castle!;}}
public class OrcArmy implements Army {@Overridepublic String toString() {return This is the Orcish Army!;}}
public class OrcCastle implements Castle {@Overridepublic String toString() {return This is the Orcish castle!;}}
public class OrcKing implements King {@Overridepublic String toString() {return This is the Orc king!;}}

Output result:
The kingdom was created.This is the Elven king!This is the Elven castle!This is the Elven Army!The kingdom was created.This is the Orc king!This is the Orcish castle!This is the Orcish Army!

 











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.