R
R
rvller2011-03-22 00:40:03
Java
rvller, 2011-03-22 00:40:03

Delegation in interface (Java)?

At the university, one of the tasks in the lab:
“Inherit the IPrinterDelegate interface from the IPrinter interface. Extend the IPrinterDelegate interface with a method that allows you to classify the relationship between IPrinterDelegate objects and IPrintable objects as a delegation relationship (IPrinterDelegate objects are delegates, IPrintable objects are delegates). Create an example of a concrete class of the IPrinterDelegate family and provide an example of client code."
IPrinter implementation:

public interface IPrinter {
  abstract void PrintString(String string);
  abstract void PrintChar(char character); 
}

IPrintable implementation:
public interface IPrintable {
  abstract void Print(IPrinter printer);
}

As I understand it, you need to make sure that there is a new method in the IPrinterDelegate interface that uses the IPrinter methods? How can something like this be implemented?
I write in Java.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2011-03-22
@rvller

Реализация IPrintable:
public interface IPrintable {
  abstract void Print(IPrinter printer);
}

Isn't it a hint? Naturally, if you cannot write code in interfaces, then you need to specify an implicit relationship. At least that's how I understood the assignment.

J
javenue, 2011-03-22
@javenue

1. It's better to call print rather than printAll. The printAll method implies an IPrintable collection.
2. The names of methods according to the code convention are written with a small letter.
To complete the picture, it is better to add another implementation of IPrintable, for example:
public class SingleRowDocument implements IPrintable {
private String row;
public SingleRowDocument(String row) { this.row = row; }
public String getRow() { return row; }
public void print(IPrinter printer) {
printer.printString(row);
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question