java interface question crying

Asked 2 years ago, Updated 2 years ago, 25 views

This is what I thought while I was studying the java interface part.

Abstract and interface are abstract concepts for easy design

Among them, I learned that interface is a more abstract concept and is useful for team projects.

Then, is the interface used to code each other's parts on their computers?

And if each coding is over and combined, do we remove the interface class and add what each coding is?

And the interface doesn't allow you to use the normal method inside yourself, but only design itGo

I studied and I wonder why.

java

2022-09-22 18:34

1 Answers

The interface can be used for many purposes.

Let me give you an example. There are actions that the user can take in the application, and for each action, redo and undo must be supported through the menu. Suppose that each member of the team shared the work to implement the actions.

First of all, I designed the following interface and abstract class for the work. Each member must now use it to implement the details of their action.

interfaceAction {
    void run();
    void redo();
    void undo();
}
abstract classRecordableActions Action {
    static final int NO_ACTION = -1;
    private List<ActionRecord> actionList = new ArrayList<>();
    private int currentActionPosition = NO_ACTION;

    @Override
    public void run() {
        final ActionRecord record = performRun();
       //Saves a record of actions performed by the user in the list
      .....
    }

    @Override
    public void undo() {
        //Call performanceUndo() for the action to execute the undo action for the actual action
        performUndo(actionList.get(currentActionPosition));
    }

    @Override
    public void redo() {
        //Call performanceRedo() for the action to execute the redo action for the actual action
        performRunWithRecord(actionList.get(currentActionPosition));
    }

    abstract ActionRecord performRun();
    abstract void performRunWithRecord(ActionRecord record);
    abstract void performUndo(ActionRecord record);
}

Now let's extend RecordableAction to implement the actual action. Let's take an example of a create action for something.

classCreateAction extensionsRecordableAction {

    @Override
    ActionRecord performRun() {
        //do something create action
        final ActionRecord record = new ActionRecord();
        //Save a detailed record of the create action in the record
        return record;
    }

    @Override
    void performRunWithRecord(ActionRecord record) {
        //do something create action with record
    }

    @Override
    void performUndo(ActionRecord record) {
        //undo for create will normally be delete
        new DeleteAction().performRunWithRecord(record);
    }
}

Now, if you configure the example code to execute Create Action when the button is pressed,

Button.setOnClickListener(newView).OnClickListener(){
    @Override
    public void onClick(View v){
        final Action createAction = new CreateAction();
        createAction.run();
    }
});

What if in the example above, we didn't design an abstract class called Action and Recordable Action, but we shared the work? Each member will implement the action in their own way, and there will be a lot of difficulties in linking for redo undo. So using interface and abstract to work with a kind of design specification, it's easier to work with even if you need improvements later.

The above example was improvised, so please look outside the discussion to see if it works properly. Please note that it can also be used in this flow.


2022-09-22 18:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.