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.
## Recommended study: "java video tutorial"
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.
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.
S1 interface (Tianwai Meteorite):
A method is declared in the interfacepublic interface S1 { void show();}Copy after login
S2 interface (a nest of dove eggs):
The interface declares a methodpublic interface S2 { void show();}Copy after login
A1 class Implement interface S1 (Changhong Sword):
Implement S1 interfacepublic class A1 implements S1 { @Override public void show() { System.out.println("打造了一把长虹剑"); }}Copy after login
B1 class implements interface S1 (Ice Soul Sword):
Implement S1 interfacepublic class B1 implements S1 { @Override public void show() { System.out.println("打造了一把冰魄剑"); }}Copy after login
C1 class implements interface S1 (Ziyunjian):
Implements S1 Interfacepublic class C1 implements S1 { @Override public void show() { System.out.println("打造了一把紫云剑"); }}Copy after login
D1 class implements interface S1 (Rain Flower Sword):
Implements S1 interfacepublic class D1 implements S1 { @Override public void show() { System.out.println("打造了一把紫云剑"); }}Copy after login
E1 class implements interface S1 (Green Light Sword):
Implements Ss interfacepublic class E1 implements S1 { @Override public void show() { System.out.println("打造了一把紫云剑"); }}Copy after login
F1 class implements interface S1 (Ben Lei Sword):
Implement Ss interfacepublic class F1 implements S1 { @Override public void show() { System.out.println("打造了一把紫云剑"); }}Copy after login
G1 class implements interface S1 (Tornado Sword):
Implement Ss interfacepublic class G1 implements S1 { @Override public void show() { System.out.println("打造了一把紫云剑"); }}Copy after login
A2 class implements interface S2 (Lingge No. 1):
Implements S1 interfacepublic class A2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
Class B2 implements interface S2 (Lingge No. 2):
Implements S1 interfacepublic class B2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
C2 Class implements interface S2 (Lingge No. 3):
implements S1 interfacepublic class C2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
D2 class implements interface S2 (Lingge No. 4) :
Implement S1 interfacepublic class D2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
E2 class implements interface S2 (Lingge No. 5):
Implement Ss interfacepublic class E2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
F2 class implements interface S2 (Lingge No. 6):
Implements Ss interfacepublic class F2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
G2 class implements interface S2 (Lingge No. 7):
Implements Ss interfaceAbstract factory, factorypublic class G2 implements S2 { @Override public void show() { System.out.println("灵鸽七号"); }}Copy after login
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);}Copy after login
Nan class implements interface Zz (master):
Implements Zz interfacepublic 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; }}Copy after login
Nv class Implement interface Zz (Master Niang):
Implement Zz interfaceTestpublic 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; }}Copy after login
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(); }}
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!