Business: in A
If (the Customer Type=="high net worth")
{
. Business A high net worth
}
The else
{
. Business A other
}
Business B
If (the Customer Type=="high net worth")
{
. Business B - high net worth
}
The else
{
. Business - other B
}
Display A:
If (the Customer Type=="high net worth")
{
. Loading of A list, disable A button, etc.
}
The else
{
.
}
B and C, D, interface, interface C
As a result, one day, suddenly said, but also get a low net worth, so the above if the else not
If (the Customer Type=="high net worth")
{
.
}
Else if (the Customer Type=="low net")
{
.
}
The else
{
.
}
Example simplifies, of course, the actual application must complex, actually very simple itself changes, the problem is
Wrote in 1, the project if the else, more, time grows, certainly can't remember where to change, will be missed, relatively large projects, it is not easy to find out all
2, if the else processing of each business is different also, want to focus on no clue
3, the business aspects of the code, can think of some way to focus, but involves the aspects of the interface, asp.net, involves a lot of page, can not think of a centralized way to
4, if next time to add a "net", and it was again, just want to make again to minimize the workload,
Using if else certainly is not a good method, also want to ask, this kind of problem, how to break? Change is not the problem, can focus your code in one place, as long as find convenient also ok
CodePudding user response:
Reminiscent of the cheat in the chicken egg button code that corresponds to the value of writing improvements, the original use the switch to write 16 lines of code, finally changed to array, much less code, examples can be,CodePudding user response:
If the business is simple, different business just change the value, the array is enough,If the business is complex, different business difference is too big, the light from the code on what can simplify,
What in fact you what this mainly displays, disable right?
It is similar to design, user role authorization to design good, what's the permissions will show what have to, the original business don't tangle,
Ready, all you have to do is to configure each type of authority,
CodePudding user response:
Feel it is ok to use the strategy patternDefinition: a unified interface
Different objects for different code implementation
CodePudding user response:
Possibly is the strategy pattern and so on, in fact I don't know much about design patterns, I don't appreciate the design patterns,I've seen other people use the design patterns of code, as to tell the story of a stiff, but not pay attention to solve the problem, in fact, the importance of design patterns as a conversation, much more than as the importance of practice,
If you ask this kind of situation what should use the design patterns, or this kind of design patterns can be used in what place, the answer often story than operability, like talking about jokes, rather than talk about engineering, the answers to these questions, don't tell you a simple judgment standard,
In fact, the question you asked very clearly, the answer is very clear, is to set up/conditions and treatment methods of mapping, and then you can according to the condition cycle or hash search process,
CodePudding user response:
Don't have special design patterns, design patterns are designed to service the seven design principles, the core is the design principle,
1.
This business you can abstract out a customer object as a base class, some customers inside some of the properties, methods, etc.
Then every such as high net worth clients to create a new high net worth clients class inherits the customer object,
2.
About save the if the else judge: build a similar factory class, ask him to help you create the object, pass in a string name to help you create a corresponding class (can be reflected in c #), then they have your business unified call the run () method, the difference between them is not the same as in the run, the customer object is created in the factory. The run (),
Then you look at the seven design principles, may find this design a good place for
CodePudding user response:
1. See condition complexity, as the above said conditions can be simple with simple way, such as factories, such as a hashmap2. Means if the condition is complex, generally there are two kinds of
A. centralized control, such as the rule engine, petri net (2 days ago and asked petri nets, somehow disliked being deleted)
B. decentralized control: such as spark, storm, flink such, based on the state machine, status messages, etc) into the state of the atomic state, decentralized decision, then each of the atoms themselves according to the decision table, decision making, the topology of self response, this kind of things can also be a neural network, said neural network exaggerated, but simple directed graph or can be, so secondary lines
After all, if the condition is complex, unified called CEP complex transaction processing, now the basic tendency of distributed control, because the rules of complex belongs to discrete system, and we are not gods, so be late to enlarge, can change, can maintain,
CodePudding user response:
Find a esper figure look, you will find that I described above, is the core of the esper architectural approachesCodePudding user response:
With the entrusted (will function as a parameter), what to do, as a function of defined by the caller, incoming,CodePudding user response:
Lost business logic to entrust the UI page logic judgment with the base class,CodePudding user response:
If too much, logic is complicatedCodePudding user response:
The thought of the polymorphic??CodePudding user response: