Home > Net >  C # beginners, for the project, increase the if the else judge lead to modify the quantity is too la
C # beginners, for the project, increase the if the else judge lead to modify the quantity is too la

Time:11-23

For example, such as customers, a customer categories, a total of two kinds of high net worth clients, other customers, display interface, business logic, etc., so, the easiest way to
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 pattern
Definition: 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 hashmap
2. 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 approaches

CodePudding 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 complicated

CodePudding user response:

The thought of the polymorphic??

CodePudding user response:

reference 13 floor response: thousand dreams life
thought of polymorphic??

Is a bit of a polymorphic expression in there, the same code invocation style, not the same as the calling code

CodePudding user response:

The class Customer
{
Virtual business (A)=0;
Virtual business (B)=0;
. Increase business
}

Class 1: Customer Customer
{

}

Class 2: Customer Customer
{

}

.

CodePudding user response:

Use an array can be ok

CodePudding user response:

Before doing a project, a lot of input interface of a box, some input box need mandatory input, while others don't need, whether you need mandatory input is set according to some parameters, such as commodity type, province, etc., if a hard-coded feel very trouble, then designed a XML, roughly as follows:

Namely the if the else all use XML to configure the judgment of the conditions, then just write to handle XML code, input parameters such as input box type id and goods, roughly the logic is that according to the first input box id to find XML is there, if you have, then find its properties "product type", if the value is the same as the input parameter, then need to mandatory input, or instructions do not need to force input, defined here a simple domain specific language, namely a; b; Said a or b,! A for the a, and so on, to facilitate processing is a bit complicated situation,
Benefit is judgment when conditions change, need to modify the XML, without having to move the code,

Back to the original poster example, whether can consider to design the XML:


Only need to modify XML for

CodePudding user response:

All the specific operation method, still, just above the concrete if the else, or switch, tagging, feeling pretty clear,

CodePudding user response:

N multiple complex logic, how do you want to,

CodePudding user response:

People feel with the switch than if the else looks more intuitive, next time, even if add what condition, as long as it is good to add a case again,

Example:
The switch (the Customer Type) :
{
Case "of" high net worth 1:
{
.
break;
}
Case 2 "high net worth" :
{
.
break;
}
Lower case "net" :
{
.
break;
}
Default:
{
break;
}
}

CodePudding user response:

In this scenario, you need not any special design pattern,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related