In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "JAVA abstract factory pattern is what and how to achieve", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "JAVA abstract factory pattern is what and how to achieve" it!
What is Abstract Factory pattern
Abstract factory (AbstractFactory) pattern definition: a pattern structure that provides an interface for the access class to create a set of related or interdependent objects, and the access class can get different levels of products of the same family without specifying the specific class of the desired product.
Meet the conditions:
There are multiple product families in the system, and each specific factory creates products that belong to the same family but belong to different hierarchical structures.
The system can only consume one family of products at a time, that is, products of the same family can be used together.
Advantages:
The associated multi-level products in the product family can be managed together within the class, without having to introduce several new classes to manage them.
When a product family is needed, the abstract factory ensures that the client always uses only product groups of the same product.
Abstract factory enhances the expansibility of the program, when adding a new product family, there is no need to modify the original code to meet 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 of the system and the difficulty of understanding.
Implementation of Abstract Factory
The main roles of the case are as follows:
Abstract Factory (Abstract Factory): the inheritance of the Seven
Factory (SimpleFactory): master, wife
Abstract products (Product): extraterrestrial meteorites (raw materials for casting swords), a nest of dove eggs (unhatched pigeons)
Specific products (ConcreteProduct): seven swords. Seven spiritual pigeons
Case study:
The master has seven heritages and will build the sword of Zhiyang and train male pigeons.
The teacher's wife has seven heritages and can build the sword of Yin and cultivate female spirit pigeons.
The Seven Warriors asked the master mother to build his own sword and adopt his own little dove.
Abstract product, product
S1 interface (extraterrestrial meteorite):
A method is declared in the interface
Public interface S1 {void show ();}
S2 interface (a nest of dove eggs):
A method is declared in the interface
Public interface S2 {void show ();}
Class A1 implementation interface S1 (Changhong Jian):
Implement S1 interface
Public class A1 implements S1 {@ Override public void show () {System.out.println ("built a Changhong sword");}}
Class B1 implementation interface S1 (ice sword):
Implement S1 interface
Public class B1 implements S1 {@ Override public void show () {System.out.println ("built an ice sword");}}
Class C1 implementation interface S1 (Ziyunjian):
Implement S1 interface
Public class C1 implements S1 {@ Override public void show () {System.out.println ("built a Ziyun sword");}}
Class D1 implementation interface S1 (rain foil):
Implement S1 interface
Public class D1 implements S1 {@ Override public void show () {System.out.println ("built a Ziyun sword");}}
Class E1 implementation interface S1 (Qingguangjian):
Implement the Ss interface
Public class E1 implements S1 {@ Override public void show () {System.out.println ("built a Ziyun sword");}}
Class F1 implements interface S1 (Ben Lei Jian):
Implement the Ss interface
Public class F1 implements S1 {@ Override public void show () {System.out.println ("built a Ziyun sword");}}
Class G1 implements interface S1 (whirlwind sword):
Implement the Ss interface
Public class G1 implements S1 {@ Override public void show () {System.out.println ("built a Ziyun sword");}}
Class A2 implementation API S2 (Lingdove No.1):
Implement S1 interface
Public class A2 implements S2 {@ Override public void show () {System.out.println ("Pigeon 7");}}
Class B2 implementation interface S2 (Ling Pigeon 2):
Implement S1 interface
Public class B2 implements S2 {@ Override public void show () {System.out.println ("Pigeon 7");}}
Class C2 implementation interface S2 (Lingdove 3):
Implement S1 interface
Public class C2 implements S2 {@ Override public void show () {System.out.println ("Pigeon 7");}}
Class D2 implementation interface S2 (Lingge 4):
Implement S1 interface
Public class D2 implements S2 {@ Override public void show () {System.out.println ("Pigeon 7");}}
Class E2 implementation interface S2 (Lingdove 5):
Implement the Ss interface
Public class E2 implements S2 {@ Override public void show () {System.out.println ("Pigeon 7");}}
Class F2 implementation interface S2 (Lingdove 6):
Implement the Ss interface
Public class F2 implements S2 {@ Override public void show () {System.out.println ("Pigeon Seven");}}
Class G2 implementation API S2 (Lingdove 7):
Implement the Ss interface
Public class G2 implements S2 {@ Override public void show () {System.out.println ("Ling Pigeon 7");} abstract factory, factory
Zz interface (seven heritages):
A sword casting method abc1 () and a pigeon raising method abc2 () are declared in the interface.
Public interface Zz {S1 abc1 (String a); S2 abc2 (String a);}
The Nan class implements interface Zz (master):
Implement the Zz interface
Public class Nan implements Zz {private static S1 S1; private static S2 S2; public S1 abc1 (String a) {if ("Changhong Jian" .equals (a)) {S1 = new A1 ();} else if ("Benlei Jian" .equals (a)) {S1 = new F1 ();} else if ("Qingguang Jian" .equals (a) {S1 = new E1 () } else if ("rain foil" .equals (a)) {S1 = new D1 ();} else if ("whirlwind sword" .equals (a)) {S1 = new G1 ();} else {System.out.println ("sword casting failure");} return S1 } public S2 abc2 (String a) {if ("Lingge No.1" .equals (a)) {S2 = new A2 ();} else if ("Lingge II" .equals (a)) {S2 = new F2 ();} else if ("Lingge III" .equals (a)) {S2 = new E2 () } else if ("Lingge 4" .equals (a)) {S2 = new D2 ();} else if ("Lingge V" .equals (a)) {S2 = new G2 ();} else {System.out.println ("adoption failure");} return S2;}}
Nv class implementation interface Zz (madam):
Implement the Zz interface
Public class Nv implements Zz {private static S1 S1; private static S2 S2; public S1 abc1 (String a) {if ("Bing Bing Jian" .equals (a)) {S1 = new B1 ();} else if ("Ziyun Jian" .equals (a)) {S1 = new C1 ();} else {System.out.println ("sword casting failure") } return S1;} public S2 abc2 (String a) {if ("Lingge 6" .equals (a)) {S2 = new B2 ();} else if ("Lingge VII" .equals (a)) {S2 = new C2 ();} else {System.out.println ("adoption failure") } return S2;}}
test
Test class:
Public class Demo {public static void main (String [] args) {System.out.println ("- Zhiyang inheritance -"); / / Master Nan nan = new Nan (); / / Sword maker S1 A1 = nan.abc1 ("Changhong sword"); S1 D1 = nan.abc1 ("rain foil") S1 E1 = nan.abc1 ("Qingguang sword"); S1 F1 = nan.abc1 ("Benglei sword"); S1 G1 = nan.abc1 ("whirlwind sword"); / / raising pigeons S2 a2 = nan.abc2 ("Lingge 1"); S2 D2 = nan.abc2 ("Ling Pigeon 2"); S2 e2 = nan.abc2 ("Ling Pigeon 3") S2 f2 = nan.abc2 ("Lingge 4"); S2 G2 = nan.abc2 ("Lingge 5"); a1.show (); d1.show (); e1.show (); f1.show (); g1.show (); a2.show (); d2.show (); e2.show (); f2.show () G2.show (); System.out.println ("- Zhiyin inheritance -"); / / female swordsmen Nv nv = new Nv (); S1 b1 = nv.abc1 ("Bing Bing Jian"); S1 C1 = nv.abc1 ("Ziyun Jian"); b1.show (); c1.show () S2 b2 = nv.abc2 ("Lingge 6"); S2 c2 = nv.abc2 ("Ling Pigeon 7"); b2.show (); c2.show () }} Thank you for your reading, the above is the content of "what is the JAVA Abstract Factory pattern and how to implement it". After the study of this article, I believe you have a deeper understanding of what the JAVA Abstract Factory pattern is and how to realize it. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.