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

When can I use the web factory method

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces in what circumstances you can use the web factory method related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you can read this article in what circumstances you can use the web factory method will have a harvest, let's take a look at it.

Applicability

Factory Method mode can be used in the following situations:

When a class does not know the class of the object it must create.

When a class needs to create an object but cannot know its type information in advance. For example, the application / document architecture mentioned earlier. The users of this architecture are developers and thousands of applications have been developed, and the provider of the architecture certainly has no way to know the type of object that needs to be created. You can use the factory method pattern to determine the type of object you want to create.

I really don't know about this situation.

When a class wants its subclasses to specify the objects it creates.

If our design is not for a general architecture, but for a specific application scenario, then in most cases we can know (or at least most of) the specific types of objects that need to be created. But for some reason (future extensions, decentralized development requirements, and so on), you still want to hand over the responsibility of creating the actual object to its subclasses.

This kind of situation is pretending not to know.

When a class delegates the responsibility of creating an object to one of several help subclasses, and you want to localize the information about which help subclass is the agent.

This sentence is difficult to understand, let's look at the original text.

Classes delegate responsibility to one of several helper subclasses, and

You want to localize the knowledge of which helper subclass is the delegate.

The responsibility entrusted in the first half of the original sentence does not clearly indicate that it is the responsibility of creating the object, which is likely to be due to inaccurate translation. The following example illustrates the author's understanding, and the class diagram is as follows:

Our example is graphic editing software that supports Rect and Circle graphics, both of which are subclasses of the Shape class. In addition, for some reason (for example, we hope that graphics can be represented in different operating systems in the future) we want to delegate the drawing function of their graphics to two help subclasses, RectDrawer and CircleDrawer. As shown in the figure above, different Drawer subclasses are created due to the use of the factory method pattern. Information about which Drawer subclass the drawing function is delegated to is limited to the specific graphics subclass.

This is the end of the article on "under what circumstances can you use the web factory method?" Thank you for reading! I believe you all have a certain understanding of "under what circumstances you can use the web factory method". If you want to learn more, you are welcome to follow the industry information channel.

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