In charge of interview

The supervisor called me and said: “Xiao Chen, the Spring Festival is coming. In order to facilitate our colleagues to inquire about train tickets, please develop a train ticket inquiry system.” I thought, this system is too simple for me.

I started the development of this requirement.

The requirements development

After requirements development, there are Easter eggs, be sure to stick to it.

This is a simple query system, example code is as follows:

Step 1: Define a ticket interface.

package com.example.createproject; Public interface Ticket {/** * public interface Ticket {/** * Public interface Ticket {/** * Public interface Ticket {/ / void showTicketInfo(); }Copy the code

Step 2: Define the fireman ticket implementation class.

package com.example.createproject; import android.util.Log; import java.util.Random; public class TrainTicket implements Ticket { private static final String TAG = TrainTicket.class.getSimpleName(); private String from; private String to; private String price; private String puwei; Public TrainTicket(String from, String to, String puwei) {this.from = from; this.to = to; this.puwei = puwei; } @Override public void showTicketInfo() { price = new Random().nextInt() + ""; Log.e(TAG, "+ from +" to "+ to +"); }}Copy the code

Step 3: Define the management class of train tickets.

package com.example.createproject; public class TrainTicketManager { public static TrainTicket getTicketInfo(String from, String to, String puwei) { return new TrainTicket(from, to, puwei); }}Copy the code

Step 4: Users go to check train ticket information.

TrainTicket ticketInfo = TrainTicketManager. GetTicketInfo (" shenzhen ", "xian", "on");Copy the code

Write to achieve the above functions, I handed over to the supervisor, hope to get supervisor praise: high efficiency, query fast… .

The supervisor made another appointment

The supervisor called me over and said, “Chen, don’t you dare show me such a system? The getTicketInfo method returns a TrainTicket object every time. If our system has 10 million people query at the same time, won’t we create 10 million objects and the company’s server will explode?” When I heard it, I thought it wasn’t the server that was going to explode, it was my head. The director looked at my confusion and said, “You can use the meta-design model to refactor and optimize.”

I am very keen to capture the key word: enjoy yuan design pattern.

My supervisor told me: In the following cases, you can consider the meta design pattern.

Enjoy the scenario of the yuan design pattern

1. When a large number of similar objects exist in a system.

2, if the system needs to design buffer pools.

For example, in the ticket query system, there will be a large number of ticket information objects, and for a car K5038, it is a fixed number of seats, such as 500. If we have 5 million queries, if there are 5 million ticket objects, it is obviously unreasonable, so we can optimize as follows.

Question to consider: Where does Android use pooling, or caching, as a design concept?

1. The idea of three-level caching in the image frame. Caches images in memory and local.

The thread pool idea used in the network framework.

3, custom control properties set TypeArray is also used in the bottom of the pool, we can use typearray.recycle (); To see it.

Message.obtain() Message is obtained by message.obtain (), which uses a linked list rather than a Map to obtain a Message. If there is none in the cache pool, create a new one.

Optimize the process

For the above process, we can do a simple optimization.

package com.example.createproject; import java.util.HashMap; import java.util.Map; Public class TrainTicketManager {static Map<String, TrainTicket> Map = new HashMap<>(); public static TrainTicket getTicketInfo(String from, String to, String puwei) {// key value String key = from + "-" + to + "-" + puwei; If (map.containsKey(key)) {return map.get(key); if (map.containsKey) {return map.get(key); } else { TrainTicket trainTicket = new TrainTicket(from, to, puwei); map.put(key, trainTicket); return trainTicket; }}}Copy the code

What is the premium mode

From the above example, we can see that the share mode has the following characteristics:

1, share mode internal maintenance of a buffer pool, this buffer pool can enable Map, can enable linked lists, can enable other data interface, choose the most appropriate.

2. Its core idea is actually the idea of caching.

3, the use of share mode to create a system, can avoid creating too many objects.

The director went on to tell me, and we graphically illustrated the process above

Enjoy the UML class diagram of the meta-pattern

conclusion

1. The metadata mode is relatively simple, mainly using the use of cached data structures, such as Map, etc.

2, can greatly reduce the number of objects created, reduce the use of program memory, enhance the performance of the program.