How to avoid multiple if else using design patterns.

Hi,
Which design patterns is used to avoid lot of if else in our code . Say for example if the user press 1 the out put should be one, if he press 2 it should be two like this upto 10. So we need to include lot of if-else part. How to avoid the multiple if else using design patter for this scenario. I can't find the solution for this problem can you guys help me?.

I'm too lazy to read a whole text book, but the first google link provides a code sample which made me laugh.
http://www.refactoring.com/catalog/replaceConditionalWithPolymorphism.html
  double getSpeed() {
    switch (_type) {
      case EUROPEAN:
        return getBaseSpeed();
      case AFRICAN:
        return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
      case NORWEGIAN_BLUE:
        return (_isNailed) ? 0 : getBaseSpeed(_voltage);
    throw new RuntimeException ("Should be unreachable");
  }My implementation of The Bridge Over The Parrot Shop
package forums;
import java.util.Random;
abstract class Bird {
  private static final Random random = new Random();
  abstract double getAirSpeed();
  public double getBaseSpeed() {
    return randomDouble(18.96);
  protected static double randomDouble(double max) {
    return random.nextDouble() * max;
  protected static int randomInt(int max) {
    return random.nextInt(max);
class EuropeanSwallow extends Bird {
  double getAirSpeed() {
    return getBaseSpeed();
class AfricanSwallow extends Bird {
  private int _numberOfCoconuts;
  public AfricanSwallow() {
    this(randomInt(4));
  public AfricanSwallow(int numberOfCoconuts) {
    _numberOfCoconuts = numberOfCoconuts;
  double getAirSpeed() {
    return getBaseSpeed() - getLoadFactor() * _numberOfCoconuts;
  public double getLoadFactor() {
    return randomDouble(3.12);
class NorwegianBlue extends Bird {
  private boolean _isNailed;
  private double _voltage;
  public NorwegianBlue() {
    this(randomInt(10)<2, randomDouble(12.0));
  public NorwegianBlue(boolean isNailed, double voltage) {
    _isNailed = isNailed;
    _voltage = voltage;
  double getAirSpeed() {
    return _isNailed ? 0 : getBaseSpeed(_voltage);
  double getBaseSpeed(double voltage) {
    return getBaseSpeed() * (1+(voltage/Math.PI));
public class AirSpeedTest
  public static void main(String[] args)
    System.out.println("EUROPEAN: "+new EuropeanSwallow().getAirSpeed());
    System.out.println("AFRICAN: "+new AfricanSwallow().getAirSpeed());
    System.out.println("NORWEGIAN_BLUE: "+new NorwegianBlue().getAirSpeed());
}... and the requisite background for the uninitiated [NORWEGIAN_BLUE|http://www.youtube.com/watch?v=4vuW6tQ0218] and [EUROPEAN v AFRICAN|http://www.youtube.com/watch?v=4b4bGAoVR7g].
You have to know these things when you're a King you know.
Cheers. Keith.

Similar Messages

  • How to avoide multiple duplicate entries in adress book?

    How to avoide multiple duplicate entries in adress book? I can add the same contact name and number more than twice and the phone isn't warning me at all!!! I's quite a heck for me.

    not possible from inside AB AFAIK. but you can do the following. in finder open the folder /users/username/library/application support/address book/metdata. switch to the list mode and sort by date modified. quicklook the vcards at the top to see which ones they are.

  • How to avoid multiple copies of data in page refresh

    How to avoid multiple copies of data in page refresh

    If you are talking about a page with an insert query, ignore the fact that it's a page refresh.  It's just another way that someone can insert duplicate records.  I like to handle this in my insert query.
    insert into thetable
    (f1, f2, etc)
    select distinct
    value1, value2, etc
    from some_small_table
    where you don't already have that record

  • How to avoid multiple listing for same artist?

    How to avoid multiple listing for same artist?

    Sounds like there might be blanks in the artist name.
    For instance "Lou Reed" and "Lou Reed_" will not match.
    (Pretend the underscore _ is a blank).
    Select all the Lou Reed songs, Get Info, and type "Lou Reed" in the artist field. That should fix it.

  • How to avoid data repetation when using select statements with innerjoin

    how to avoid data repetation when using select statements with innerjoin.
    thanks in advance,
    satheesh

    you can use a query like this...
      SELECT DISTINCT
             frg~prc_group1                  "Product Group 1
             frg~prc_group2                  "Product Group 2
             frg~prc_group3                  "Product Group 3
             frg~prc_group4                  "Product Group 4
             frg~prc_group5                  "Product Group 5
             prc~product_id                  "Product ID
             txt~short_text                  "Product Description
    UP TO 10 ROWS
    INTO TABLE l_i_data
    FROM
    Joining CRMM_PR_SALESG and
    COMM_PR_FRG_ROD
    crmm_pr_salesg AS frg
    INNER JOIN comm_pr_frg_rod AS prd
    ON frgfrg_guid = prdfragment_guid
    Joining COMM_PRODUCT and
    COMM_PR_FRG_ROD
    INNER JOIN comm_product AS prc
    ON prdproduct_guid = prcproduct_guid
    Joining COMM_PRSHTEXT and
    COMM_PR_FRG_ROD
    INNER JOIN comm_prshtext AS txt
    ON prdproduct_guid = txtproduct_guid
    WHERE frg~prc_group1 IN r_zprc_group1
       AND frg~prc_group2 IN r_zprc_group2
       AND frg~prc_group3 IN r_zprc_group3
       AND frg~prc_group4 IN r_zprc_group4
       AND frg~prc_group5 IN r_zprc_group5.
    reward it it helps
    Edited by: Apan Kumar Motilal on Jun 24, 2008 1:57 PM

  • How to avoid multiple DataConnections with LCD ES2

    Hi, we are just starting using the data connections to connect via a database with LiveCycle Designer. It seems we are missing something important regarding to manipulation of data from the PDF.
    At first we have tried to apply a INSERT command to our first table and be able to browse through the items in the database. We are able to browse through the database only before we have inserted something.
    If we insert an item and then try to browse through the database with (Next, Previous, Last or First) it will crash and have an error of the following :
    (Next, Previous, Last or First) failed. Multiple-step operation generated errors. check each status value [ID:@11]
    So then we have decided to create a second data connection which would have each of the column in the database except for the ID which appears to make everything crash.
    E.G.:  Table_1
    DataConnection1 --> ID, Field1, Field2, Field3, Field4          SELECT Command connection
    DataConnection2 --> Field1, Field2, Field3, Field4               INSERT Command connection
    Seems like we can't have a SELECT and INSERT in the same DataConnection because with those 2 different connections it works fine....
    Then we are trying to show multiple data's in a table which is linked with the ID of the Selected ID in DataConnection1.. To show the data is all fine using a 3rd connection for that Table_2, then we are making sure a blank row is always there at the end of the table to be able to add this new entry to the database with a Add button to INSERT into the Table_2... Unfortunately we do not have a 2nd connection to that table because we cannot link those fields with the database for the purpose of a multiple entry view.
    We have tried to make a 2nd connection for the purpose of the INSERT but it doesnt work at all.
    We are basing ourselves on the sample provided by Stefan Cameron in his blog http://forms.stefcameron.com/2006/12/18/databases-inserting-updating-and-deleting-records/
    I am wondering if we are using the right functionalities and if it is the simplest way to work with databases...
    If anyone can help, it would be greatly appreciated!!
    Thanks in advance!
    Mag

    Don't forget to activate the RESOURCE_LIMIT parameter, which default is FALSE :
    alter system set RESOURCE_LIMIT = true;
    Laurent, I had a similar problem some time ago : I didn't want to avoid multiple access, but only control who was doing what. That's because moving from Client/Server to Web the TERMINAL column in V$SESSION becomes useless.
    I tried your solution, but I had to give up with it, because in my Forms9i application some forms call Reports, which generate a new session.
    I decided to use DBMS_APPLICATION_INFO, and this is satisfactory for my requirements, but I'm interested to discover other solutions.
    P.S. with my solution I'm able to limit accesses, because in the CLIENT_INFO string I put, among other things, the
    application user, so I can control if an user is already connected. The problem is that existing applications have to be modified .....:-(

  • How to avoid multiple LOGIN with same user in database.

    Hi All,
    Using database triggers how to control multiple logins of a pertiuclar user.
    thanks in advance

    Don't forget to activate the RESOURCE_LIMIT parameter, which default is FALSE :
    alter system set RESOURCE_LIMIT = true;
    Laurent, I had a similar problem some time ago : I didn't want to avoid multiple access, but only control who was doing what. That's because moving from Client/Server to Web the TERMINAL column in V$SESSION becomes useless.
    I tried your solution, but I had to give up with it, because in my Forms9i application some forms call Reports, which generate a new session.
    I decided to use DBMS_APPLICATION_INFO, and this is satisfactory for my requirements, but I'm interested to discover other solutions.
    P.S. with my solution I'm able to limit accesses, because in the CLIENT_INFO string I put, among other things, the
    application user, so I can control if an user is already connected. The problem is that existing applications have to be modified .....:-(

  • How to avoid multiple users to access same tcode

    Hi all,
    Am creating one z tcode to display the material details for the particular reservation number. How can  I avoid multiple users to access the same tcode simultaneously. If anybody works over that tcode then no other user should be able to access the same tcode until or unless the first user works over it.
    Regards,
    Ramya

    HI,
    you can even use import and export logic and check if user has entered the transaction ..
    Regards,
    Santosh
    Message was edited by:
            Santosh Kumar Patha

  • How to avoid multiple password prompt for intarnet portal

    we have developed a intranet portal in Share Point 2013 with multiple site collection. when accessing through IE, Chrome, we can do settings to avoid multiple password prompt by adding the domain name of site in Trusted sites. How we can achieve the same in Safari while accessing through MAC or IPAD

    first time when you call the BSP application from the browser (or by double clicking the BSP_APPLICATION link from SAP GUI) you need to pass user credentials. For further windows this will not be required as browser will remember your info.
    if you need to <b>somehow</b> avoid this then the only option is to hard code the user id and password in the service in transaction SICF or by passing the same via URL.
    Another option is that if you port this application in a SAP portal environment, SSO can be used .
    Hope this helps.
    Regards
    Raja

  • Query: how to avoid multiple POs  from being combined into  delivery

    Hi all,
    We are working on an enhancement.
    The requirement is:
    We need to avoid multiple POs from being combined into one delivery.
    Any pointers in this regard would be highly appreciated.
    regards
    Gurpreet

    Hi Murali,
    I think your suggestion should solve Gurpreets problem.
    I have a query here.
    In the copy controls VLTA, defines between sales doc to dlivery doc.
    Here we want to maintain copy control between purchasing doc to delivery doc.
    How do we achive this?
    Also in my view we should write a new combination requirement.
    Please check if combination requirement 51 can be used?
    Regards
    Abhishek

  • How to avoid multiple call to function:

    In our datawarehouse we have a huge receipt row table where all metrics ar stored in the local currency. On top over that we have views which calculate metrics to the desired currency.
    So basically all views looks like this
    select geo_region,
    product_group,
    customer_group,
    metric1 * (select get_exchange_rate(currency_id) from dual) metric1,
    metric1 * (select get_exchange_rate(currency_id) from dual) metric2,
    metric1 * (select get_exchange_rate(currency_id) from dual) metricx,
    group by..
    As we have about 20 metrics we notices that the function is called 20 times per row.
    Is there really anyway to avoid that? Shouldn't it be it's just the exact same call with the same in-parameters over and over again.
    We've tried with local sys_context and the performance is better but the call to the context is still performed 20 times. Any Ideas?

    Can you avoid multiple function calls? Maybe, if as in your example all the function calls values are computing the same result. If they operate on different columns then you'll have to perform the function call anyway.
    Either way you should be able to eliminate the (near as I can tell) pointless subquery from dual
    You might be able to avoid the repeated function calls if the values are always the same. If every computation you could save the function call (and subquery!) by doing it once and then using assignments after the initial query using variables after the initial query, perhaps using NULL in the query as placeholders to select into a record - something like
    select inital_region,
             product_group,
             customer_group,
             metric1 * exchange_rate(currency_id) metric1,
             null metric2,
    v_metric2 := metric1;
    ...Message was edited by (fixed typo):
    riedelme

  • How to avoid multiple selection in ALV tree control?

    Hi,
    Experts,
    I want to avoid multiple selections on Alv tree control after pressing control keyboard button(Ctrl). Even by pressing Ctrl keyword button i want to select only one row of a Alv tree control.i have used cl_salv_tree class for it is there any method to achieve this please pass some code/idea on it.
    Thanks in advance,
    Shabeer ahmed.

    I haven't tried it before but it should help.
    [https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/cda3992d-0e01-0010-90b2-c4e1f899ac01|https://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/cda3992d-0e01-0010-90b2-c4e1f899ac01]

  • How to avoid multiple votings?

    Hello All. How can I avoid, that there are multiple votings from one person?

    I think there is no option to avoid multiple votings. Adobe has not implemented this funktion.
    Formstack is as alternative to Formcentral. The Fieldoption there is called - Unique.
    Unique allows you to restrict submissions of the same value in this field. For example, you can allow only one submission for a particular email address or physical address.
    That is the option i was looking too.
    Formcentral is useless for me in this way.

  • How to achieve multiple hirearcy structure using RBS feature in Project server 2013

    HI,
    How can we achieve multiple hirearcy structure using RBS feature in Project server 2013?
    The below is the screen shot of hirearcy.
    GM
    DGM
    AGM
    CM
    Officers
    There are different departments and one AM may handle 4 or more department. How to achieve this permission levels so that GM can see all departments officers created project, same with other levels
    Thanks,
    Sunitha

    Hi Sunitha,
    You could populate the RBS lookup table with 2 levels (department and sub department). Then configuring correctly your
    security categories based on the RBS, the resources in the 1st level (departments) will see projects from all theirs sub departments.
    Some more references:
    http://aboutmsproject.com/project-server-securitypart-1/
    http://ntrajkovski.com/2012/06/04/the-rbs-lookup-table-in-ms-project-server-2010/
    Hope this helps,
    Guillaume Rouyre, MBA, MVP, P-Seller |

  • How to create multiple ifs users using XML

    I used the following xml file to create a ifs user. But I don't know how to create multiple users using XML. Thanks for your help.
    <SimpleUser>
    <UserName>bill</UserName>
    <Password>bill</Password>
    <DirectoryUserDescription>Bill</DirectoryUserDescription>
    </SimpleUser>

    There are many good resources for Powershell tutorials. The MS help page for each cmdlet is usually a good place to start.
    The suggested answer is very good and easy to modify to your needs. The linked article provides good detail on creating your csv file. If you look at their sample csv, you will see the column for LicenseAgreement and UsageLocation. You can change the script
    to use the values from the csv instead of the same value for every user like so:
    -LicenseAssignment $_.LicenseAgreement -UsageLocation $_UsageLocation
    Basically, $_.<valuename> is the filled in by the value under the heading <valuename> in your csv file for the current user. 
    As for what msdivision, replace this with the correct domain name value for your organization. You can get the values for license SKUs from existing users by using the following:
    Connect-MsolService
    Get-MsolUser -UserPrincipalName <userprincipalname> |select Licenses

Maybe you are looking for