14. fukabori.fm で t_wada さんが言ってた
Design Patterns 15 Years Later: An Interview with Erich Gamma, Richard Helm, and Ralph Johnson
https://www.informit.com/articles/article.aspx?p=1404056
● 2009年のGoF(Gang of Four) へのイン
タビュー
○ Erich Gamma
○ Richard Helm
○ Ralph Johnson
○ John Vlissides (故人)
● リファクタリングによってデザインパ
ターンに近づけていく
● プログラミング言語自体にデザインパ
ターンが実装されている場合もあるな
ら不要だよね
22. 削除されたパターン
interface ProductPrice{
public int getPrice();
}
class Product{
private int cost;
public int getCost(){
return cost;
}
}
class ProductAdapter implements ProductPrice{
private Product product = new Product();
public int getPrice(){
return product.getCost();
}
}
Adapterパターン
Adapter パターン
https://ja.wikipedia.org/wiki/Adapter_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3
interface ProductPrice{
public int getPrice();
}
class Product{
private int cost;
public int getCost(){
return cost;
}
}
class ProductAdapter extends Product implements
ProductPrice{
public int getPrice(){
return this.getCost();
}
}
継承を使って実装 委譲を使って実装
27. 削除されたパターン
import java.util.*;
abstract class Logger {
public static final int ERROR = 3;
public static final int NOTICE = 5;
public static final int DEBUG = 7;
private final int mask;
protected Logger(int mask) { this.mask = mask; }
// The next element in the chain of responsibility
protected Logger next;
public Logger setNext(Logger l) {
next = l;
return this;
}
public void message(String msg, int priority) {
if (priority <= mask) {
writeMessage(msg);
if (next != null) {
next.message(msg, priority);
}
}
}
abstract protected void writeMessage(String msg);
}
Chain of responsibilityパターン
Chain of Responsibility パターン
https://ja.wikipedia.org/wiki/Chain_of_Responsibility_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3
28. class StdoutLogger extends Logger {
public StdoutLogger(int mask) { super(mask); }
@Override
protected void writeMessage(String msg) {
System.out.println("Writting to stdout: " + msg);
}
}
class EmailLogger extends Logger {
public EmailLogger(int mask) { super(mask); }
@Override
protected void writeMessage(String msg) {
System.out.println("Sending via email: " + msg);
}
}
class StderrLogger extends Logger {
public StderrLogger(int mask) { super(mask); }
@Override
protected void writeMessage(String msg) {
System.out.println("Sending to stderr: " + msg);
}
}
削除されたパターン
Chain of responsibilityパターン
29. public class ChainOfResponsibilityExample {
public static void main(String[] args) {
// Build the chain of responsibility
final Logger l =
new StdoutLogger(Logger.DEBUG).setNext(
new EmailLogger(Logger.NOTICE).setNext(
new StderrLogger(Logger.ERROR)));
// Handled by StdoutLogger
l.message("Entering function y.", Logger.DEBUG);
// Handled by StdoutLogger and EmailLogger
l.message("Step1 completed.", Logger.NOTICE);
// Handled by all three loggers
l.message("An error has occurred.", Logger.ERROR);
}
}
削除されたパターン
Chain of responsibilityパターン
31. 削除されたパターン
class Originator
class Memento
def initialize(state)
@state = state.dup
end
def state
@state.dup
end
end
attr_accessor :state
def save_to_memento
Memento.new(@state)
end
def restore_from_memento(m)
@state = m.state
end
end
describe Originator do
before(:all) do
@caretaker = Caretaker.new
@originator = Originator.new
@originator.state = "State1"
end
it "should have original state" do
@originator.state.should == 'State1'
end
it "should update state" do
@originator.state = "State2"
@originator.state.should == 'State2'
end
it "should save memento" do
@caretaker << @originator.save_to_memento
@caretaker.size.should == 1
end
以下略
Mementoパターン
Memento パターン
https://ja.wikipedia.org/wiki/Memento_%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3
36. class Client
{
private $service;
public function __construct(Service $service)
{
$this->service = $service;
}
public function doSomething()
{
$this->service->doSomething();
}
}
class Service
{
public function doSomething()
{
}
}
class Client
{
private $service;
public function __construct()
{
$this->service = new Service();
}
public function doSomething()
{
$this->service->doSomething();
}
}
class Service
{
public function doSomething()
{
}
}
追加されたパターン
Dependency Injectionパターン
やはりあなた方のDependency Injectionはまちがっている。
http://blog.a-way-out.net/blog/2015/08/31/your-dependency-injection-is-wrong-as-I-expected/
DIパターン
DIパターンでない
38. 追加されたパターン
Type Objectパターン
class TypeObject
def initialize(name, value)
@name = name
@value = value
end
def get(name)
return @value
end
end
def main(argv)
t1 = TypeObject.new("a", "AAAA")
t2 = TypeObject.new("b", "BBBB")
puts(t1.get("a"))
puts(t2.get("b"))
end
main(ARGV)