Repository navigation
Expand file tree
/
Copy pathMultiFunctionDevice.java
More file actions
69 lines (60 loc) · 1.73 KB
/
Copy pathMultiFunctionDevice.java
File metadata and controls
69 lines (60 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.uk.lld.solid;
public interface MultiFunctionDevice {
void print(String document);
void scan(String document);
void fax(String document);
}
/*
* Interface Segregation Principle (ISP) states that clients should not be forced to depend upon interfaces that they do not use.
* Interfaces should belong to the clients that invoke them, not to the implementations that execute them.
*
* public class BasicPrinter implements MultiFunctionDevice {
@Override
public void print(String document) {
System.out.println("Printing: " + document);
}
@Override
public void scan(String document) {
// Violates ISP (forced to implement) AND LSP (throws unexpected exception)
throw new UnsupportedOperationException("Scanner not supported");
}
@Override
public void fax(String document) {
throw new UnsupportedOperationException("Fax not supported");
}
}
*
* SOLUTION
*
* Segregate based on the client
* Printer only exhibits print(String document) functionality
* Xerox exhibits both print(String document) and scan(String document) functionality.
*
* public interface Printer {
void print(String document);
}
public interface Scanner {
void scan(String document);
}
public interface Fax {
void fax(String document);
}
*
* public class BasicPrinter implements Printer {
@Override
public void print(String document) {
System.out.println("Printing: " + document);
}
}
*
* public class Photocopier implements Printer, Scanner {
@Override
public void print(String document) {
System.out.println("Printing: " + document);
}
@Override
public void scan(String document) {
System.out.println("Scanning: " + document);
}
}
* */