Created
February 28, 2017 17:19
-
-
Save wreulicke/c3e08ab03d40ac94c189e976edf51d24 to your computer and use it in GitHub Desktop.
委譲メモ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SomeService{ | |
public void fire(String parameter){} | |
} | |
// pattern 1 複数のクラスに委譲 | |
class DelegatingSomeService implements SomeService{ | |
private List<SomeService> services; | |
public DelegatingSomeService(List<SomeService> service){ | |
this.services=service; | |
} | |
@Override | |
public void fire(String parameter){ | |
this.services.stream().forEach((s)->s.fire(parameter)); | |
} | |
} | |
// ThreadLocalでThreadに応じたオブジェクトに委譲 | |
class ContextProxyService{ | |
private ThreadLocal<SomeService> local=new ThreadLocal<>(); | |
@Override | |
public void fire(Stirng parameter){ | |
SomeService service=local.get(); | |
Objects.requireNonNull(service, "Illegal context status").fire(parameter); | |
} | |
public void setContext(SomeService service){ | |
local.set(); | |
} | |
public void removeContext(){ | |
local.remove(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment