current position:Home>Common design patterns in Android are known only when you become a project manager
Common design patterns in Android are known only when you become a project manager
2022-02-04 16:27:04 【m0_ sixty-four million six hundred and four thousand six hundre】
public void setTitle(String msg){
}
public void setContent(String msg){
}
}
}
var builder =TestBuider.Builder()
builder.setContent("")
TestBuider.Builder builder1 = new TestBuider.Builder();
builder1.setContent("");
Strategy method pattern
public interface IUserManager {
/**
- @Decription register
**/
void register(final UserDALEx user, final ICallBackObject callBack);
/**
- @Decription land
**/
void login(final Context context, final String name, final String psw, final ICallBackObject callBack);
}
Define different implementation methods
public class BmobUserManager implements IUserManager {
@Override
public void register(UserDALEx user, final ICallBackObject callBack) {
...
}
@Override
public void login(Context context, String name, final String psw, final ICallBackObject callBack) {
…
}
Of course, it can also be implemented by other object methods . Set which method to use during application initialization .
// Initialize the cloud database
IUserManager userManager = new BmobUserManager();
Bmob.initialize(this, NCAppContext.ApplicationID);
CloudManager.getInstance().setUserManager(userManager);
...
}
Factory method model
Factory method model
We created activity perhaps fragment in , When defining a base class , Different parts of each interface can be abstracted , Let the truly implemented subclass implement the unified method .
Methods of the base class
public interface IBase
{
/**
- Function description : to view Data binding
**/
void onInitView(Bundle savedInstanceState);
/**
- Function description : Get layout file Id
**/
int getLayoutResource();
}
In different implementation subclasses activity perhaps fragment To implement the method
public class LoginActivity implements IBaseview{
@Override
public void onInitView(Bundle savedInstanceState) {
//…
}
@Override
public int getLayoutResource() {
return R.layout.activity_login;
}
}
Observer mode
https://www.cnblogs.com/fuyouG/p/fuyou-A-entryName.html
In Android, the observer mode is used more in the database changes or listview When the content changes , Use notifyDataSetChanged() Method .
public ContactListAdapter netAdapter;
mNetDataList.addAll(mList);
netAdapter.notifyDataSetChanged();
notifyDataSetChanged() The function is defined in adapter in
public final void notifyDataSetChanged() {
mObservable.notifyChanged();
}
Notify all observers to update .
Android A simple implementation of the observer pattern demo
The observer pattern is : When the state of an object changes , All objects that depend on it can be notified and automatically updated .
Here is a simple way to use ,( There is demo link ) Have a look first project Directory composition of :
ObserverListener It's the observer interface ,SubjectListener Is the observer interface ,ObserverManager It is the management class of the observer .
Copy code
1 /**
2 * Created by Administrator on 2016/9/28.
3 * Observer interface
4 _/
5
6 public interface ObserverListener {
7 void observerUpData(String content);// Refresh operation
8 }
Copy code
Copy code
1 /_*
2 * Created by Administrator on 2016/9/28.
3 * The observed interface
4 */
5
6 public interface SubjectListener {
7 void add(ObserverListener observerListener);
8 void notifyObserver(String content);
9 void remove(ObserverListener observerListener);
10 }
Copy code
The following are important management classes :
Copy code
1 public class ObserverManager implements SubjectListener {
2 private static ObserverManager observerManager;
3 // Observer interface set
4 private List list = new ArrayList<>();
5
6 /**
7 * Single case
8 _/
9 public static ObserverManager getInstance(){
10 if (null == observerManager){
11 synchronized (ObserverManager.class){
12 if (null == observerManager){
13 observerManager = new ObserverManager();
14 }
15 }
16 }
17 return observerManager;
18 }
19
20 /_*
21 * Join the listening queue
22 _/
23 @Override
24 public void add(ObserverListener observerListener) {
25 list.add(observerListener);
26 }
27
28 /_*
29 * Notify the observer to refresh the data
30 _/
31 @Override
32 public void notifyObserver(String content) {
33 for (ObserverListener observerListener : list){
34 observerListener.observerUpData(content);
35 }
36 }
37
38 /_*
39 * Remove... From listening queue
40 */
41 @Override
42 public void remove(ObserverListener observerListener) {
43 if (list.contains(observerListener)){
44 list.remove(observerListener);
45 }
46 }
47 }
Copy code
The effect is when you click ThirdActivity Button in , perform ObserverManager.getInstance().notifyObserver(“ Observer, please refresh the information ”);
Then all the added to the listening queue Activity Will update the data ,ObserverManager.getInstance().add(this) Is the method of adding to the listening queue , The update operation is performed in observerUpData(String content) In the method .
Here is 3 individual Activity, It's simple :
Copy code
1 public class MainActivity extends AppCompatActivity implements ObserverListener {
2 private TextView textView;
3 @Override
4 protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.activity_main);
7 textView = (TextView) findViewById(R.id.tv);
8 findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
9 @Override
《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 Full content open source sharing
10 public void onClick(View v) {
11 //ObserverManager.getInstance().notifyObserver(“ Observer, please refresh the information ”);
12 Intent intent = new Intent(MainActivity.this,SecondActivity.class);
13 startActivity(intent);
14 }
15 });
16
17 // register
copyright notice
author[m0_ sixty-four million six hundred and four thousand six hundre],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041627029881.html
The sidebar is recommended
- What do you think of Peng YuYan's question
- How to use the if statement to output the following results
- one hundred and eleven trillion and one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
- Then I ask for the sum difference quotient product of 3 and 2. What should I do
- There is an unhandled exception at 0x008672b2 (ucrtbased. DLL) (in 20211009. Exe): 0xc0000005: an access conflict occurred while writing to location 0xcccc.
- Mysql database needs to be migrated, and all primary key types UUID should be changed to bigint. How to deal with the foreign keys of the sub table?
- In the quick sort code, remove if (left & gt; right) return 0; After, why does the code not work properly?
- Find the sum of N rational numbers (the result is expressed by shaping / shaping)
- How to change the color of the label text of the sector chart in E-chart
- Why choose C for this question and what knowledge points
guess what you like
-
C calls WebKit kernel. How to display Chinese about input?
-
one trillion and one hundred and eleven billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
-
How does the VMware Workstation open interface look like Figure 2
-
How to write this one, everyone (JS algorithm problem)
-
MySQL finds the duplicate data of a single field or multiple fields and clears the duplicate data
-
Summary and sharing of Android construction speed optimization, two sides of meituan Android R & D post
-
Vue project Internationalization -- El table view is not updated
-
Mybatis caching mechanism (first level cache, second level cache, interview experience of Java spring recruitment in 2021
-
An exception eolesyserror in module word clock occurred during the installation of world clock exe at 0005248D. No class is registered What should I do?
-
Basic usage of mybatis, Linux interrupt handler architecture
Random recommended
- Wrote a parquet file with Bloom filter. Now you can use bloom filter when you want to read files with spark. Does spark have the corresponding implementation?
- There are four round towers with centers of (2,2), (- 2.2), (- 2, - 2), (2, - 2) and circle radius of 1, as shown in Figure 4.17. The height of the four towers is 10m, and there are no buildings outside the towers. Now enter the coordinates of any point t
- The source of DTS is self built MySQL or RDS. What is the migration scheme for MySQL?
- The source of DTS is RDS. What is the migration scheme of MariaDB?
- The source of DTS is RDS. What is the migration scheme of PPAs?
- What are the tasks in MapReduce?
- How to synchronize zoocfg configuration files in zookeeper?
- What is the migration scheme for polardb mysql, the source of DTS?
- What is the migration scheme for polardb o engine as the source of DTS?
- What is the migration scheme with polardb-x 2.0 as the source of DTS?
- What is dataX?
- What is the workflow of MapReduce?
- The source of DTS is polardb. What is the migration scheme of PostgreSQL?
- What system management functions does the metadata mechanism in the data warehouse mainly support?
- What is the migration scheme where the source of DTS is maxcompute?
- Where is the docker configuration file? How to modify the configuration?
- What is the migration scheme for Oracle as the source of DTS?
- What is the execution process of MapReduce application?
- What is the difference between left semi join and left join in data warehouse?
- What is the design architecture of dataX?
- The source of DTS is self built PostgreSQL or RDS. What is the migration scheme of PostgreSQL?
- The source of DTS is self built SQL server or RDS. What is the migration scheme of SQL Server?
- The source of DTS is self built mongodb or cloud database. What is the migration scheme for the new console of mongodb?
- Has MySQL encountered deadlock problem? How to solve it?
- What is the framework structure of dataX?
- What is the difference between dimension modeling and paradigm modeling in data warehouse?
- Serverless deconstructs the game industry's pain points. What are the fast iterations and global deployment that game customers focus on?
- What are the core components of dataX?
- The source of DTS is self built mongodb or cloud database. What is the migration scheme of the old console of mongodb?
- What is the data drift in the data warehouse?
- The source of DTS is self built redis or cloud database redis. What is the migration scheme of the new console?
- Why does MapReduce run slowly?
- Serverless deconstructs the game industry's pain points. What are the high concurrency and high deployment concerns of game customers?
- How to solve the problem of data drift in data warehouse?
- What are the prerequisites for the installation of dataX?
- The source of DTS is self built redis or cloud database. What is the migration scheme of the old console of redis?
- How to optimize SQL in daily work?
- What is the migration scheme where the source of DTS is tidb?
- What are the types of MySQL sub database and sub table?
- Serverless deconstructs the pain points of the game industry. What are the stability, reliability and security that customers pay attention to?