如何使这个OOP [英] How to make this to OOP

查看:167
本文介绍了如何使这个OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Override
public void onMotionSensingEvent( MotionSensingEvent arg0) {

    double gradient = 1.38;
    double speed;

    float testpitch = 0;
    testpitch = arg0.getOrientation().getPitch();

    float testroll = 0;
    testroll = arg0.getOrientation().getRoll();


    if (testroll > 16 && flyingControl)
    {
        speed = gradient*testroll-22.1;
        System.out.println("kanan = " + speed);
        ardrone.goRight(speed);
    }
    else if (testroll < (-24) && flyingControl)
    {
        speed = gradient*testroll*-1 - 22.1;
        System.out.println("kiri = " + speed);
        ardrone.goLeft(speed);
    }
    else if (testpitch < (-20) && flyingControl)
    {
        System.out.println("go up");
        ardrone.up();
    }
    else if (testpitch > 20 && flyingControl)
    {
        System.out.println("go down");
        ardrone.down();
    }       
}

@Override
public void onExpansionEvent(ExpansionEvent e)
{
    JoystickEvent joy = getJoystickEvent(e);

    float gradient =64;
    float speed;

    float angle = 0;
    angle = joy.getAngle();
    float magnitude = joy.getMagnitude();


    if ((angle > 340 || angle < 20) && magnitude > 0.49 && flyingControl)
    {
        speed = gradient*magnitude-19;

        ardrone.forward(speed);
    }
    else if ((angle > 160 && angle < 200) && magnitude > 0.49 && flyingControl)
    {
        speed = gradient*magnitude-19;

        ardrone.backward(speed);
    }
    else if ((angle > 70 && angle < 110) && magnitude > 0.7 && flyingControl)
    {
        System.out.println("turn right ");
        ardrone.spinRight();
    }
    else if ((angle > 250 && angle < 290) && magnitude > 0.7 && flyingControl)
    {
        System.out.println("turnleft ");
        ardrone.spinLeft();
    }
}

我有这个代码,所有这些IF-ELSE的东西出了主类,使它成为OOP,但我不知道一个好的方法开始。我的主管说了一些关于听者,但我不明白。 fyi,我有那些听众,但我不包括他们在这个问题。

I have this code, and I need help to remove all these IF-ELSE things out of the main class, make it into OOP but I don't know a good way to start with. My supervisor said something about listener but I didn't understand. fyi, I have those listener but I don't include them in this question.

那么,任何人都可以告诉我的步骤,删除所有这些IF ELSE主类?
非常感谢!

so then, can anybody tell me the step to remove all these IF ELSE out of the main class? thanks a lot!!

推荐答案

使用责任链?不同的行为然后可以封装在自己的类中。

What about using a chain of responsibility? Behaviors that vary here could then be encapsulate in their own class.

在这种情况下,这些专用类将作为监听器,正如您的老师所想。

In this case, these dedicated classes would act as listeners, as your teacher thought.

您的if / else块中的语句将被封装在一个特定对象中。

Each sequence of statements in your if/else blocks, would then be encapsulated in one specific object.

这将大大有助于保持开启/关闭原则


当多个对象可能处理一个请求并且事先不知道实际的处理程序
时,您可以使用责任链。

When more than one object may handle a request and the actual handler is not known in advance, you may use a Chain of Responsibility.

一些类似于yours(来自GUI的事件)的例子在这里: http://www.dcs.bbk.ac.uk/~oded/OODP13/Sessions/Session6/Chain.pdf

Some examples similar to yours (events from GUI) here:http://www.dcs.bbk.ac.uk/~oded/OODP13/Sessions/Session6/Chain.pdf

这篇关于如何使这个OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