티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/94
class Person implements Serializable { // 직렬화를 위해 직렬화 의도를 Serializable을 구현함으로써 드러냄
String name;
String job;
// transient String job; // 직렬화 하지 않으려는 것에 transient
public Person() {}
public Person(String name, String job) {
this.name = name;
this.job = job;
}
public String toString() {
return name + "," + job;
}
}
public class SerializationTest {
public static void main(String[] args) {
Person personRico = new Person("리코", "아이돌");
Person personMari = new Person("마리", "이사장");
// "Serial.txt"에 작성
try(FileOutputStream fos = new FileOutputStream("Serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(personRico);
oos.writeObject(personMari);
} catch (IOException e) {
System.out.println(e);
}
// "Serial.txt" 를 복원
try(FileInputStream fis = new FileInputStream("Serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis)) {
Person pRico = (Person)ois.readObject();
Person pMari = (Person)ois.readObject();
System.out.println(pRico);
System.out.println(pMari);
} catch (IOException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
}
}
class Person implements Externalizable{
String name;
String job;
public Person() {}
public Person(String name, String job) {
this.name = name;
this.job = job;
}
public String toString()
{
return name + "," + job;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF(name);
//out.writeUTF(job);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = in.readUTF();
//job = in.readUTF();
}
}
public class RandomAccessFileTest {
public static void main(String[] args) throws IOException {
RandomAccessFile rf = new RandomAccessFile("random.txt", "rw"); // write, read가 동시에 가능
rf.writeInt(100);
System.out.println("pos:" + rf.getFilePointer());
rf.writeDouble(3.14);
System.out.println("pos:" + rf.getFilePointer());
rf.writeUTF("헬로");
System.out.println("pos:" + rf.getFilePointer());
rf.seek(0); // rf를 맨 처음으로 이동시킴
int i = rf.readInt();
double d = rf.readDouble();
String str = rf.readUTF();
System.out.println(i);
System.out.println(d);
System.out.println(str);
}
}
public abstract class Coffee { // 상속을 위한 추상화 클래스
// component
public abstract void brewing();
}
public class EtiopiaAmericano extends Coffee {
// ConcereteComponent
@Override
public void brewing() {
System.out.print("Etiopia Americano ");
}
}
public abstract class Decorator extends Coffee { // 다른 클래스 상속을 위해 추상화
Coffee coffee; // component를 포함해야 함
public Decorator(Coffee coffee) {
this.coffee = coffee;
}
@Override
public void brewing() {
coffee.brewing(); // component가 실행할 부분
}
}
public class Latte extends Decorator {
public Latte(Coffee coffee) {
super(coffee); // 상위 클래스에 기본 생성자가 없어 super 필요
}
@Override
public void brewing() {
super.brewing();
System.out.print("우유를 탄다 ");
}
}
public class Mocha extends Decorator {
public Mocha(Coffee coffee) {
super(coffee);
}
@Override
public void brewing() {
super.brewing();
System.out.print("시럽을 탄다 ");
}
}
public class WhippingCream extends Decorator {
public WhippingCream(Coffee coffee) {
super(coffee);
}
@Override
public void brewing() {
super.brewing();
System.out.print("휘핑 크림을 얹는다 ");
}
}
public class CoffeeTest {
public static void main(String[] args) {
Coffee etiopiaCoffee = new EtiopiaAmericano();
etiopiaCoffee.brewing();
System.out.println();
Coffee etiopiaLatte = new Latte(etiopiaCoffee);
etiopiaLatte.brewing();
System.out.println();
Coffee mochaEtiopia = new Mocha(new Latte(etiopiaCoffee));
mochaEtiopia.brewing();
System.out.println();
Coffee creamMochaEtiopia = new WhippingCream(new Mocha(new Latte(etiopiaCoffee)));
creamMochaEtiopia.brewing();
}
}