Jump to content
razvandragos29

Delegation in java

Recommended Posts

Posted

Salut invat in acest moment dupa Thinking in Java editia a 4-a si am intampinat o problema nu inteleg la ce se refera autorul prin "delegation" acesta este exemplu

public class SpaceShipControls {

void up(int velocity) {}

void down(int velocity) {}

void left(int velocity) {}

void right(int velocity) {}

void forward(int velocity) {}

void back(int velocity) {}

void turboBoost() {}

public class SpaceShipDelegation {

private String name;

private SpaceShipControls controls =

new SpaceShipControls();

public SpaceShipDelegation(String name) {

this.name = name;

}

// Delegated methods:

public void back(int velocity) {

controls.back(velocity);

}

public void down(int velocity) {

controls.down(velocity);

}

public void forward(int velocity) {

controls.forward(velocity);

}

public void left(int velocity) {

controls.left(velocity);

}

public void right(int velocity) {

controls.right(velocity);

}

public void turboBoost() {

controls.turboBoost();

}

public void up(int velocity) {

controls.up(velocity);

}

public static void main(String[] args) {

SpaceShipDelegation protector =

new SpaceShipDelegation("NSEA Protector");

protector.forward(100);

}

Posted

Chiar si in programare, multe cuvinte sunt imprumutate din alte domenii pentru a fi mai usor de abstractizat.

Delegation - Wikipedia, the free encyclopedia

Clasa SpaceShipControls are definite anumite metode.

SpaceShipDelegation implementeaza aceleasi metode (au acelasi nume) iar implementarea lor este sa apeleze metodele din SpaceShipControls.

Deci daca ai un SpaceShipDelegation deleg = new SpaceShipDelegation("adsa"); si vrei sa apelezi metoda back.

Se va apela defapt back din SpaceShipDelegation si pe urma back() din SpaceShipControls.

Adica clasa SpaceShipDelegation delega apelul la SpaceShipControls.

Cred ca urmeaza sa inveti despre interfete, clase abstracte si polimorfism si asta a fost introducerea.

  • Downvote 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...