Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Definition and Code demonstration of Factory pattern in java Design pattern

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 relevant knowledge of "definition and code demonstration of factory pattern in java design pattern". In the operation of actual cases, many people will encounter this 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. The factory method definition defines an interface to create an object, but lets the class that implements this interface decide which class to instantiate, and the factory method defers class instantiation to a subclass. Applicable scenarios: 1) creating objects requires a lot of repetitive code 2) the client (application layer) does not depend on details such as how product class instances are created and implemented. 3) A class specifies which object to create through its subclasses: 1) the user only needs to care about the factory corresponding to the desired product, not the creation details. 2) adding a new product conforms to the opening and closing principle. Disadvantages of improving scalability: 1) the number of classes is easy to be too many, and the complexity is increased; 2) it increases the abstractness and difficulty of understanding of the system; 2. Code demonstration.

Public abstract class Video {public abstract void produce ();} public class PythonVideo extends Video {@ Override public void produce () {System.out.println ("recording Python course videos");}} public class JavaVideo extends Video {@ Override public void produce () {System.out.println ("recording Java course videos");}} public abstract class VideoFactory {public abstract Video getVideo () } public class PythonVideoFactory extends VideoFactory {@ Override public Video getVideo () {return new PythonVideo ();}} public class JavaVideoFactory extends VideoFactory {@ Override public Video getVideo () {return new JavaVideo ();} public class Test {public static void main (String [] args) {VideoFactory videoFactory = new PythonVideoFactory (); Video video = videoFactory.getVideo (); video.produce (); videoFactory = new JavaVideoFactory () Video = videoFactory.getVideo (); video.produce ();}} "definition and code demonstration of factory patterns 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report