Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock Q2c #592

Open
GraceYongXM opened this issue Apr 26, 2022 · 6 comments
Open

Mock Q2c #592

GraceYongXM opened this issue Apr 26, 2022 · 6 comments

Comments

@GraceYongXM
Copy link

Source

mock paper q2c traffic light q!

Description

anyone thought of a way to prevent cyclic dependencies?

part a implementation Red and Green depends on each other

was thinking of creating a new class NextLight which will determine which colour of light to return but its still cyclic since green -> next -> green/ amber/ red

Screenshots (if any):

Insert Images here if necessary

@Swagston20
Copy link

Just create abstract class for each lights and then create anonymous class to toggle from one light to another

@ReganKvioke
Copy link

@Swagston20 Hey, do you mind sharing your code for this? I understand creating the abstract classes, but you lost me at the part to create an anonymous class to toggle. Like this toggle do I implement it in TrafficLight or in the various abstract classes? Thank you!

@Swagston20
Copy link

Redlight redlight = new Redlight() {
Redlight red = this;

@Override
toggle() {
    return new AmberLight() {
        @Override
        toggle() {
            return new GreenLight() {
                @Override
                toggle() {
                    return new AmberLight() {
                        @Override
                        toggle() {
                            return red;
                        }
                    }
                }
            }
        }
    }
}

}

@ReganKvioke
Copy link

@Swagston20 Thanks for the code, I saw it in another post that you sent, however where do you put this chunk of code into?

@seantanyurong
Copy link

If I'm not wrong, the generated redlight object should just be inserted as the first argument into the toggling method

@joelleng
Copy link

Hi, actually I am confused with this question and the concept of anonymous inner class. May I ask what is the purpose of defining a property i.e. in the new RedLight() {Redlight red = this; ...}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants