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

How to implement Factory method pattern in Android

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to achieve the factory method pattern in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this Android article on how to achieve the factory method pattern. Let's take a look at it.

1. Intention

Define an interface for creating objects and let subclasses decide which class to instantiate. The factory mode pattern delays the instantiation of a class to its subclasses.

Buzzword: the fabricator delays creating object subclasses

two。 Structure diagrams and codes

Let's first look at the standard factory method structure diagram:

First abstract product class, abstract factory class, and then use the client specific factory to produce the corresponding specific products, but the client does not know how the specific products are produced, and the production process is encapsulated in the factory. So, to some extent, the factory method pattern changes the way we create objects directly with new, which is a good start and makes a lot of sense.

Take ThreadFactory as an example:

This diagram is actually slightly different from the original structure diagram, that is, the parameterized factory, and in the business sense is also a little different, but the idea is the same.

Let's take a look at the specific code:

/ / Abstract product public interface Runnable {public abstract void run ();} / Abstract Factory public interface ThreadFactory {Thread newThread (Runnable r);}

The following is the specific implementation:

For example, the specific implementation of a factory in the AsyncTask class is as follows:

/ / Factory implementation class private static final ThreadFactory sThreadFactory = new ThreadFactory () {private final AtomicInteger mCount = new AtomicInteger (1); public Thread newThread (Runnable r) {return newThread (r, "AsyncTask #" + mCount.getAndIncrement ());}}; / / so where is the product class? / / as a parameter Runnable r, we can create tens of thousands of product classes of this series / / similarly, we can create another similar factory to produce some kind of specialized thread, which is very easy to expand.

Seeing here, on the one hand, we sigh for its production convenience, on the other hand, we feel tedious that we have to create a factory without creating a certain kind of products, so we introduce the simple factory below, and its structure is as follows:

Simple factories get rid of abstract factories, and you just create one that specializes in producing certain types of products. It is very practical and convenient to apply this model in some specific but irresponsible areas.

This class is used in the Connection class in android:

Connection, an abstract class, acts as both an abstract product class and a concrete factory class.

Because in this case, we often need to produce subclasses right away, the getConnection method is often static, so a simple factory is also called a static factory method.

Let's look at the code as follows:

Abstract class Connection {static Connection getConnection (Context context, HttpHost host, HttpHost proxy, RequestFeeder requestFeeder) {if (host.getSchemeName (). Equals ("http")) {return new HttpConnection (context, host, requestFeeder);} / Otherwise, default to https return new HttpsConnection (context, host, proxy, requestFeeder) }} this is the end of the article on "how to implement the factory method pattern in Android". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to realize the factory method model in Android". 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

Development

Wechat

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

12
Report