[Job is reposted] you are still using if ele?

  [Job is reposted] you are still using if else? 

  Panchiao里人http://www.jdon.com 2006/1/11 (reproduced retained) 

  Process-oriented design and object-oriented design is the main difference: whether the use of lengthy Yewuluojiceng if else judgement.    If you are still widely used if else, of course, except the interface layer performance, even if you use Java / C # this fully object-oriented language, that you can only stay in the traditional thinking of the process-oriented language. 

  Analysis of traditional thinking habits 

  Why Yewuluojiceng use if else, the fact is the purpose of users to reuse, but this is the reuse process-oriented programming, programmers can only see code reusability, because he had seen several circumstances if else most of the code are repeated, only different individual, the use of code duplication can be avoided if else, and think that this is template Template mode. 

  He is wrong Fan: programmers only from the running order of the code in this direction to look at the code, such thinking similar pipes or serial circuit, water mains along the flow (code running order), there are several charge (a tube), in charge of several assigned to the mobile, met here at the code if else has. 

  And the use of OO, the first to break the code from running down in the same order when the order allowing this rule, and allowing the implementation of the code structure was not decided by what decision?    OO design by the design of these models will be replaced if else, but always by a final such as the Service in accordance with the operating sequence assembly of these OO module, only one, which may include services, general Service is, in the EJB Session bean. 

  Once demand changes, we may be more in various OO Service Module, or even only changes in the Service Module OO implementation of the order will be able to meet the demand. 

  Here we also see OO separation of ideas, the language will be a process before Main function completely biodegradable, and will run code sequence separated from other logic, and not as chaotic as the process-oriented together.    So it was mixed feelings, OO is to the order, which is in the affirmative, and the key is to separate operation of the order separated. 

  Are there can be seen if else do you have running order from home. 

  Design Mode entry point 

  Often reflect the design model is good, but I find it difficult to use, in fact if you use if else to write code (except Display Control outside), the business logic is written, but the use of simple language to the judgement of the situation as a reality the alternative. 

  We are familiar with or to the forum posts, for example, such as ForumMessage is a model, but the actual posting of two properties: the theme paste (a root paste), and to verify the (previous posting to the posts), there is a simple solution : 
  The establishment of a ForumMessage, and then joined isTopic ForumMessage statement that a judgement, attention to you here attribute the judgement of a simple introduction, the procedure may lead to other parts of your presence everywhere if else judgement. 

  If we switch to another realization of ideas to the concept of object-oriented view, the actual theme in the paste and verify that the two objects, but most of these targets is the same, I will, therefore, expressed as a theme ForumMessage stickers and create a subclass inheritance ForumMessage ForumMessageReply as replies, so, I place in the process, such as Service, I have determined that the Model is to verify, I will direct the ForumMessageReply can be traced back to this release is similar to Collection Remove all objects, and the mandatory conversion.    Through this means I eliminate the proceedings after the judge sentences if else may arise. 

  From embodied here, and if analysis of the wrong direction would lead to the misuse of mode. 

  Discuss design patterns, for example, can not do business context of the case scenarios, not to decide whether the mode, the following comparison cite two examples: 

  1. To the posting of the code, for example the first case is not the context of the article, that there is a code only: 

  Main () ( 

  If (case A) ( 

  / / Do with strategy A 

  ) Else (case B) ( 

  / / Do with strategy B 

  ) Else (case C) ( 

  / / Do with strategy C 

  ) 

  ) 

