mb61ab4da9006e7 2022-01-26 13:16:20 阅读数:612
// // If the request fails, skip directly , Request registration agreement
// checkRegisterAgreement();
checkNeedShowH5();
}
});
}
private void showADialog() {
new AlertDialog.Builder(this)
.setTitle(" This is an advertisement with attitude ")
.setPositiveButton(" I have watched all of it ", null)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
// // After the pop-up, request to register the agreement
// checkRegisterAgreement();
// Now the product is going to insert a H5 Page priority request
checkNeedShowH5();
}
}).create().show();
}
private void checkRegisterAgreement() {
Utils.fakeRequest(" http://www.api2.com", new HttpCallBack() {
@Override
public void onOk() {
showBDialog();
}
@Override
public void onFailure() {
//do nothing
}
});
}
private void showBDialog() {
new AlertDialog.Builder(this)
.setTitle(" This is the registration agreement ")
.setPositiveButton(" I have watched all of it ", null)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//do nothing
}
}).create().show();
}
private void checkNeedShowH5() {
Utils.fakeRequest(" http://www.api3.com", new HttpCallBack() {
@Override
public void onOk() {
toH5Page();
}
@Override
public void onFailure() {
checkRegisterAgreement();
}
});
}
private void toH5Page() {
startActivityForResult(new Intent(this, TestH5Activity.class), REQUEST_CODE_H5);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_H5:
checkRegisterAgreement();
break;
default:
break;
}
}
First, the original step1 The registration protocol will not be called after completion , Instead, call the request H5 Methods .
Due to go H5 It's a Activity The jump , So we are onActivityResult In, we continue the previous registration protocol call .
Look at the general demo effect :
later … Several iterations , The first pop-up frame and page Jump on the home page have been 7、8 A the , Every time a product has similar needs , We're going to repeat the above process again , Sort it out for about half a day each time .
Did you find any problems ?
1. Strong coupling between home page sequences , Every time you want to insert another pop-up box or page in front of both , We have to modify the call chain before and after it every time , Make at least three changes , It's easy to miss , But in fact, they are in addition to each other's order , There's no other connection .
2. Every time a new requirement comes, it is necessary to completely sort out the original logic , It's a waste of time , Affect efficiency .
What do I do ?
1. Can we uniformly manage the things to be handled in one chain , Everything is not related to each other , The order between them can be easily replaced with a simple configuration .
2. Later defenders , You can clearly know the order of calls , There is no need to comb the whole business code every time .
1. Can we abstract everything we have to do into a node , Each node only cares about whether its task is completed , It doesn't know what it is , I don't know who is in front of or behind it .
2. Each node is managed by a flow , It acts as a global coordinator , You can control starting from any node 、 Control the opening and ending of the whole flow , The order of each node is managed by the flow .
With the above design ideas , I refactored the code , This is what the code is like :
public class AfterActivity extends AppCompatActivity {
private static final int REQUEST_CODE_H5 = 1;
/**
*/
private static final int NODE_FIRST_AD = 10;
/**
*/
private static final int NODE_CHECK_H5 = 20;
/**
*/
private static final int NODE_REGISTER_AGREEMENT = 30;
private WorkFlow workFlow;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startWorkFlow();
}
private void startWorkFlow() {
workFlow = new WorkFlow.Builder()
.withNode(getFirstAdNode())
.withNode(getShowRegisterAgreementNode())
.withNode(getShowH5Node())
.create();
workFlow.start();
}
private WorkNode getFirstAdNode() {
return WorkNode.build(NODE_FIRST_AD, new Worker() {
@Override
public void doWork(final Node current) {
Utils.fakeRequest(" http://www.api1.com", new HttpCallBack() {
@Override
public void onOk() {
new AlertDialog.Builder(AfterActivity.this)
.setTitle(" This is an advertisement with attitude ")
.setPositiveButton(" I have watched all of it ", null)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
// Just care about whether you finish , The next node will automatically execute
current.onCompleted();
}
}).create().show();
}
@Override
public void onFailure() {
// Just care about whether you finish , The next node will automatically execute
current.onCompleted();
}
});
}
});
}
private WorkNode getShowRegisterAgreementNode() {
return WorkNode.build(NODE_REGISTER_AGREEMENT, new Worker() {
@Override
public void doWork(final Node current) {
Utils.fakeRequest(" http://www.api2.com", new HttpCallBack() {
@Override
public void onOk() {
new AlertDialog.Builder(AfterActivity.this)
.setTitle(" This is the registration agreement ")
.setPositiveButton(" I have watched all of it ", null)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
current.onCompleted();
}
}).create().show();
}
@Override
public void onFailure() {
current.onCompleted();
}
});
}
});
}
private WorkNode getShowH5Node() {
return (WorkNode.build(NODE_CHECK_H5, new Worker() {
@Override
public void doWork(final Node current) {
Utils.fakeRequest(" http://www.api3.com", new HttpCallBack() {
@Override
public void onOk() {
startActivityForResult(new Intent(AfterActivity.this, TestH5Activity.class), REQUEST_CODE_H5);
}
@Override
public void onFailure() {
current.onCompleted();
}
});
}
}));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_H5:
workFlow.continueWork();
break;
default:
break;
}
}
}
After the above reconstruction , Now the home page process :
1. There is no correlation between the several things to do when entering the home page , Their positions can be switched arbitrarily , Only need to change id You can easily adjust their execution order .
2. If you want to join or insert nodes, you don't need to change the original logic .
[ Project source code portal ]( )
Realize the idea
1. I want each task to be independent of each other , Only care about whether their own things are completed , I abstract it into a node , Each node has its own node id and The method of completion :
public interface Node {
/**
node id
*/
int getId();
/**
*/
void onCompleted();
}
As for why id, I'll talk about it later .
Let's take a look at its implementation class WorkNode Core code :
public class WorkNode implements Node {
/**
*/
private int nodeId;
/**
*/
private Worker worker;
private WorkCallBack callBack;
public static WorkNode build(int nodeId, Worker worker) {
return new WorkNode(nodeId, worker);
}
/**
*/
public WorkNode(int nodeId, Worker worker) {
this.nodeId = nodeId;
this.worker = worker;
}
/**
from workFlow To call
*/
void doWork(WorkCallBack callBack) {
this.callBack = callBack;
worker.doWork(this);
}
@Override
public int getId() {
return nodeId;
}
@Override
public void onCompleted() {
if (null != callBack) {
callBack.onWorkCompleted();
}
}
interface WorkCallBack {
/**
*/
void onWorkCompleted();
}
}
A node is passed in the constructor id, and Worker, This Worker Of doWork The implementation of method is what our node really wants to do :
public interface Worker {
/**
Perform tasks
*/
void doWork(Node current);
You don't step out , Never know your potential , Don't be bound by the shackles of this society on us ,30 I'm not afraid ,35 I'm not afraid of , Do what you want to do , Fight for yourself ! How do you know you can't do it without trying ?
Change your life , There is no shortcut , This road needs to be taken in person , Only in-depth thinking , Constantly reflect and summarize , Keep your passion for learning , Step by step to build their own complete knowledge system , Is the ultimate way to win , It's also the mission of programmers .
Enclosed : We collected 20 sets of first-line and second-line Internet companies because of autumn recruitment Android The real question of the interview ( contain BAT、 millet 、 Huawei 、 Meituan 、 sound of dripping water ) And I sort it out myself Android Review notes ( contain Android Basic knowledge points 、Android Expand your knowledge 、Android The source code parsing 、 Design pattern summary 、Gradle Knowledge point 、 Summary of common algorithm questions .)
This article has been CODING Open source project :《Android Summary of learning notes + Mobile architecture video + The real interview question of Dachang + Project source code 》 Included
copyright:author[mb61ab4da9006e7],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261316189797.html