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

Random recommended