In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "definition and code demonstration of abstract factory in java design pattern". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Abstract factory definition Abstract Factory pattern provides an interface for creating a series of related or interdependent objects. There is no need to specify their specific classes. Applicable scenarios: 1) the client does not rely on details such as how product class instances are created and implemented. 2) emphasize that a series of related product objects (belonging to the same product family) are used together to create object objects requires a lot of repetitive code. 3) provide a library of product classes, all products appear with the same interface So that the client does not rely on the specific implementation advantages: 1) the specific products are isolated in the application layer code, do not care about the creation details 2) unify a series of product families together to create disadvantages: 1) specify all the product collections that may be created, it is difficult to expand new products in the product family, and the collection of abstract factories needs to be modified. 2) it increases the abstraction and difficulty of understanding the system.
The following figure shows the product family and product hierarchy of an abstract factory. The same line is the same product family, such as refrigerators and air conditioners made by companies that belong to the United States. And the same column is the same kind of products, such as Mei's air conditioners and Haier's air conditioners. The factory method pattern aims at the product level structure, while the abstract factory pattern aims at the product family, which is the biggest difference between the factory method and the abstract factory pattern.
2. Code demonstration public abstract class Video {public abstract void produce ();} public class JavaVideo extends Video {@ Override public void produce () {System.out.println ("recording Java course video");}} public class PythonVideo extends Video {@ Override public void produce () {System.out.println ("recording Python course video");}} public abstract class Article {public abstract void produce () } public class JavaArticle extends Article {@ Override public void produce () {System.out.println ("writing Java course notes");}} public class PythonArticle extends Article {@ Override public void produce () {System.out.println ("writing Python course notes");}} public interface CourseFactory {Video getVideo (); Article getArticle () } public class JavaCourseFactory implements CourseFactory {@ Override public Video getVideo () {return new JavaVideo ();} @ Override public Article getArticle () {return new JavaArticle ();}} public class PythonCourseFactory implements CourseFactory {@ Override public Video getVideo () {return new PythonVideo ();} @ Override public Article getArticle () {return new PythonArticle () }} public class Test {public static void main (String [] args) {CourseFactory courseFactory = new JavaCourseFactory (); Video video = courseFactory.getVideo (); Article article = courseFactory.getArticle (); video.produce (); article.produce ();}} "definition and code demonstration of abstract factories in java design pattern" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.