?

  This code simply code, no business functions, therefore, in such circumstances, we will have a difficult determining that the use of the mode is with a certain strategy mode, or use if else can not escape the fate of the design model is not magic and can not be a section of the code has become meaningless simple, and can only be embodied in the business functions can be more easily expanded. 

  The second in this posting, the author cited an PacketParser business case, this code is embodied business functions, is a data packet analysis, the authors also compare different models use different, we still use Dynamic Proxy mode Command mode or to eliminate those that may exist if else 

  The above two examples demonstrate: We use the business logic design pattern is the starting point, and decomposition of business logic, we may use habits if else to realize that when you have such an attempt has been achieved or code, then they should be Refactoring consider the need for the reconstruction. 

  If else alternatives 

  Then a real war, which design patterns can be replaced if else?    In fact GoF design patterns can be used as a substitute if else, we were described as follows: 

  •   When the data object model state of the possibility of existence of the state, and this state will be affected to different business results, then we should consider whether or not to use state model, of course, use the model, you must first have the memory state concept, rather than the database concept, as in the traditional process-oriented /-oriented database system, you state that it is very difficult, from a reader value in the database, then the value of code triage, which is Many beginners often dry matter.    Reference article: state Target: database alternative to the use of traditional thinking of the language are: the use of a logo of the state integer variables: 

    ?

      Public class Order ( 

      Private int status; 

      / / Note: 

      / / = 1 status but said orders for the Show; 

      / / Status = 2 Show that has been pending; 

      / / = 3 status that has been dealing with payment 

      / / Status = 4 that had already paid Unfilled 

      / / Status that has been shipping = 5 

      ) 

      Design of the above categories is undoubtedly will be used as a function of the traditional language to use, this process resulted in a large number of code if else. 

  •   When you have a strategy model algorithm, or formula several options could be considered strategy model, the traditional process of language is: read from the database numerical algorithms, numerical 1 that a strategy, for example, saved to the database; numerical 2 for 2 said that strategy, For example XMl to preserve documents.    If else here use as a strategic option of switching. 
  •   Command of the traditional mode of thinking is: If the client is issued a code or "A", then I call A.java to deal with this object if the code is 2 or "B", I call B.java to deal with, Through if else to judge sent from the client code, and then click the corresponding table prior agreement, calling to deal with the corresponding category. 
  •   MVC model MVC-traditional language misuse and Command pattern similar, in a Class Action, the use of before and after Taiwan if else for scheduling, what if the client delivery orders; background, I call on what the outcome, if any background processing structure, and then decide What pushed pages, but now we use Struts / JSF such MVC model frameworks to achieve this would not have Fan junior wrong. 
  •   Chain duties and responsibilities chain Command mode is optional, if you really do not know what the client code will be issued, nor did a pre-defined table, then you can only prepare a touch of class to open the same luck look at the package can be.    And the Command is different in the AOP vs Decorator in a text analysis. 
  •   Acting Deputy agent mode or dynamic object is in line with certain conditions can be represented, for instance, competence test, the traditional process-oriented thinking is: When a user landing after a visit to resources, the use of judgement if else, only certain conditions meet, to allow access to such data business judgement and competence of logic confusion with the use of proxy mode can be clearly separated, if the suspicion is not a good use of dynamic agents, or below, including AOP. 
  •   In fact, or Decorator AOP mode filter using filter can be replaced in our business if else, filters play a role in filtering and screening will comply with the conditions of this filter down to intercept the target of something, which is a filter function, a number of filters combined if else is actually a combination. 
      So, if you really can think of any way, you can use filters, filter as a firewall to better understanding of the client when there is a request, the nature of different firewall, the firewall is to intercept port; that firewall security checks is to intercept, and so on.    Filters also like all kinds of red-blue filters; red filter only through a red light in the interception; blue light filters to intercept down in the blue, which is actually light for use if else decomposition. 

      Figure through a filter conditions we were able to make a three-dimensional separation of the signal, if you use if else that you will map the conditions of 1/2/3/4 combined, in the same place Conditions judgement. 
      Filter requires in-depth understanding of the details and the realization of small differences, please refer to the article: AOP vs Decorator 

      OO design summary 

      There is also a pseudo-mode, although the use of such a model state, but in reality is the use of internal models if else, or switch to an important condition for the state switching or judgement, then no doubt that further efforts were needed.    More importantly, the model can not be self-proclaimed, and a book showing people. 

      Object-oriented real grasp these ideas is a difficult thing, there are all kinds of hair Jiuzhuo their upward Stubbs captions, are Wurenzidi, therefore, I think that beginners read Thinking in Java (Java programming ideas) no use, it tried to level terms from OO programming language ideology, have failed miserably, as the language reference books can be, but as a Java OO embodied in the thinking of learning materials, be wrong. 

      OO programming methodology is a kind of thinking, if there is no comparison methodology is unable to understand the characteristics of this methodology, ancient Zen is a methodology, Wushan is all carried water on these applications can of chopped wood. 

      What is so OO thinking can be applied to the experience?    GoF design pattern is, GoF design model is the software equivalent of all carried water and other basic living chopped wood, so that if a programmer does not have even the basic living, the self-proclaimed why he OO programmers?    OO programming professionals engaged in the design work, if we do not grasp basic skills design patterns, as a monk who do not want to do all carried water chopped wood, and why he based this industry?    Has long been masters downhill rush. 

      Concluded: if else will be used in small areas can be, such as a simple numerical judgement, but if you accordance with the traditions and customs of thinking in the implementation of business functions also use if else, then thinking that you may need remodeling, you Programming experience more rich, the traditional mode of thinking process can be easily rooted and seeks to change its very difficult to accept the proposed storm-minded professional training. 

      Summed up in one sentence: If you do a lot of systems, a long time did not use if else, so that you may enter the real position of the OO design.    (This is my own invention of the actual combat evaluation of the measurement standard). 

  •   Posted on 2006-09-16 18:45 Xu Haidong Reading (88) Comments (0) edit their collections quoted Category: J2EE learning 

    Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
    • Digg
    • del.icio.us
    • Facebook
    • DotNetKicks
    • DZone
    • Netvouz
    • Propeller

    Recommend Articles

    Comments

    Leave a Reply