Skip to content

Instantly share code, notes, and snippets.

@Shamrock-Frost
Created May 5, 2021 05:41
Show Gist options
  • Save Shamrock-Frost/846ad483a46ba0d018c6b55f2ff94cb2 to your computer and use it in GitHub Desktop.
Save Shamrock-Frost/846ad483a46ba0d018c6b55f2ff94cb2 to your computer and use it in GitHub Desktop.
Representation Exposure
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("test.txt");
file.createNewFile();
A a = new A(file);
a.sayHello();
a.solveEquations(0,1,2);
System.out.close();
a.sayWorld();
}
}
class A {
private PrintStream internal;
// File objects are immutable, no worries there
public A(File file) throws FileNotFoundException {
internal = new PrintStream(file);
}
public void sayHello() {
internal.println("Hello");
}
public void sayWorld() {
internal.println("World");
}
// doesn't modify internal...
public int solveEquations(int x, int y, int z) {
System.setOut(internal);
return x + y + z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment