Home >Java >javaTutorial >Detailed explanation of JAVA abstract factory pattern with examples

Detailed explanation of JAVA abstract factory pattern with examples

WBOY
WBOYforward
2022-04-20 12:50:492758browse

This article brings you relevant knowledge about java, which mainly introduces related issues about the abstract factory pattern. Learn what the abstract factory pattern is based on actual examples. Let’s do it together. Take a look, hope it helps everyone.

Detailed explanation of JAVA abstract factory pattern with examples

## Recommended study: "

java video tutorial"

What is the abstract factory pattern

Definition of the Abstract Factory (AbstractFactory) pattern: It is an interface that provides an access class with an interface to create a set of related or interdependent objects, and the access class can obtain the same family without specifying the specific class of the desired product. Pattern structure of different levels of products.

Meet the conditions:

    There are multiple product families in the system, and each specific factory creates products of the same family but belonging to different hierarchical structures.
  • The system can only consume products of one family at a time, that is, products of the same family can be used together.

Advantages:

  • # Multi-level products related in the product family can be jointly managed within the class without having to specifically Introduce multiple new classes for management.

  • When a product family is required, the abstract factory can ensure that the client always only uses the product group of the same product.

  • Abstract factory enhances the scalability of the program. When adding a new product family, there is no need to modify the original code and satisfy the opening and closing principle.

Disadvantages:

    When a new product needs to be added to the product family, all factory classes need to be modified. It increases the abstraction and difficulty of understanding the system.
Implementation of Abstract Factory

The main roles in the case are as follows:
Abstract Factory: The inheritance of the Seven Heroes
Factory (SimpleFactory): Master, Mistress
Abstract Product (Product): Meteorite (raw material for making swords), a nest of dove eggs (unhatched Spirit Pigeon)
Concrete Product: Seven Swords. Seven Spiritual Pigeons

Case: The master has the inheritance of the Seven Heroes and can forge the Sword of the Sun and cultivate male little spirit pigeons.
The master’s wife has the inheritance of the Seven Heroes, and can forge the Yin Sword and cultivate female doves.
The Seven Heroes find their master and their wife to make their own swords and adopt their own little pigeons.

Abstract product, product

S1 interface (Tianwai Meteorite):

A method is declared in the interface

public interface S1 {
    void show();}

S2 interface (a nest of dove eggs):

The interface declares a method

public interface S2 {
    void show();}

A1 class Implement interface S1 (Changhong Sword):

Implement S1 interface

public class A1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把长虹剑");

    }}

B1 class implements interface S1 (Ice Soul Sword):

Implement S1 interface

public class B1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把冰魄剑");

    }}

C1 class implements interface S1 (Ziyunjian):

Implements S1 Interface

public class C1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把紫云剑");
    }}

D1 class implements interface S1 (Rain Flower Sword):

Implements S1 interface

public class D1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把紫云剑");
    }}

E1 class implements interface S1 (Green Light Sword):

Implements Ss interface

public class E1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把紫云剑");
    }}

F1 class implements interface S1 (Ben Lei Sword):

Implement Ss interface

public class F1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把紫云剑");
    }}

G1 class implements interface S1 (Tornado Sword):

Implement Ss interface

public class G1 implements S1 {
    @Override
    public void show() {
        System.out.println("打造了一把紫云剑");
    }}

A2 class implements interface S2 (Lingge No. 1):

Implements S1 interface

public class A2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");

    }}

Class B2 implements interface S2 (Lingge No. 2):

Implements S1 interface

public class B2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");

    }}

C2 Class implements interface S2 (Lingge No. 3):

implements S1 interface

public class C2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");
    }}

D2 class implements interface S2 (Lingge No. 4) :

Implement S1 interface

public class D2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");
    }}

E2 class implements interface S2 (Lingge No. 5):

Implement Ss interface

public class E2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");
    }}

F2 class implements interface S2 (Lingge No. 6):

Implements Ss interface

public class F2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");
    }}

G2 class implements interface S2 (Lingge No. 7):

Implements Ss interface

public class G2 implements S2 {
    @Override
    public void show() {
        System.out.println("灵鸽七号");
    }}
Abstract factory, factory

Zz interface (Legendary of the Seven Heroes):

The interface declares a sword-making method abc1() and a pigeon-raising method abc2()

public interface Zz {
    S1 abc1(String a);
    S2 abc2(String a);}

Nan class implements interface Zz (master):

Implements Zz interface

public class Nan implements Zz {
    private static S1 s1;
    private static S2 s2;

    public S1 abc1(String a) {
        if ("长虹剑".equals(a)) {
            s1 = new A1();
        } else if ("奔雷剑".equals(a)) {
            s1 = new F1();
        } else if ("青光剑".equals(a)) {
            s1 = new E1();
        } else if ("雨花剑".equals(a)) {
            s1 = new D1();
        } else if ("旋风剑".equals(a)) {
            s1 = new G1();
        } else {
            System.out.println("铸剑失败");
        }
        return s1;
    }
    public S2 abc2(String a) {
        if ("灵鸽一号".equals(a)) {
            s2 = new A2();
        } else if ("灵鸽二号".equals(a)) {
            s2 = new F2();
        } else if ("灵鸽三号".equals(a)) {
            s2 = new E2();
        } else if ("灵鸽四号".equals(a)) {
            s2 = new D2();
        } else if ("灵鸽五号".equals(a)) {
            s2 = new G2();
        } else {
            System.out.println("领养失败");
        }
        return s2;
    }}

Nv class Implement interface Zz (Master Niang):

Implement Zz interface

public class Nv implements Zz {
    private static S1 s1;
    private static S2 s2;

    public S1 abc1(String a) {
        if ("冰魄剑".equals(a)) {
            s1 = new B1();
        } else if ("紫云剑".equals(a)) {
            s1 = new C1();
        } else {
            System.out.println("铸剑失败");
        }
        return s1;
    }
    public S2 abc2(String a) {
        if ("灵鸽六号".equals(a)) {
            s2 = new B2();
        } else if ("灵鸽七号".equals(a)) {
            s2 = new C2();
        } else {
            System.out.println("领养失败");
        }
        return s2;
    }}
Test


Test class:

public class Demo {
    public static void main(String[] args) {
        System.out.println("---------至阳传承-------------");
        //师傅
        Nan nan = new Nan();
        //造剑
        S1 a1 = nan.abc1("长虹剑");
        S1 d1 = nan.abc1("雨花剑");
        S1 e1 = nan.abc1("青光剑");
        S1 f1 = nan.abc1("奔雷剑");
        S1 g1 = nan.abc1("旋风剑");
        //养鸽子
        S2 a2 = nan.abc2("灵鸽一号");
        S2 d2 = nan.abc2("灵鸽二号");
        S2 e2 = nan.abc2("灵鸽三号");
        S2 f2 = nan.abc2("灵鸽四号");
        S2 g2 = nan.abc2("灵鸽五号");
        a1.show();
        d1.show();
        e1.show();
        f1.show();
        g1.show();
        a2.show();
        d2.show();
        e2.show();
        f2.show();
        g2.show();
        System.out.println("---------至阴传承-------------");
        //女铸剑师
        Nv nv = new Nv();
        S1 b1 = nv.abc1("冰魄剑");
        S1 c1 = nv.abc1("紫云剑");
        b1.show();
        c1.show();
        S2 b2 = nv.abc2("灵鸽六号");
        S2 c2 = nv.abc2("灵鸽七号");
        b2.show();
        c2.show();
    }}
Recommended learning: "

java video tutorial"

The above is the detailed content of Detailed explanation of JAVA abstract factory pattern with examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete