読者です 読者をやめる 読者になる 読者になる

Adapterパターン

Java

継承と委譲とどちらが使いやすいかですね。どちらも覚えとけと。
Javaになってからプリントデバッグする機会へったよなー

% find . -name "*.java" -print0 |xargs -0 cat                                []
package extend;

public class Banner {
	private String string;

	public Banner(String string) {
		this.string = string;
	}

	public void showWithParen() {
		System.out.println("(" + string + ")");
	}

	public void showWithAster() {
		System.out.println("*" + string + "*");
	}
}
package extend;

public class Main {

	public static void main(String[] args) {
		Print p = new PrintBanner("Hello");
		p.printWeak();
		p.printStrong();
	}

}
package extend;

public interface Print {
	public abstract void printWeak();

	public abstract void printStrong();
}
package extend;

public class PrintBanner extends Banner implements Print {

	public PrintBanner(String string) {
		super(string);
	}

	@Override
	public void printWeak() {
		showWithParen();
	}

	@Override
	public void printStrong() {
		showWithAster();
	}

}
package transfer;

import extend.Print;

public class Main {

	public static void main(String[] args) {
		Print p = new PrintBanner("Fuga");
		p.printWeak();
		p.printStrong();
	}

}
package transfer;

import extend.Banner;
import extend.Print;

public class PrintBanner implements Print {
	private Banner banner;

	public PrintBanner(String string) {
		this.banner = new Banner(string);
	}

	@Override
	public void printWeak() {
		banner.showWithParen();
	}

	@Override
	public void printStrong() {
		banner.showWithAster();
	}

}