SlideShare a Scribd company logo
1 of 464
Download to read offline
prepare for…..
Proxy Deep Dive
JavaOne 2015 20151027
1
@SvenRuppert
@SvenRuppert
has been coding java since 1996
Fellow / Senior Manager
reply Group
Germany - Munich
2
@SvenRuppert
has been coding java since 1996
3
@SvenRuppert
has been coding java since 1996
Projects in the field of:
•Automobile-industry
•Energy
•Finance / Leasing
•Space- Satellit-
•Government / UN / World-bank
Where?
•Europe
•Asia - from India up to Malaysia
3
prepare for…..
Proxy Deep Dive - the first steps
some words about Adapter, Delegator
and the first Proxy
4
@SvenRuppert
5
@SvenRuppert
5
@SvenRuppert
Why a Proxy ?
6
@SvenRuppert
Proxies you can use them for :
generic parts inside your application
hiding technical stuff
decouple parts of your applications
Why a Proxy ?
7
@SvenRuppert
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
.addSecurityRule(() -> true)
but it must be easy to use like…
Why a Proxy ?
7
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
.addSecurityRule(() -> true)
.build();
but it must be easy to use like…
Why a Proxy ?
8
@SvenRuppert
but it must be easy to use like…
Why a Proxy ?
8
@SvenRuppert
@Inject
@Proxy(virtual = true, metrics = true)
Service service;
but it must be easy to use like…
Adapter v Proxy - Pattern Hello World
9
@SvenRuppert
Difference between Proxy and Adapter
Adapter v Proxy - Pattern Hello World
Adapter - or called Wrapper
maybe : Use an Adapter if you have to use an incompatible
interface between two systems.
In detail I am using/talking about an Adapter with Delegator.
10
@SvenRuppert
Adapter v Proxy - Pattern Hello World
11
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
public interface WorkerAdapter {

public void workOnSomething(List<String> stringListe);

}
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
public interface WorkerAdapter {

public void workOnSomething(List<String> stringListe);

}
public class Adapter implements WorkerAdapter {

private LegacyWorker worker = new LegacyWorker();

@Override

public void workOnSomething(List<String> stringListe) {

final String[] strings = stringListe.toArray(new String[0]);

worker.writeTheStrings(strings);

}

}
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
public interface WorkerAdapter {

public void workOnSomething(List<String> stringListe);

}
public class Adapter implements WorkerAdapter {

private LegacyWorker worker = new LegacyWorker();

@Override

public void workOnSomething(List<String> stringListe) {

final String[] strings = stringListe.toArray(new String[0]);

worker.writeTheStrings(strings);

}

}
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
public interface WorkerAdapter {

public void workOnSomething(List<String> stringListe);

}
public class Adapter implements WorkerAdapter {

private LegacyWorker worker = new LegacyWorker();

@Override

public void workOnSomething(List<String> stringListe) {

final String[] strings = stringListe.toArray(new String[0]);

worker.writeTheStrings(strings);

}

}
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
public interface WorkerAdapter {

public void workOnSomething(List<String> stringListe);

}
public class Adapter implements WorkerAdapter {

private LegacyWorker worker = new LegacyWorker();

@Override

public void workOnSomething(List<String> stringListe) {

final String[] strings = stringListe.toArray(new String[0]);

worker.writeTheStrings(strings);

}

}
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class LegacyWorker {

public void writeTheStrings(String[] strArray){

Arrays.stream(strArray).forEach(System.out::println);

}

}
11
public interface WorkerAdapter {

public void workOnSomething(List<String> stringListe);

}
public class Adapter implements WorkerAdapter {

private LegacyWorker worker = new LegacyWorker();

@Override

public void workOnSomething(List<String> stringListe) {

final String[] strings = stringListe.toArray(new String[0]);

worker.writeTheStrings(strings);

}

}
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
• No need to know the LegacyWorker
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
• No need to know the LegacyWorker
• No inheritance between LegacyWorker and Adapter
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
• No need to know the LegacyWorker
• No inheritance between LegacyWorker and Adapter
• only a transformation of an interface
@SvenRuppert
Adapter v Proxy - Pattern Hello World
public class Main {

public static void main(String[] args) {

final ArrayList<String> strings = new ArrayList<>();

Collections.addAll(strings, „A","B","C","D");


new Adapter().workOnSomething(strings);

}

}
12
• No need to know the LegacyWorker
• No inheritance between LegacyWorker and Adapter
• only a transformation of an interface
• same creation time for Delegator and Adapter
@SvenRuppert
Proxy - the easiest version
13
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
Proxy - the easiest version
13
• We need to know the LegacyWorker
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
Proxy - the easiest version
13
• We need to know the LegacyWorker
• Inheritance between LegacyWorker and Adapter
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
Proxy - the easiest version
13
• We need to know the LegacyWorker
• Inheritance between LegacyWorker and Adapter
• do not use it like an Adapter !
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
Proxy - the easiest version
13
• We need to know the LegacyWorker
• Inheritance between LegacyWorker and Adapter
• do not use it like an Adapter !
@SvenRuppert
We need a few steps to come from an Adapter with
Delegator to a Proxy
please show the code
Proxy - the easiest version - (Delegator)
14
@SvenRuppert
Proxy - the easiest version - (Delegator)
public interface Service {

String work(String txt);

}
14
@SvenRuppert
Proxy - the easiest version - (Delegator)
public interface Service {

String work(String txt);

}
14
@SvenRuppert
public class ServiceImpl implements Service {

public String work(String txt) {

return "ServiceImpl - " + txt;

}

}
Proxy - the easiest version - (Delegator)
public interface Service {

String work(String txt);

}
14
@SvenRuppert
public class ServiceImpl implements Service {

public String work(String txt) {

return "ServiceImpl - " + txt;

}

}
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
Security Proxy
15
@SvenRuppert
Security Proxy
15
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
Security Proxy
15
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Security Proxy
15
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
SecurityProxy:
execute only if it is allowed
Security Proxy
15
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
SecurityProxy:
execute only if it is allowed
public class ServiceSecurityProxy implements Service {

private Service service = new ServiceImpl();

private String code; //who will set this ?

public String work(String txt) {

if(code.equals(„hoppel")) { return service.work(txt); }

else { return "nooooop"; }

}

}
Security Proxy
15
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
SecurityProxy:
execute only if it is allowed
public class ServiceSecurityProxy implements Service {

private Service service = new ServiceImpl();

private String code; //who will set this ?

public String work(String txt) {

if(code.equals(„hoppel")) { return service.work(txt); }

else { return "nooooop"; }

}

}
Remote Proxy - JDK only
16
@SvenRuppert
Remote Proxy - JDK only
16
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
Remote Proxy - JDK only
16
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Remote Proxy - JDK only
16
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
RemoteProxy:
execute outside of my process
Remote Proxy - JDK only
16
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
RemoteProxy:
execute outside of my process
Service proxy = new ServiceRemoteProxy();

String hello = proxy.work(„Hello");
Remote Proxy - JDK only
16
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
RemoteProxy:
execute outside of my process
Service proxy = new ServiceRemoteProxy();

String hello = proxy.work(„Hello");
Remote Proxy - JDK only
16
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
RemoteProxy:
execute outside of my process
Service proxy = new ServiceRemoteProxy();

String hello = proxy.work(„Hello");
some work needed
Remote Proxy - JDK only
17
@SvenRuppert
Remote Proxy - JDK only
17
@SvenRuppert
Remote Proxy - JDK only
17
@SvenRuppert
Interface
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Interface
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
17
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
18
@SvenRuppert
@WebService

@SOAPBinding(style = SOAPBinding.Style.RPC)

public interface Service {

@WebMethod

public String work(String txt);

}
Remote Proxy - JDK only
19
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
19
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
20
@SvenRuppert
@SOAPBinding(style = SOAPBinding.Style.RPC)

@WebService(endpointInterface = "org.rapidpm.Service")

public class ServiceImpl implements Service {

@Override

public String work(String txt) {

return "ServiceImpl - " + txt;

}

}
Remote Proxy - JDK only
21
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
21
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
22
@SvenRuppert
final String address = "http://localhost:9999/ws/service";

final ExecutorService threadPool = Executors.newFixedThreadPool(10);

final Endpoint endpoint = Endpoint.create(new ServiceImpl());

endpoint.setExecutor(threadPool);

endpoint.publish(address);

System.out.println("endpoint = " + endpoint.isPublished());
Remote Proxy - JDK only
22
@SvenRuppert
final String address = "http://localhost:9999/ws/service";

final ExecutorService threadPool = Executors.newFixedThreadPool(10);

final Endpoint endpoint = Endpoint.create(new ServiceImpl());

endpoint.setExecutor(threadPool);

endpoint.publish(address);

System.out.println("endpoint = " + endpoint.isPublished());
Remote Proxy - JDK only
23
@SvenRuppert
Proxy Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
23
@SvenRuppert
Proxy Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
24
@SvenRuppert
public class ServiceRemoteProxy implements Service {



private URL url;

private Service realSubject;

final String namespaceURI = "http://rapidpm.org/";

final String localPart = "ServiceImplService";



public ServiceRemoteProxy() {

try {

url = new URL("http://localhost:9999/ws/service?wsdl");

QName qname = new QName(namespaceURI, localPart);

javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);

realSubject = service.getPort(Service.class);

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

public String work(String txt){

return realSubject.work(txt);

}

}
Remote Proxy - JDK only
24
@SvenRuppert
public class ServiceRemoteProxy implements Service {



private URL url;

private Service realSubject;

final String namespaceURI = "http://rapidpm.org/";

final String localPart = "ServiceImplService";



public ServiceRemoteProxy() {

try {

url = new URL("http://localhost:9999/ws/service?wsdl");

QName qname = new QName(namespaceURI, localPart);

javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);

realSubject = service.getPort(Service.class);

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

public String work(String txt){

return realSubject.work(txt);

}

}
Remote Proxy - JDK only
24
@SvenRuppert
public class ServiceRemoteProxy implements Service {



private URL url;

private Service realSubject;

final String namespaceURI = "http://rapidpm.org/";

final String localPart = "ServiceImplService";



public ServiceRemoteProxy() {

try {

url = new URL("http://localhost:9999/ws/service?wsdl");

QName qname = new QName(namespaceURI, localPart);

javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);

realSubject = service.getPort(Service.class);

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

public String work(String txt){

return realSubject.work(txt);

}

}
Remote Proxy - JDK only
25
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
Remote Proxy - JDK only
25
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
• remote communication
Remote Proxy - JDK only
25
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
• remote communication
• most of this is technology based work
Remote Proxy - JDK only
25
@SvenRuppert
Proxy
Remote
Service
Interface
Interface
remote communication
client server
• remote communication
• most of this is technology based work
• goal: developer will see no remote
communication
Virtual Proxy
26
@SvenRuppert
Virtual Proxy
26
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
Virtual Proxy
26
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Virtual Proxy
26
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Virtual Proxy:
create the Delegator later
Virtual Proxy
26
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Virtual Proxy:
create the Delegator later
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
Virtual Proxy
26
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Virtual Proxy:
create the Delegator later
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
Virtual Proxy
26
@SvenRuppert
public class ServiceProxy implements Service {

private Service service = new ServiceImpl();

public String work(String txt) { return service.work(txt); }

}
What could we
change now ?
Virtual Proxy:
create the Delegator later
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
This is NOT ThreadSafe
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
This is NOT ThreadSafe
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
This is NOT ThreadSafe
fixed decision for
an implementation
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
This is NOT ThreadSafe
fixed decision for
an implementation
Virtual Proxy
27
@SvenRuppert
public class VirtualService implements Service {

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }


return service.work(txt);

}

}
This is NOT ThreadSafe
fixed decision for
an implementation
how to combine it with
a FactoryPattern ?
Virtual Proxy
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public interface ServiceFactory {

Service createInstance();

}
Virtual Proxy
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public interface ServiceFactory {

Service createInstance();

}
Virtual Proxy
28
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public interface ServiceFactory {

Service createInstance();

}
public interface ServiceStrategyFactory {

Service realSubject(ServiceFactory factory);

}
Virtual Proxy - Not - ThreadSafe
29
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Not - ThreadSafe
29
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Not - ThreadSafe
29
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Not - ThreadSafe
29
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Not - ThreadSafe
29
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceStrategyFactoryImpl implements ServiceStrategyFactory {

Service realSubject;

public Service realSubject(final ServiceFactory factory) {

if (realSubject == null) {

realSubject = factory.createInstance();

}

return realSubject;

}

}
private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
}

}
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
return
}

}
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
return strategyFactory
}

}
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
return strategyFactory .realSubject(
}

}
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
return strategyFactory .realSubject(serviceFactory
}

}
Virtual Proxy - Not - ThreadSafe
30
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceProxy implements Service {
private ServiceFactory serviceFactory = ServiceImpl::new;
private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
public String work(String txt) {
return strategyFactory .realSubject(serviceFactory).work(txt);
}

}
Virtual Proxy - Synchronized
31
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Synchronized
31
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Synchronized
31
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Synchronized
31
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Synchronized
31
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceStrategyFactorySynchronized implements ServiceStrategyFactory {

Service realSubject;

public synchronized Service realSubject(final ServiceFactory factory) {

if (realSubject == null) {

realSubject = factory.createInstance();

}

return realSubject;

}

}
private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Synchronized
31
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceStrategyFactorySynchronized implements ServiceStrategyFactory {

Service realSubject;

public synchronized Service realSubject(final ServiceFactory factory) {

if (realSubject == null) {

realSubject = factory.createInstance();

}

return realSubject;

}

}
private ServiceFactory serviceFactory = ServiceImpl::new;
This is ThreadSafe
Virtual Proxy - Method Scoped
32
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Method Scoped
32
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

Virtual Proxy - Method Scoped
32
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Method Scoped
32
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Method Scoped
32
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceStrategyFactoryMethodScoped implements
ServiceStrategyFactory {

public Service realSubject(final ServiceFactory factory) { 

return factory.createInstance();

}

}
private ServiceFactory serviceFactory = ServiceImpl::new;
Virtual Proxy - Method Scoped
32
@SvenRuppert
if(service == null) { service = new ServiceImpl(); }

public class ServiceStrategyFactoryMethodScoped implements
ServiceStrategyFactory {

public Service realSubject(final ServiceFactory factory) { 

return factory.createInstance();

}

}
private ServiceFactory serviceFactory = ServiceImpl::new;
Combining Proxies
33
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
33
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
33
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
33
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
33
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
hard wired proxies
Combining Proxies - SecureVirtualProxy
34
@SvenRuppert
Security Proxy Virtual Proxy
public class VirtualProxy implements Service {

public VirtualProxy() { out.println("VirtualProxy " + now()); }

private Service service = null;

public String work(String txt) {

if(service == null) { service = new ServiceImpl(); }

return service.work(txt);

}

}
public class SecureVirtualProxy implements Service {

public SecureProxy() { out.println("SecureProxy " + now()); }

private Service service = new VirtualProxy();

//SNIPP….

public String work(String txt) {

if(code.equals("hoppel")) { return service.work(txt); }

return { return "nooooop"; }

}

}
hard wired proxies
What could we
change now ?
Combining Proxies - SecureVirtualProxy
35
@SvenRuppert
Security Proxy Virtual Proxy
hard wired proxies
What could we
change now ?
we need a Generic Proxy
Combining Proxies - SecureVirtualProxy
35
@SvenRuppert
Security Proxy Virtual Proxy
hard wired proxies
What could we
change now ?
we need a Generic Proxy
since JDK1.3 we have it
Combining Proxies - SecureVirtualProxy
35
@SvenRuppert
Security Proxy Virtual Proxy
hard wired proxies
What could we
change now ?
we need a Generic Proxy
since JDK1.3 we have it
the DynamicProxy
prepare for…..
Proxy Deep Dive - DynamicProxies
some words about :
how to use and how to extend….but.. also what is not nice
36
@SvenRuppert
37
@SvenRuppert
37
@SvenRuppert
Dynamic Proxy - Hello World
38
@SvenRuppert
How to use it ?
Dynamic Proxy - Hello World
38
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
Dynamic Proxy - Hello World
38
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
Dynamic Proxy - Hello World
38
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
we need an implementation : here ServiceImpl
Dynamic Proxy - Hello World
38
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
we need an implementation : here ServiceImpl
Service proxyInstance = Service.class.cast( Proxy.newProxyInstance(

Service.class.getClassLoader(),

new Class<?>[]{Service.class},

new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

return method.invoke(service, args);

}

}

)

);
Dynamic Proxy - Hello World
38
@SvenRuppert
How to use it ?
java.lang.reflect.Proxy
we need an interface : here Service
we need an implementation : here ServiceImpl
Service proxyInstance = Service.class.cast( Proxy.newProxyInstance(

Service.class.getClassLoader(),

new Class<?>[]{Service.class},

new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

return method.invoke(service, args);

}

}

)

);
OK, step by step , please
Dynamic Proxy - Hello World
39
@SvenRuppert
Dynamic Proxy - Hello World
39
@SvenRuppert
Service proxyInstance = Service.class.cast (
Dynamic Proxy - Hello World
39
@SvenRuppert
Proxy.newProxyInstance (
Service proxyInstance = Service.class.cast (
Dynamic Proxy - Hello World
39
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
Service proxyInstance = Service.class.cast (
Dynamic Proxy - Hello World
39
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
Dynamic Proxy - Hello World
39
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}
Dynamic Proxy - Hello World
39
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}
)

);
Dynamic Proxy - Hello World
39
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}
)

);
how to make a VirtualProxy ?
Dynamic Virtual Proxy
40
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
)

);
Dynamic Virtual Proxy
40
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service;

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

if(service == null) { service = new ServiceImpl(); }
return m.invoke(service, args);

}

}
)

);
Dynamic Virtual Proxy
40
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service;

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

if(service == null) { service = new ServiceImpl(); }
return m.invoke(service, args);

}

}
)

);
Dynamic Virtual Proxy
40
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service;

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

if(service == null) { service = new ServiceImpl(); }
return m.invoke(service, args);

}

}
)

);
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
get it from outside
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
get it from outside
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
get it from outside
we will remove it
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
get it from outside
we will remove it
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
get it from outside
we will remove it
we will get it from outside
Dynamic Proxy - make it generic
41
@SvenRuppert
Proxy.newProxyInstance (
Service.class.getClassLoader(),
new Class<?>[]{Service.class},
Service proxyInstance = Service.class.cast (
new InvocationHandler() {

private final ServiceImpl service = new ServiceImpl();

@Override

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {

return m.invoke(service, args);

}

}

)

);
get it from outside
we will remove it
we will get it from outside
and finally we will call it ProxyGenerator
Dynamic Proxy - make it generic
42
@SvenRuppert
public class ProxyGenerator {

public static <P> P makeProxy(Class<P> subject, P realSubject) {

Object proxyInstance = Proxy.newProxyInstance(

subject.getClassLoader(),

new Class<?>[]{subject},

new InvocationHandler() {

@Override

public Object invoke(final Object proxy, final Method m, final Object[] args) throws Throwable {

return m.invoke(realSubject, args);

}

}

);

return subject.cast(proxyInstance);

}

}
Dynamic Proxy - make it generic
43
@SvenRuppert
public class ProxyGenerator {

public static <P> P makeProxy(Class<P> subject, P realSubject) {

Object proxyInstance = Proxy.newProxyInstance(

subject.getClassLoader(),

new Class<?>[]{subject},

(proxy, m, args) -> m.invoke(realSubject, args)

);

return subject.cast(proxyInstance);

}

}
Dynamic Proxy - make it generic
44
@SvenRuppert
with DynamicProxies
- we are writing less (more generic) code
- we can create Virtual-/Remote-/Security Proxies
Dynamic Proxy - make it generic
44
@SvenRuppert
with DynamicProxies
- we are writing less (more generic) code
- we can create Virtual-/Remote-/Security Proxies
but how to combine Proxies more generic ?
Dynamic Proxy - make it generic
45
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Dynamic Proxy - make it generic
45
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Factory-Method-Pattern
Dynamic Proxy - make it generic
45
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Factory-Method-Pattern
Dynamic Proxy - make it generic
45
@SvenRuppert
but how to combine Proxies more generic ?
for this we have to switch from
Factory-Method-Pattern
Builder-Pattern
DynamicProxyBuilder
46
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
DynamicProxyBuilder
46
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
1 Security Proxy with 2 Rules and 1 VirtualProxy
DynamicProxyBuilder
46
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
1 Security Proxy with 2 Rules and 1 VirtualProxy
2 Security Proxies with 1 Rule and 1 VirtualProxy
DynamicProxyBuilder
46
@SvenRuppert
but how to combine Proxies more generic ?
Security Proxy Virtual ProxySecurity Proxy
let´s think about Two possible ways:
1 Security Proxy with 2 Rules and 1 VirtualProxy
2 Security Proxies with 1 Rule and 1 VirtualProxy
OK, we need a generic CascadedProxy
DynamicProxyBuilder
47
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
DynamicProxyBuilder
47
@SvenRuppert
OK, we need a generic CascadedProxy
Proxy n
Proxy n-1
Proxy n-2
Proxy n-3
a child must know his parent
first we only add different DynamicProxies
always the same target interface
DynamicProxyBuilder
48
@SvenRuppert
for example: add n SecurityRules
DynamicProxyBuilder
48
@SvenRuppert
for example: add n SecurityRules
public interface SecurityRule {

boolean checkRule();

}
DynamicProxyBuilder
48
@SvenRuppert
for example: add n SecurityRules
public interface SecurityRule {

boolean checkRule();

}
DynamicProxyBuilder
48
@SvenRuppert
for example: add n SecurityRules
public interface SecurityRule {

boolean checkRule();

}
functional interface
DynamicProxyBuilder
48
@SvenRuppert
private List<SecurityRule> securityRules = new ArrayList<>();
for example: add n SecurityRules
public interface SecurityRule {

boolean checkRule();

}
functional interface
DynamicProxyBuilder
48
@SvenRuppert
private List<SecurityRule> securityRules = new ArrayList<>();
for example: add n SecurityRules
public interface SecurityRule {

boolean checkRule();

}
functional interface
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
last child
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
last child
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
last child
work on child
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
last child
work on child
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
last child
work on child
set child for next level
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
49
@SvenRuppert
private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {

final ClassLoader classLoader = original.getClass().getClassLoader();

final Class<?>[] interfaces = {clazz};

final Object nextProxy = Proxy.newProxyInstance(

classLoader, interfaces,

new InvocationHandler() {

private T original = ProxyBuilder.this.original;

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

final boolean checkRule = rule.checkRule();

if (checkRule) {

return method.invoke(original, args);

} else {

return null;

}

}

});

original = (T) clazz.cast(nextProxy);

return this;

}
how this will be used ?
last child
work on child
set child for next level
Collections.reverse(securityRules);

securityRules.forEach(this::build_addSecurityRule);
DynamicProxyBuilder
50
@SvenRuppert
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
.addSecurityRule(() -> true)
DynamicProxyBuilder
50
@SvenRuppert
final InnerDemoClass original = new InnerDemoClass();
final InnerDemoInterface demoLogic =
ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
.addLogging()
.addMetrics()
.addSecurityRule(() -> true)
.addSecurityRule(() -> true)
.build();
DynamicProxyBuilder
51
@SvenRuppert
DynamicProxyBuilder
51
@SvenRuppert
Until now, we had only a cascade of one interface
DynamicProxyBuilder
51
@SvenRuppert
Until now, we had only a cascade of one interface
how to deal with different interfaces ?
DynamicProxyBuilder
51
@SvenRuppert
Until now, we had only a cascade of one interface
how to deal with different interfaces ?
DynamicProxyBuilder
51
@SvenRuppert
Until now, we had only a cascade of one interface
how to deal with different interfaces ?
we need a generic NestedBuilder
NestedBuilder - The Beginning
52
@SvenRuppert
NestedBuilder - The Beginning
53
@SvenRuppert
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = new ArrayList<>();

wheels.add(wheel1);

wheels.add(wheel2);

wheels.add(wheel3);
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = new ArrayList<>();

wheels.add(wheel1);

wheels.add(wheel2);

wheels.add(wheel3);
}
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = new ArrayList<>();

wheels.add(wheel1);

wheels.add(wheel2);

wheels.add(wheel3);
}
NestedBuilder - The Beginning
53
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
List<Wheel> wheels = new ArrayList<>();

wheels.add(wheel1);

wheels.add(wheel2);

wheels.add(wheel3);
}
NestedBuilder - The Beginning
54
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
List<Wheel> wheels = new ArrayList<>();

wheels.add(wheel1);

wheels.add(wheel2);

wheels.add(wheel3);
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
List<Wheel> wheels = new ArrayList<>();

wheels.add(wheel1);

wheels.add(wheel2);

wheels.add(wheel3);
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
//List<Wheel> wheels = new ArrayList<>();

// wheels.add(wheel1);

// wheels.add(wheel2);

// wheels.add(wheel3);
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
//List<Wheel> wheels = new ArrayList<>();

// wheels.add(wheel1);

// wheels.add(wheel2);

// wheels.add(wheel3);
List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build();
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
//List<Wheel> wheels = new ArrayList<>();

// wheels.add(wheel1);

// wheels.add(wheel2);

// wheels.add(wheel3);
List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build(); //more robust if you add tests at build()
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
//List<Wheel> wheels = new ArrayList<>();

// wheels.add(wheel1);

// wheels.add(wheel2);

// wheels.add(wheel3);
List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build(); //more robust if you add tests at build()
}
}
NestedBuilder - The Beginning
55
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
//List<Wheel> wheels = new ArrayList<>();

// wheels.add(wheel1);

// wheels.add(wheel2);

// wheels.add(wheel3);
List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build(); //more robust if you add tests at build()
}
}
how to combine ?
NestedBuilder - The Beginning
56
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build();
}
}
how to combine ?
NestedBuilder - The Beginning
57
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
// Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 

// Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();

// Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
// List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build();
NestedBuilder - The Beginning
57
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
// List<Wheel> wheelList = WheelListBuilder.newBuilder()

.withNewList()

.addWheel(wheel1)

.addWheel(wheel2)

.addWheel(wheel3)

.build();
NestedBuilder - The Beginning
57
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
NestedBuilder - The Beginning
57
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
NestedBuilder - The Beginning
58
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
NestedBuilder - The Beginning
58
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
NestedBuilder - The Beginning
58
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
WheelBuilder
NestedBuilder - The Beginning
58
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
WheelBuilder
NestedBuilder - The Beginning
58
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
WheelBuilder
WheelListBuilder
NestedBuilder - The Beginning
59
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
NestedBuilder - The Beginning
59
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
now we have to combine all
NestedBuilder - The Beginning
59
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
List<Wheel> wheels = wheelListBuilder

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.build();
now we have to combine all
NestedBuilder - The Beginning
59
@SvenRuppert
Car car = Car.newBuilder()

.withEngine(engine)

.withWheelList(wheels)

.build();
now we have to combine all
NestedBuilder - The Beginning
59
@SvenRuppert
now we have to combine all
NestedBuilder - The Beginning
59
@SvenRuppert
now we have to combine all
NestedBuilder - The Beginning
59
@SvenRuppert
now we have to combine all
Car car = Car.newBuilder()

.addEngine().withPower(100).withType(5).done()

.addWheels()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.done()

.build();
NestedBuilder - The Beginning
59
@SvenRuppert
now we have to combine all
Car car = Car.newBuilder()

.addEngine().withPower(100).withType(5).done()

.addWheels()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.addWheel().withType(1).withSize(2).withColour(2).addWheelToList()

.done()

.build();
NestedBuilder - The Pattern
60
@SvenRuppert
public abstract class NestedBuilder<T, V> {
protected T parent;
public abstract V build()
public <P extends NestedBuilder<T, V>> P 

withParentBuilder(T parent) {

this.parent = parent;

return (P) this;

}
NestedBuilder - The Pattern
60
@SvenRuppert
public abstract class NestedBuilder<T, V> {
protected T parent;
public abstract V build()
public <P extends NestedBuilder<T, V>> P 

withParentBuilder(T parent) {

this.parent = parent;

return (P) this;

}
NestedBuilder - The Pattern
60
@SvenRuppert
public abstract class NestedBuilder<T, V> {
protected T parent;
public abstract V build()
public <P extends NestedBuilder<T, V>> P 

withParentBuilder(T parent) {

this.parent = parent;

return (P) this;

}
parent will connect itself…
NestedBuilder - The Pattern
61
@SvenRuppert
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
public T done() {
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
public T done() {
Class<?> parentClass = parent.getClass();
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
public T done() {
Class<?> parentClass = parent.getClass();
try {

V build = this.build();

String methodname = "with" + build.getClass().getSimpleName();

Method method = parentClass.getDeclaredMethod(

methodname, build.getClass());

method.invoke(parent, build);

} catch (NoSuchMethodException 

| IllegalAccessException | InvocationTargetException e) {

e.printStackTrace();

}
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
public T done() {
Class<?> parentClass = parent.getClass();
try {

V build = this.build();

String methodname = "with" + build.getClass().getSimpleName();

Method method = parentClass.getDeclaredMethod(

methodname, build.getClass());

method.invoke(parent, build);

} catch (NoSuchMethodException 

| IllegalAccessException | InvocationTargetException e) {

e.printStackTrace();

}
}
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
public T done() {
Class<?> parentClass = parent.getClass();
try {

V build = this.build();

String methodname = "with" + build.getClass().getSimpleName();

Method method = parentClass.getDeclaredMethod(

methodname, build.getClass());

method.invoke(parent, build);

} catch (NoSuchMethodException 

| IllegalAccessException | InvocationTargetException e) {

e.printStackTrace();

}
}
NestedBuilder - The Pattern
61
@SvenRuppert
public abstract class NestedBuilder<T, V> {
public T done() {
Class<?> parentClass = parent.getClass();
try {

V build = this.build();

String methodname = "with" + build.getClass().getSimpleName();

Method method = parentClass.getDeclaredMethod(

methodname, build.getClass());

method.invoke(parent, build);

} catch (NoSuchMethodException 

| IllegalAccessException | InvocationTargetException e) {

e.printStackTrace();

}
}
connect itself with parent
NestedBuilder - The Pattern
62
@SvenRuppert
NestedBuilder - The Pattern
62
@SvenRuppert
the basic steps in short words
NestedBuilder - The Pattern
62
@SvenRuppert
the basic steps in short words
the Parent-Builder will hold the Child-Builder
NestedBuilder - The Pattern
62
@SvenRuppert
the basic steps in short words
the Parent-Builder will hold the Child-Builder
the Parent-Builder will have a addChild - Method
NestedBuilder - The Pattern
62
@SvenRuppert
the basic steps in short words
the Parent-Builder will hold the Child-Builder
the Parent-Builder will have a addChild - Method
the Child-Builder will extend the NestedBuilder
NestedBuilder - The Pattern
62
@SvenRuppert
the basic steps in short words
the Parent-Builder will hold the Child-Builder
the Parent-Builder will have a addChild - Method
the Child-Builder will extend the NestedBuilder
the rest could be generated with a 

default Builder-Generator
NestedBuilder - The Pattern
63
@SvenRuppert
public class Parent {

private KidA kidA;

private KidB kidB;

//snipp.....

public static final class Builder {

private KidA kidA;

private KidB kidB;

// to add manually

private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);

private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);



public KidA.Builder addKidA() {

return this.builderKidA;

}

public KidB.Builder addKidB() {

return this.builderKidB;

}

//---------
NestedBuilder - The Pattern
63
@SvenRuppert
public class Parent {

private KidA kidA;

private KidB kidB;

//snipp.....

public static final class Builder {

private KidA kidA;

private KidB kidB;

// to add manually

private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);

private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);



public KidA.Builder addKidA() {

return this.builderKidA;

}

public KidB.Builder addKidB() {

return this.builderKidB;

}

//---------
NestedBuilder - The Pattern
63
@SvenRuppert
public class Parent {

private KidA kidA;

private KidB kidB;

//snipp.....

public static final class Builder {

private KidA kidA;

private KidB kidB;

// to add manually

private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);

private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);



public KidA.Builder addKidA() {

return this.builderKidA;

}

public KidB.Builder addKidB() {

return this.builderKidB;

}

//---------
connect itself with child
NestedBuilder - The Pattern
63
@SvenRuppert
public class Parent {

private KidA kidA;

private KidB kidB;

//snipp.....

public static final class Builder {

private KidA kidA;

private KidB kidB;

// to add manually

private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);

private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);



public KidA.Builder addKidA() {

return this.builderKidA;

}

public KidB.Builder addKidB() {

return this.builderKidB;

}

//---------
connect itself with child
NestedBuilder - The Pattern
63
@SvenRuppert
public class Parent {

private KidA kidA;

private KidB kidB;

//snipp.....

public static final class Builder {

private KidA kidA;

private KidB kidB;

// to add manually

private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);

private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);



public KidA.Builder addKidA() {

return this.builderKidA;

}

public KidB.Builder addKidB() {

return this.builderKidB;

}

//---------
connect itself with child
switch to Child-Builder
NestedBuilder - The Pattern
64
@SvenRuppert
public static final class Builder 

extends NestedBuilder<Parent.Builder, KidA> {
NestedBuilder - The Pattern
64
@SvenRuppert
public static final class Builder 

extends NestedBuilder<Parent.Builder, KidA> {
NestedBuilder - The Pattern
64
@SvenRuppert
only extends on Child-Builder
public static final class Builder 

extends NestedBuilder<Parent.Builder, KidA> {
NestedBuilder - The Pattern
65
@SvenRuppert
NestedBuilder - The Pattern
65
@SvenRuppert
Parent build = Parent.newBuilder()



.addKidA().withNote("A").done()

.addKidB().withNote("B").done()



.build();

System.out.println("build = " + build);
NestedBuilder - The Pattern
65
@SvenRuppert
Parent build = Parent.newBuilder()



.addKidA().withNote("A").done()

.addKidB().withNote("B").done()



.build();

System.out.println("build = " + build);
and a child could be a parent in the same time
NestedBuilder - The Pattern
65
@SvenRuppert
Parent build = Parent.newBuilder()



.addKidA().withNote("A").done()

.addKidB().withNote("B").done()



.build();

System.out.println("build = " + build);
and a child could be a parent in the same time
Parent build = Parent.newBuilder()

.addKidA().withNote("A")
.addKidB().withNote("B").done()
.done()

.build();
System.out.println("build = " + build);

NestedProxyBuilder - The Goal
66
@SvenRuppert
handwritten , params for diff Proxies
Combining Proxies
67
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
67
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
67
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
67
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
Combining Proxies
67
@SvenRuppert
Security Proxy Virtual ProxyRemote Proxy
Security ProxyVirtual ProxyRemote Proxy
Security ProxyVirtual Proxy Remote Proxy
VirtualProxies are mostly the last one
combining Proxies is easy
SecurityProxies are mostly the first one ?
DynamicProxyBuilder
68
@SvenRuppert
One functionality leads to one Proxy Instance
runtime overhead for every Proxy - but
make the first design simple.. optimize later
DynamicObjectAdapter - orig Version
69
@SvenRuppert
the original Version of the DynamicObjectAdapter
was written by Dr. Heinz Kabutz
DynamicObjectAdapter - orig Version
70
@SvenRuppert
DynamicObjectAdapter - orig Version
70
@SvenRuppert
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
orig /
adapter
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
orig /
adapter
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
orig /
adapter
invoke orig
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
orig /
adapter
invoke orig
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
orig /
adapter
invoke orig
invoke adapter
DynamicObjectAdapter - orig Version
70
@SvenRuppert
Proxy
orig /
adapter
invoke orig
invoke adapter
Goal: do not need to write an
Adapter with Delegator
DynamicObjectAdapter - orig Version
71
@SvenRuppert
core concept:
switching between method implementations
DynamicObjectAdapter - orig Version
71
@SvenRuppert
core concept:
switching between method implementations
how to identify a method?
DynamicObjectAdapter - orig. Version
72
@SvenRuppert
public interface Service {

String doWork_A();

}



public static class ServiceImpl implements Service {

public String doWork_A() {

return ServiceImpl.class.getSimpleName();

}

}
DynamicObjectAdapter - orig Version
73
@SvenRuppert
how to identify a method?
public class MethodIdentifier {

private final String name;

private final Class[] parameters;



public MethodIdentifier(Method m) {

name = m.getName();

parameters = m.getParameterTypes();

}

// we can save time by assuming that we only compare against

// other MethodIdentifier objects

public boolean equals(Object o) {

MethodIdentifier mid = (MethodIdentifier) o;

return name.equals(mid.name) &&

Arrays.equals(parameters, mid.parameters);

}

public int hashCode() { return name.hashCode(); }

}
DynamicObjectAdapter - orig Version
73
@SvenRuppert
how to identify a method?
public class MethodIdentifier {

private final String name;

private final Class[] parameters;



public MethodIdentifier(Method m) {

name = m.getName();

parameters = m.getParameterTypes();

}

// we can save time by assuming that we only compare against

// other MethodIdentifier objects

public boolean equals(Object o) {

MethodIdentifier mid = (MethodIdentifier) o;

return name.equals(mid.name) &&

Arrays.equals(parameters, mid.parameters);

}

public int hashCode() { return name.hashCode(); }

}
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001
Proxy deep-dive java-one_20151027_001

More Related Content

What's hot

What's hot (20)

Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharp
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
 
Java SE 8 for Java EE developers
Java SE 8 for Java EE developersJava SE 8 for Java EE developers
Java SE 8 for Java EE developers
 
Completable future
Completable futureCompletable future
Completable future
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
Java 8: the good parts!
Java 8: the good parts!Java 8: the good parts!
Java 8: the good parts!
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)Refactoring to Java 8 (Devoxx BE)
Refactoring to Java 8 (Devoxx BE)
 
Java 7 New Features
Java 7 New FeaturesJava 7 New Features
Java 7 New Features
 
Building Maintainable Applications in Apex
Building Maintainable Applications in ApexBuilding Maintainable Applications in Apex
Building Maintainable Applications in Apex
 
What's new in Scala 2.13?
What's new in Scala 2.13?What's new in Scala 2.13?
What's new in Scala 2.13?
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
Porting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQLPorting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQL
 
Java(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the UglyJava(8) The Good, The Bad and the Ugly
Java(8) The Good, The Bad and the Ugly
 
Introduction to nsubstitute
Introduction to nsubstituteIntroduction to nsubstitute
Introduction to nsubstitute
 
PHP 8: What's New and Changed
PHP 8: What's New and ChangedPHP 8: What's New and Changed
PHP 8: What's New and Changed
 
Refactoring for Software Design Smells - Tech Talk
Refactoring for Software Design Smells - Tech TalkRefactoring for Software Design Smells - Tech Talk
Refactoring for Software Design Smells - Tech Talk
 

Viewers also liked (9)

Proxy & CGLIB
Proxy & CGLIBProxy & CGLIB
Proxy & CGLIB
 
Object relationship mapping and hibernate
Object relationship mapping and hibernateObject relationship mapping and hibernate
Object relationship mapping and hibernate
 
Dynamic Proxy by Java
Dynamic Proxy by JavaDynamic Proxy by Java
Dynamic Proxy by Java
 
Classloading and Type Visibility in OSGi
Classloading and Type Visibility in OSGiClassloading and Type Visibility in OSGi
Classloading and Type Visibility in OSGi
 
hibernate with JPA
hibernate with JPAhibernate with JPA
hibernate with JPA
 
Understanding Java Dynamic Proxies
Understanding Java Dynamic ProxiesUnderstanding Java Dynamic Proxies
Understanding Java Dynamic Proxies
 
AWS CloudFormation en 5 Minutos
AWS CloudFormation en 5 MinutosAWS CloudFormation en 5 Minutos
AWS CloudFormation en 5 Minutos
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 

Similar to Proxy deep-dive java-one_20151027_001

Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
JAXLondon2014
 

Similar to Proxy deep-dive java-one_20151027_001 (20)

Proxy Deep Dive Voxxed Belgrad 2015
Proxy Deep Dive Voxxed Belgrad 2015Proxy Deep Dive Voxxed Belgrad 2015
Proxy Deep Dive Voxxed Belgrad 2015
 
Proxy Deep Dive JUG Saxony Day 2015-10-02
Proxy Deep Dive JUG Saxony Day 2015-10-02Proxy Deep Dive JUG Saxony Day 2015-10-02
Proxy Deep Dive JUG Saxony Day 2015-10-02
 
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
Kicking off with Zend Expressive and Doctrine ORM (ConFoo YVR 2017)
 
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
ClojureScript - Making Front-End development Fun again - John Stevenson - Cod...
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
 
Cloud Native Serverless Java — Orkhan Gasimov
Cloud Native Serverless Java — Orkhan GasimovCloud Native Serverless Java — Orkhan Gasimov
Cloud Native Serverless Java — Orkhan Gasimov
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Whoops! Where did my architecture go?
Whoops! Where did my architecture go?Whoops! Where did my architecture go?
Whoops! Where did my architecture go?
 
Java APIs - the missing manual
Java APIs - the missing manualJava APIs - the missing manual
Java APIs - the missing manual
 
Arquitecturas de microservicios - Medianet Software
Arquitecturas de microservicios   -  Medianet SoftwareArquitecturas de microservicios   -  Medianet Software
Arquitecturas de microservicios - Medianet Software
 
Spring Cloud Function — Going Serverless
Spring Cloud Function — Going ServerlessSpring Cloud Function — Going Serverless
Spring Cloud Function — Going Serverless
 
Functional Programming With Lambdas and Streams in JDK8
 Functional Programming With Lambdas and Streams in JDK8 Functional Programming With Lambdas and Streams in JDK8
Functional Programming With Lambdas and Streams in JDK8
 
Open Source, infrastructure as Code, Cloud Native Apps 2015
Open Source, infrastructure as Code, Cloud Native Apps 2015Open Source, infrastructure as Code, Cloud Native Apps 2015
Open Source, infrastructure as Code, Cloud Native Apps 2015
 
OpenDaylight and YANG
OpenDaylight and YANGOpenDaylight and YANG
OpenDaylight and YANG
 
Understanding and Developing Web Services: For DBAs and Database Developers
Understanding and Developing Web Services: For DBAs and Database DevelopersUnderstanding and Developing Web Services: For DBAs and Database Developers
Understanding and Developing Web Services: For DBAs and Database Developers
 
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon RitterLambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
Lambdas and Streams in Java SE 8: Making Bulk Operations simple - Simon Ritter
 
Lambdas And Streams in JDK8
Lambdas And Streams in JDK8Lambdas And Streams in JDK8
Lambdas And Streams in JDK8
 
Devfest 2023 - Service Weaver Introduction - Taipei.pdf
Devfest 2023 - Service Weaver Introduction - Taipei.pdfDevfest 2023 - Service Weaver Introduction - Taipei.pdf
Devfest 2023 - Service Weaver Introduction - Taipei.pdf
 

More from Sven Ruppert

More from Sven Ruppert (13)

JUnit5 Custom TestEngines intro - version 2020-06
JUnit5 Custom TestEngines intro - version 2020-06JUnit5 Custom TestEngines intro - version 2020-06
JUnit5 Custom TestEngines intro - version 2020-06
 
Hidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-PersistenceHidden pearls for High-Performance-Persistence
Hidden pearls for High-Performance-Persistence
 
Vaadin Flow - How to start - a short intro for Java Devs
Vaadin Flow - How to start - a short intro for Java DevsVaadin Flow - How to start - a short intro for Java Devs
Vaadin Flow - How to start - a short intro for Java Devs
 
Functional Reactive With Core Java - Voxxed Melbourne
Functional Reactive With Core Java - Voxxed MelbourneFunctional Reactive With Core Java - Voxxed Melbourne
Functional Reactive With Core Java - Voxxed Melbourne
 
Functional Reactive with Core Java - Workshop - Slides
Functional Reactive with Core Java - Workshop - SlidesFunctional Reactive with Core Java - Workshop - Slides
Functional Reactive with Core Java - Workshop - Slides
 
Functional reactive-talk 20170301-001
Functional reactive-talk 20170301-001Functional reactive-talk 20170301-001
Functional reactive-talk 20170301-001
 
From Mess To Masterpiece - JFokus 2017
From Mess To Masterpiece - JFokus 2017From Mess To Masterpiece - JFokus 2017
From Mess To Masterpiece - JFokus 2017
 
From Jurassic Park to Microservices
From Jurassic Park to MicroservicesFrom Jurassic Park to Microservices
From Jurassic Park to Microservices
 
From jUnit to Mutationtesting
From jUnit to MutationtestingFrom jUnit to Mutationtesting
From jUnit to Mutationtesting
 
Warum ich so auf das c von cdi stehe
Warum ich so auf das c von cdi steheWarum ich so auf das c von cdi stehe
Warum ich so auf das c von cdi stehe
 
Java8 ready for the future
Java8 ready for the futureJava8 ready for the future
Java8 ready for the future
 
JavaFX8 TestFX - CDI
JavaFX8   TestFX - CDIJavaFX8   TestFX - CDI
JavaFX8 TestFX - CDI
 
Java FX8 JumpStart - JUG ch - zürich
Java FX8   JumpStart - JUG ch - zürichJava FX8   JumpStart - JUG ch - zürich
Java FX8 JumpStart - JUG ch - zürich
 

Recently uploaded

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Recently uploaded (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

Proxy deep-dive java-one_20151027_001

  • 1. prepare for….. Proxy Deep Dive JavaOne 2015 20151027 1 @SvenRuppert
  • 2. @SvenRuppert has been coding java since 1996 Fellow / Senior Manager reply Group Germany - Munich 2
  • 3. @SvenRuppert has been coding java since 1996 3
  • 4. @SvenRuppert has been coding java since 1996 Projects in the field of: •Automobile-industry •Energy •Finance / Leasing •Space- Satellit- •Government / UN / World-bank Where? •Europe •Asia - from India up to Malaysia 3
  • 5. prepare for….. Proxy Deep Dive - the first steps some words about Adapter, Delegator and the first Proxy 4 @SvenRuppert
  • 8. Why a Proxy ? 6 @SvenRuppert Proxies you can use them for : generic parts inside your application hiding technical stuff decouple parts of your applications
  • 9. Why a Proxy ? 7 @SvenRuppert but it must be easy to use like…
  • 10. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); but it must be easy to use like…
  • 11. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = but it must be easy to use like…
  • 12. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) but it must be easy to use like…
  • 13. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() but it must be easy to use like…
  • 14. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() but it must be easy to use like…
  • 15. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() .addSecurityRule(() -> true) but it must be easy to use like…
  • 16. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() .addSecurityRule(() -> true) .addSecurityRule(() -> true) but it must be easy to use like…
  • 17. Why a Proxy ? 7 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() .addSecurityRule(() -> true) .addSecurityRule(() -> true) .build(); but it must be easy to use like…
  • 18. Why a Proxy ? 8 @SvenRuppert but it must be easy to use like…
  • 19. Why a Proxy ? 8 @SvenRuppert @Inject @Proxy(virtual = true, metrics = true) Service service; but it must be easy to use like…
  • 20. Adapter v Proxy - Pattern Hello World 9 @SvenRuppert Difference between Proxy and Adapter
  • 21. Adapter v Proxy - Pattern Hello World Adapter - or called Wrapper maybe : Use an Adapter if you have to use an incompatible interface between two systems. In detail I am using/talking about an Adapter with Delegator. 10 @SvenRuppert
  • 22. Adapter v Proxy - Pattern Hello World 11 @SvenRuppert
  • 23. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 @SvenRuppert
  • 24. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 public interface WorkerAdapter {
 public void workOnSomething(List<String> stringListe);
 } @SvenRuppert
  • 25. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 public interface WorkerAdapter {
 public void workOnSomething(List<String> stringListe);
 } public class Adapter implements WorkerAdapter {
 private LegacyWorker worker = new LegacyWorker();
 @Override
 public void workOnSomething(List<String> stringListe) {
 final String[] strings = stringListe.toArray(new String[0]);
 worker.writeTheStrings(strings);
 }
 } @SvenRuppert
  • 26. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 public interface WorkerAdapter {
 public void workOnSomething(List<String> stringListe);
 } public class Adapter implements WorkerAdapter {
 private LegacyWorker worker = new LegacyWorker();
 @Override
 public void workOnSomething(List<String> stringListe) {
 final String[] strings = stringListe.toArray(new String[0]);
 worker.writeTheStrings(strings);
 }
 } @SvenRuppert
  • 27. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 public interface WorkerAdapter {
 public void workOnSomething(List<String> stringListe);
 } public class Adapter implements WorkerAdapter {
 private LegacyWorker worker = new LegacyWorker();
 @Override
 public void workOnSomething(List<String> stringListe) {
 final String[] strings = stringListe.toArray(new String[0]);
 worker.writeTheStrings(strings);
 }
 } @SvenRuppert
  • 28. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 public interface WorkerAdapter {
 public void workOnSomething(List<String> stringListe);
 } public class Adapter implements WorkerAdapter {
 private LegacyWorker worker = new LegacyWorker();
 @Override
 public void workOnSomething(List<String> stringListe) {
 final String[] strings = stringListe.toArray(new String[0]);
 worker.writeTheStrings(strings);
 }
 } @SvenRuppert
  • 29. Adapter v Proxy - Pattern Hello World public class LegacyWorker {
 public void writeTheStrings(String[] strArray){
 Arrays.stream(strArray).forEach(System.out::println);
 }
 } 11 public interface WorkerAdapter {
 public void workOnSomething(List<String> stringListe);
 } public class Adapter implements WorkerAdapter {
 private LegacyWorker worker = new LegacyWorker();
 @Override
 public void workOnSomething(List<String> stringListe) {
 final String[] strings = stringListe.toArray(new String[0]);
 worker.writeTheStrings(strings);
 }
 } @SvenRuppert
  • 30. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 @SvenRuppert
  • 31. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 @SvenRuppert
  • 32. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 @SvenRuppert
  • 33. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 • No need to know the LegacyWorker @SvenRuppert
  • 34. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 • No need to know the LegacyWorker • No inheritance between LegacyWorker and Adapter @SvenRuppert
  • 35. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 • No need to know the LegacyWorker • No inheritance between LegacyWorker and Adapter • only a transformation of an interface @SvenRuppert
  • 36. Adapter v Proxy - Pattern Hello World public class Main {
 public static void main(String[] args) {
 final ArrayList<String> strings = new ArrayList<>();
 Collections.addAll(strings, „A","B","C","D"); 
 new Adapter().workOnSomething(strings);
 }
 } 12 • No need to know the LegacyWorker • No inheritance between LegacyWorker and Adapter • only a transformation of an interface • same creation time for Delegator and Adapter @SvenRuppert
  • 37. Proxy - the easiest version 13 @SvenRuppert We need a few steps to come from an Adapter with Delegator to a Proxy
  • 38. Proxy - the easiest version 13 • We need to know the LegacyWorker @SvenRuppert We need a few steps to come from an Adapter with Delegator to a Proxy
  • 39. Proxy - the easiest version 13 • We need to know the LegacyWorker • Inheritance between LegacyWorker and Adapter @SvenRuppert We need a few steps to come from an Adapter with Delegator to a Proxy
  • 40. Proxy - the easiest version 13 • We need to know the LegacyWorker • Inheritance between LegacyWorker and Adapter • do not use it like an Adapter ! @SvenRuppert We need a few steps to come from an Adapter with Delegator to a Proxy
  • 41. Proxy - the easiest version 13 • We need to know the LegacyWorker • Inheritance between LegacyWorker and Adapter • do not use it like an Adapter ! @SvenRuppert We need a few steps to come from an Adapter with Delegator to a Proxy please show the code
  • 42. Proxy - the easiest version - (Delegator) 14 @SvenRuppert
  • 43. Proxy - the easiest version - (Delegator) public interface Service {
 String work(String txt);
 } 14 @SvenRuppert
  • 44. Proxy - the easiest version - (Delegator) public interface Service {
 String work(String txt);
 } 14 @SvenRuppert public class ServiceImpl implements Service {
 public String work(String txt) {
 return "ServiceImpl - " + txt;
 }
 }
  • 45. Proxy - the easiest version - (Delegator) public interface Service {
 String work(String txt);
 } 14 @SvenRuppert public class ServiceImpl implements Service {
 public String work(String txt) {
 return "ServiceImpl - " + txt;
 }
 } public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 }
  • 47. Security Proxy 15 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 }
  • 48. Security Proxy 15 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ?
  • 49. Security Proxy 15 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? SecurityProxy: execute only if it is allowed
  • 50. Security Proxy 15 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? SecurityProxy: execute only if it is allowed public class ServiceSecurityProxy implements Service {
 private Service service = new ServiceImpl();
 private String code; //who will set this ?
 public String work(String txt) {
 if(code.equals(„hoppel")) { return service.work(txt); }
 else { return "nooooop"; }
 }
 }
  • 51. Security Proxy 15 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? SecurityProxy: execute only if it is allowed public class ServiceSecurityProxy implements Service {
 private Service service = new ServiceImpl();
 private String code; //who will set this ?
 public String work(String txt) {
 if(code.equals(„hoppel")) { return service.work(txt); }
 else { return "nooooop"; }
 }
 }
  • 52. Remote Proxy - JDK only 16 @SvenRuppert
  • 53. Remote Proxy - JDK only 16 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 }
  • 54. Remote Proxy - JDK only 16 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ?
  • 55. Remote Proxy - JDK only 16 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? RemoteProxy: execute outside of my process
  • 56. Remote Proxy - JDK only 16 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? RemoteProxy: execute outside of my process Service proxy = new ServiceRemoteProxy();
 String hello = proxy.work(„Hello");
  • 57. Remote Proxy - JDK only 16 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? RemoteProxy: execute outside of my process Service proxy = new ServiceRemoteProxy();
 String hello = proxy.work(„Hello");
  • 58. Remote Proxy - JDK only 16 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? RemoteProxy: execute outside of my process Service proxy = new ServiceRemoteProxy();
 String hello = proxy.work(„Hello"); some work needed
  • 59. Remote Proxy - JDK only 17 @SvenRuppert
  • 60. Remote Proxy - JDK only 17 @SvenRuppert
  • 61. Remote Proxy - JDK only 17 @SvenRuppert Interface
  • 62. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface
  • 63. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface remote communication
  • 64. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface remote communication
  • 65. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface remote communication
  • 66. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface remote communication
  • 67. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface remote communication
  • 68. Remote Proxy - JDK only 17 @SvenRuppert Proxy Interface Interface remote communication
  • 69. Remote Proxy - JDK only 17 @SvenRuppert Proxy Remote Service Interface Interface remote communication
  • 70. Remote Proxy - JDK only 17 @SvenRuppert Proxy Remote Service Interface Interface remote communication
  • 71. Remote Proxy - JDK only 17 @SvenRuppert Proxy Remote Service Interface Interface remote communication client
  • 72. Remote Proxy - JDK only 17 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 73. Remote Proxy - JDK only 17 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 74. Remote Proxy - JDK only 17 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 75. Remote Proxy - JDK only 18 @SvenRuppert @WebService
 @SOAPBinding(style = SOAPBinding.Style.RPC)
 public interface Service {
 @WebMethod
 public String work(String txt);
 }
  • 76. Remote Proxy - JDK only 19 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 77. Remote Proxy - JDK only 19 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 78. Remote Proxy - JDK only 20 @SvenRuppert @SOAPBinding(style = SOAPBinding.Style.RPC)
 @WebService(endpointInterface = "org.rapidpm.Service")
 public class ServiceImpl implements Service {
 @Override
 public String work(String txt) {
 return "ServiceImpl - " + txt;
 }
 }
  • 79. Remote Proxy - JDK only 21 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 80. Remote Proxy - JDK only 21 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 81. Remote Proxy - JDK only 22 @SvenRuppert final String address = "http://localhost:9999/ws/service";
 final ExecutorService threadPool = Executors.newFixedThreadPool(10);
 final Endpoint endpoint = Endpoint.create(new ServiceImpl());
 endpoint.setExecutor(threadPool);
 endpoint.publish(address);
 System.out.println("endpoint = " + endpoint.isPublished());
  • 82. Remote Proxy - JDK only 22 @SvenRuppert final String address = "http://localhost:9999/ws/service";
 final ExecutorService threadPool = Executors.newFixedThreadPool(10);
 final Endpoint endpoint = Endpoint.create(new ServiceImpl());
 endpoint.setExecutor(threadPool);
 endpoint.publish(address);
 System.out.println("endpoint = " + endpoint.isPublished());
  • 83. Remote Proxy - JDK only 23 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 84. Remote Proxy - JDK only 23 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 85. Remote Proxy - JDK only 24 @SvenRuppert public class ServiceRemoteProxy implements Service {
 
 private URL url;
 private Service realSubject;
 final String namespaceURI = "http://rapidpm.org/";
 final String localPart = "ServiceImplService";
 
 public ServiceRemoteProxy() {
 try {
 url = new URL("http://localhost:9999/ws/service?wsdl");
 QName qname = new QName(namespaceURI, localPart);
 javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);
 realSubject = service.getPort(Service.class);
 } catch (MalformedURLException e) {
 e.printStackTrace();
 }
 }
 public String work(String txt){
 return realSubject.work(txt);
 }
 }
  • 86. Remote Proxy - JDK only 24 @SvenRuppert public class ServiceRemoteProxy implements Service {
 
 private URL url;
 private Service realSubject;
 final String namespaceURI = "http://rapidpm.org/";
 final String localPart = "ServiceImplService";
 
 public ServiceRemoteProxy() {
 try {
 url = new URL("http://localhost:9999/ws/service?wsdl");
 QName qname = new QName(namespaceURI, localPart);
 javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);
 realSubject = service.getPort(Service.class);
 } catch (MalformedURLException e) {
 e.printStackTrace();
 }
 }
 public String work(String txt){
 return realSubject.work(txt);
 }
 }
  • 87. Remote Proxy - JDK only 24 @SvenRuppert public class ServiceRemoteProxy implements Service {
 
 private URL url;
 private Service realSubject;
 final String namespaceURI = "http://rapidpm.org/";
 final String localPart = "ServiceImplService";
 
 public ServiceRemoteProxy() {
 try {
 url = new URL("http://localhost:9999/ws/service?wsdl");
 QName qname = new QName(namespaceURI, localPart);
 javax.xml.ws.Service service = javax.xml.ws.Service.create(url, qname);
 realSubject = service.getPort(Service.class);
 } catch (MalformedURLException e) {
 e.printStackTrace();
 }
 }
 public String work(String txt){
 return realSubject.work(txt);
 }
 }
  • 88. Remote Proxy - JDK only 25 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server
  • 89. Remote Proxy - JDK only 25 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server • remote communication
  • 90. Remote Proxy - JDK only 25 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server • remote communication • most of this is technology based work
  • 91. Remote Proxy - JDK only 25 @SvenRuppert Proxy Remote Service Interface Interface remote communication client server • remote communication • most of this is technology based work • goal: developer will see no remote communication
  • 93. Virtual Proxy 26 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 }
  • 94. Virtual Proxy 26 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ?
  • 95. Virtual Proxy 26 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? Virtual Proxy: create the Delegator later
  • 96. Virtual Proxy 26 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? Virtual Proxy: create the Delegator later public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 }
  • 97. Virtual Proxy 26 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? Virtual Proxy: create the Delegator later public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 }
  • 98. Virtual Proxy 26 @SvenRuppert public class ServiceProxy implements Service {
 private Service service = new ServiceImpl();
 public String work(String txt) { return service.work(txt); }
 } What could we change now ? Virtual Proxy: create the Delegator later public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 }
  • 99. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 }
  • 100. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 }
  • 101. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 } This is NOT ThreadSafe
  • 102. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 } This is NOT ThreadSafe
  • 103. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 } This is NOT ThreadSafe fixed decision for an implementation
  • 104. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 } This is NOT ThreadSafe fixed decision for an implementation
  • 105. Virtual Proxy 27 @SvenRuppert public class VirtualService implements Service {
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); } 
 return service.work(txt);
 }
 } This is NOT ThreadSafe fixed decision for an implementation how to combine it with a FactoryPattern ?
  • 106. Virtual Proxy 28 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 107. Virtual Proxy 28 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 108. Virtual Proxy 28 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public interface ServiceFactory {
 Service createInstance();
 }
  • 109. Virtual Proxy 28 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public interface ServiceFactory {
 Service createInstance();
 }
  • 110. Virtual Proxy 28 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public interface ServiceFactory {
 Service createInstance();
 } public interface ServiceStrategyFactory {
 Service realSubject(ServiceFactory factory);
 }
  • 111. Virtual Proxy - Not - ThreadSafe 29 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 112. Virtual Proxy - Not - ThreadSafe 29 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 113. Virtual Proxy - Not - ThreadSafe 29 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 private ServiceFactory serviceFactory = ServiceImpl::new;
  • 114. Virtual Proxy - Not - ThreadSafe 29 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 private ServiceFactory serviceFactory = ServiceImpl::new;
  • 115. Virtual Proxy - Not - ThreadSafe 29 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceStrategyFactoryImpl implements ServiceStrategyFactory {
 Service realSubject;
 public Service realSubject(final ServiceFactory factory) {
 if (realSubject == null) {
 realSubject = factory.createInstance();
 }
 return realSubject;
 }
 } private ServiceFactory serviceFactory = ServiceImpl::new;
  • 116. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 117. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service {
  • 118. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new;
  • 119. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl();
  • 120. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) {
  • 121. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) { }
 }
  • 122. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) { return }
 }
  • 123. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) { return strategyFactory }
 }
  • 124. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) { return strategyFactory .realSubject( }
 }
  • 125. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) { return strategyFactory .realSubject(serviceFactory }
 }
  • 126. Virtual Proxy - Not - ThreadSafe 30 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceProxy implements Service { private ServiceFactory serviceFactory = ServiceImpl::new; private ServiceStrategyFactory strategyFactory = new ServiceStrategyFactoryImpl(); public String work(String txt) { return strategyFactory .realSubject(serviceFactory).work(txt); }
 }
  • 127. Virtual Proxy - Synchronized 31 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 128. Virtual Proxy - Synchronized 31 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 129. Virtual Proxy - Synchronized 31 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 private ServiceFactory serviceFactory = ServiceImpl::new;
  • 130. Virtual Proxy - Synchronized 31 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 private ServiceFactory serviceFactory = ServiceImpl::new;
  • 131. Virtual Proxy - Synchronized 31 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceStrategyFactorySynchronized implements ServiceStrategyFactory {
 Service realSubject;
 public synchronized Service realSubject(final ServiceFactory factory) {
 if (realSubject == null) {
 realSubject = factory.createInstance();
 }
 return realSubject;
 }
 } private ServiceFactory serviceFactory = ServiceImpl::new;
  • 132. Virtual Proxy - Synchronized 31 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceStrategyFactorySynchronized implements ServiceStrategyFactory {
 Service realSubject;
 public synchronized Service realSubject(final ServiceFactory factory) {
 if (realSubject == null) {
 realSubject = factory.createInstance();
 }
 return realSubject;
 }
 } private ServiceFactory serviceFactory = ServiceImpl::new; This is ThreadSafe
  • 133. Virtual Proxy - Method Scoped 32 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 134. Virtual Proxy - Method Scoped 32 @SvenRuppert if(service == null) { service = new ServiceImpl(); }

  • 135. Virtual Proxy - Method Scoped 32 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 private ServiceFactory serviceFactory = ServiceImpl::new;
  • 136. Virtual Proxy - Method Scoped 32 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 private ServiceFactory serviceFactory = ServiceImpl::new;
  • 137. Virtual Proxy - Method Scoped 32 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceStrategyFactoryMethodScoped implements ServiceStrategyFactory {
 public Service realSubject(final ServiceFactory factory) { 
 return factory.createInstance();
 }
 } private ServiceFactory serviceFactory = ServiceImpl::new;
  • 138. Virtual Proxy - Method Scoped 32 @SvenRuppert if(service == null) { service = new ServiceImpl(); }
 public class ServiceStrategyFactoryMethodScoped implements ServiceStrategyFactory {
 public Service realSubject(final ServiceFactory factory) { 
 return factory.createInstance();
 }
 } private ServiceFactory serviceFactory = ServiceImpl::new;
  • 139. Combining Proxies 33 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 140. Combining Proxies 33 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 141. Combining Proxies 33 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 142. Combining Proxies 33 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 143. Combining Proxies 33 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 144. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 }
  • 145. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 }
  • 146. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 }
  • 147. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 }
  • 148. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 }
  • 149. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 } hard wired proxies
  • 150. Combining Proxies - SecureVirtualProxy 34 @SvenRuppert Security Proxy Virtual Proxy public class VirtualProxy implements Service {
 public VirtualProxy() { out.println("VirtualProxy " + now()); }
 private Service service = null;
 public String work(String txt) {
 if(service == null) { service = new ServiceImpl(); }
 return service.work(txt);
 }
 } public class SecureVirtualProxy implements Service {
 public SecureProxy() { out.println("SecureProxy " + now()); }
 private Service service = new VirtualProxy();
 //SNIPP….
 public String work(String txt) {
 if(code.equals("hoppel")) { return service.work(txt); }
 return { return "nooooop"; }
 }
 } hard wired proxies What could we change now ?
  • 151. Combining Proxies - SecureVirtualProxy 35 @SvenRuppert Security Proxy Virtual Proxy hard wired proxies What could we change now ? we need a Generic Proxy
  • 152. Combining Proxies - SecureVirtualProxy 35 @SvenRuppert Security Proxy Virtual Proxy hard wired proxies What could we change now ? we need a Generic Proxy since JDK1.3 we have it
  • 153. Combining Proxies - SecureVirtualProxy 35 @SvenRuppert Security Proxy Virtual Proxy hard wired proxies What could we change now ? we need a Generic Proxy since JDK1.3 we have it the DynamicProxy
  • 154. prepare for….. Proxy Deep Dive - DynamicProxies some words about : how to use and how to extend….but.. also what is not nice 36 @SvenRuppert
  • 157. Dynamic Proxy - Hello World 38 @SvenRuppert How to use it ?
  • 158. Dynamic Proxy - Hello World 38 @SvenRuppert How to use it ? java.lang.reflect.Proxy
  • 159. Dynamic Proxy - Hello World 38 @SvenRuppert How to use it ? java.lang.reflect.Proxy we need an interface : here Service
  • 160. Dynamic Proxy - Hello World 38 @SvenRuppert How to use it ? java.lang.reflect.Proxy we need an interface : here Service we need an implementation : here ServiceImpl
  • 161. Dynamic Proxy - Hello World 38 @SvenRuppert How to use it ? java.lang.reflect.Proxy we need an interface : here Service we need an implementation : here ServiceImpl Service proxyInstance = Service.class.cast( Proxy.newProxyInstance(
 Service.class.getClassLoader(),
 new Class<?>[]{Service.class},
 new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 return method.invoke(service, args);
 }
 }
 )
 );
  • 162. Dynamic Proxy - Hello World 38 @SvenRuppert How to use it ? java.lang.reflect.Proxy we need an interface : here Service we need an implementation : here ServiceImpl Service proxyInstance = Service.class.cast( Proxy.newProxyInstance(
 Service.class.getClassLoader(),
 new Class<?>[]{Service.class},
 new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 return method.invoke(service, args);
 }
 }
 )
 ); OK, step by step , please
  • 163. Dynamic Proxy - Hello World 39 @SvenRuppert
  • 164. Dynamic Proxy - Hello World 39 @SvenRuppert Service proxyInstance = Service.class.cast (
  • 165. Dynamic Proxy - Hello World 39 @SvenRuppert Proxy.newProxyInstance ( Service proxyInstance = Service.class.cast (
  • 166. Dynamic Proxy - Hello World 39 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), Service proxyInstance = Service.class.cast (
  • 167. Dynamic Proxy - Hello World 39 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast (
  • 168. Dynamic Proxy - Hello World 39 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
  • 169. Dynamic Proxy - Hello World 39 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 } )
 );
  • 170. Dynamic Proxy - Hello World 39 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 } )
 ); how to make a VirtualProxy ?
  • 171. Dynamic Virtual Proxy 40 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( )
 );
  • 172. Dynamic Virtual Proxy 40 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service;
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 if(service == null) { service = new ServiceImpl(); } return m.invoke(service, args);
 }
 } )
 );
  • 173. Dynamic Virtual Proxy 40 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service;
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 if(service == null) { service = new ServiceImpl(); } return m.invoke(service, args);
 }
 } )
 );
  • 174. Dynamic Virtual Proxy 40 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service;
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 if(service == null) { service = new ServiceImpl(); } return m.invoke(service, args);
 }
 } )
 );
  • 175. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 );
  • 176. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 );
  • 177. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 );
  • 178. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 );
  • 179. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 ); get it from outside
  • 180. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 ); get it from outside
  • 181. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 ); get it from outside we will remove it
  • 182. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 ); get it from outside we will remove it
  • 183. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 ); get it from outside we will remove it we will get it from outside
  • 184. Dynamic Proxy - make it generic 41 @SvenRuppert Proxy.newProxyInstance ( Service.class.getClassLoader(), new Class<?>[]{Service.class}, Service proxyInstance = Service.class.cast ( new InvocationHandler() {
 private final ServiceImpl service = new ServiceImpl();
 @Override
 public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 return m.invoke(service, args);
 }
 }
 )
 ); get it from outside we will remove it we will get it from outside and finally we will call it ProxyGenerator
  • 185. Dynamic Proxy - make it generic 42 @SvenRuppert public class ProxyGenerator {
 public static <P> P makeProxy(Class<P> subject, P realSubject) {
 Object proxyInstance = Proxy.newProxyInstance(
 subject.getClassLoader(),
 new Class<?>[]{subject},
 new InvocationHandler() {
 @Override
 public Object invoke(final Object proxy, final Method m, final Object[] args) throws Throwable {
 return m.invoke(realSubject, args);
 }
 }
 );
 return subject.cast(proxyInstance);
 }
 }
  • 186. Dynamic Proxy - make it generic 43 @SvenRuppert public class ProxyGenerator {
 public static <P> P makeProxy(Class<P> subject, P realSubject) {
 Object proxyInstance = Proxy.newProxyInstance(
 subject.getClassLoader(),
 new Class<?>[]{subject},
 (proxy, m, args) -> m.invoke(realSubject, args)
 );
 return subject.cast(proxyInstance);
 }
 }
  • 187. Dynamic Proxy - make it generic 44 @SvenRuppert with DynamicProxies - we are writing less (more generic) code - we can create Virtual-/Remote-/Security Proxies
  • 188. Dynamic Proxy - make it generic 44 @SvenRuppert with DynamicProxies - we are writing less (more generic) code - we can create Virtual-/Remote-/Security Proxies but how to combine Proxies more generic ?
  • 189. Dynamic Proxy - make it generic 45 @SvenRuppert but how to combine Proxies more generic ? for this we have to switch from
  • 190. Dynamic Proxy - make it generic 45 @SvenRuppert but how to combine Proxies more generic ? for this we have to switch from Factory-Method-Pattern
  • 191. Dynamic Proxy - make it generic 45 @SvenRuppert but how to combine Proxies more generic ? for this we have to switch from Factory-Method-Pattern
  • 192. Dynamic Proxy - make it generic 45 @SvenRuppert but how to combine Proxies more generic ? for this we have to switch from Factory-Method-Pattern Builder-Pattern
  • 193. DynamicProxyBuilder 46 @SvenRuppert but how to combine Proxies more generic ? Security Proxy Virtual ProxySecurity Proxy let´s think about Two possible ways:
  • 194. DynamicProxyBuilder 46 @SvenRuppert but how to combine Proxies more generic ? Security Proxy Virtual ProxySecurity Proxy let´s think about Two possible ways: 1 Security Proxy with 2 Rules and 1 VirtualProxy
  • 195. DynamicProxyBuilder 46 @SvenRuppert but how to combine Proxies more generic ? Security Proxy Virtual ProxySecurity Proxy let´s think about Two possible ways: 1 Security Proxy with 2 Rules and 1 VirtualProxy 2 Security Proxies with 1 Rule and 1 VirtualProxy
  • 196. DynamicProxyBuilder 46 @SvenRuppert but how to combine Proxies more generic ? Security Proxy Virtual ProxySecurity Proxy let´s think about Two possible ways: 1 Security Proxy with 2 Rules and 1 VirtualProxy 2 Security Proxies with 1 Rule and 1 VirtualProxy OK, we need a generic CascadedProxy
  • 197. DynamicProxyBuilder 47 @SvenRuppert OK, we need a generic CascadedProxy Proxy n Proxy n-1 Proxy n-2 Proxy n-3 a child must know his parent first we only add different DynamicProxies always the same target interface
  • 198. DynamicProxyBuilder 47 @SvenRuppert OK, we need a generic CascadedProxy Proxy n Proxy n-1 Proxy n-2 Proxy n-3 a child must know his parent first we only add different DynamicProxies always the same target interface
  • 200. DynamicProxyBuilder 48 @SvenRuppert for example: add n SecurityRules public interface SecurityRule {
 boolean checkRule();
 }
  • 201. DynamicProxyBuilder 48 @SvenRuppert for example: add n SecurityRules public interface SecurityRule {
 boolean checkRule();
 }
  • 202. DynamicProxyBuilder 48 @SvenRuppert for example: add n SecurityRules public interface SecurityRule {
 boolean checkRule();
 } functional interface
  • 203. DynamicProxyBuilder 48 @SvenRuppert private List<SecurityRule> securityRules = new ArrayList<>(); for example: add n SecurityRules public interface SecurityRule {
 boolean checkRule();
 } functional interface
  • 204. DynamicProxyBuilder 48 @SvenRuppert private List<SecurityRule> securityRules = new ArrayList<>(); for example: add n SecurityRules public interface SecurityRule {
 boolean checkRule();
 } functional interface Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 205. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 206. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 207. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } last child Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 208. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } last child Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 209. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } last child work on child Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 210. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } last child work on child Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 211. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } last child work on child set child for next level Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 212. DynamicProxyBuilder 49 @SvenRuppert private ProxyBuilder<I, T> build_addSecurityRule(SecurityRule rule) {
 final ClassLoader classLoader = original.getClass().getClassLoader();
 final Class<?>[] interfaces = {clazz};
 final Object nextProxy = Proxy.newProxyInstance(
 classLoader, interfaces,
 new InvocationHandler() {
 private T original = ProxyBuilder.this.original;
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 final boolean checkRule = rule.checkRule();
 if (checkRule) {
 return method.invoke(original, args);
 } else {
 return null;
 }
 }
 });
 original = (T) clazz.cast(nextProxy);
 return this;
 } how this will be used ? last child work on child set child for next level Collections.reverse(securityRules);
 securityRules.forEach(this::build_addSecurityRule);
  • 215. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic =
  • 216. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original)
  • 217. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging()
  • 218. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics()
  • 219. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() .addSecurityRule(() -> true)
  • 220. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() .addSecurityRule(() -> true) .addSecurityRule(() -> true)
  • 221. DynamicProxyBuilder 50 @SvenRuppert final InnerDemoClass original = new InnerDemoClass(); final InnerDemoInterface demoLogic = ProxyBuilder.createBuilder(InnerDemoInterface.class, original) .addLogging() .addMetrics() .addSecurityRule(() -> true) .addSecurityRule(() -> true) .build();
  • 223. DynamicProxyBuilder 51 @SvenRuppert Until now, we had only a cascade of one interface
  • 224. DynamicProxyBuilder 51 @SvenRuppert Until now, we had only a cascade of one interface how to deal with different interfaces ?
  • 225. DynamicProxyBuilder 51 @SvenRuppert Until now, we had only a cascade of one interface how to deal with different interfaces ?
  • 226. DynamicProxyBuilder 51 @SvenRuppert Until now, we had only a cascade of one interface how to deal with different interfaces ? we need a generic NestedBuilder
  • 227. NestedBuilder - The Beginning 52 @SvenRuppert
  • 228. NestedBuilder - The Beginning 53 @SvenRuppert
  • 229. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build();
  • 230. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build();
  • 231. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
  • 232. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
  • 233. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = new ArrayList<>();
 wheels.add(wheel1);
 wheels.add(wheel2);
 wheels.add(wheel3);
  • 234. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = new ArrayList<>();
 wheels.add(wheel1);
 wheels.add(wheel2);
 wheels.add(wheel3); }
  • 235. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = new ArrayList<>();
 wheels.add(wheel1);
 wheels.add(wheel2);
 wheels.add(wheel3); }
  • 236. NestedBuilder - The Beginning 53 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); List<Wheel> wheels = new ArrayList<>();
 wheels.add(wheel1);
 wheels.add(wheel2);
 wheels.add(wheel3); }
  • 237. NestedBuilder - The Beginning 54 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); List<Wheel> wheels = new ArrayList<>();
 wheels.add(wheel1);
 wheels.add(wheel2);
 wheels.add(wheel3);
  • 238. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); List<Wheel> wheels = new ArrayList<>();
 wheels.add(wheel1);
 wheels.add(wheel2);
 wheels.add(wheel3);
  • 239. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
  • 240. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); //List<Wheel> wheels = new ArrayList<>();
 // wheels.add(wheel1);
 // wheels.add(wheel2);
 // wheels.add(wheel3);
  • 241. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); //List<Wheel> wheels = new ArrayList<>();
 // wheels.add(wheel1);
 // wheels.add(wheel2);
 // wheels.add(wheel3); List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build();
  • 242. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); //List<Wheel> wheels = new ArrayList<>();
 // wheels.add(wheel1);
 // wheels.add(wheel2);
 // wheels.add(wheel3); List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build(); //more robust if you add tests at build()
  • 243. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); //List<Wheel> wheels = new ArrayList<>();
 // wheels.add(wheel1);
 // wheels.add(wheel2);
 // wheels.add(wheel3); List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build(); //more robust if you add tests at build() } }
  • 244. NestedBuilder - The Beginning 55 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); //List<Wheel> wheels = new ArrayList<>();
 // wheels.add(wheel1);
 // wheels.add(wheel2);
 // wheels.add(wheel3); List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build(); //more robust if you add tests at build() } } how to combine ?
  • 245. NestedBuilder - The Beginning 56 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build(); } } how to combine ?
  • 246. NestedBuilder - The Beginning 57 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); // Wheel wheel1 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); 
 // Wheel wheel2 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build();
 // Wheel wheel3 = Wheel.newBuilder().withType(2).withColour(3).withSize(4).build(); // List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build();
  • 247. NestedBuilder - The Beginning 57 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); // List<Wheel> wheelList = WheelListBuilder.newBuilder()
 .withNewList()
 .addWheel(wheel1)
 .addWheel(wheel2)
 .addWheel(wheel3)
 .build();
  • 248. NestedBuilder - The Beginning 57 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build();
  • 249. NestedBuilder - The Beginning 57 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build();
  • 250. NestedBuilder - The Beginning 58 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build();
  • 251. NestedBuilder - The Beginning 58 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build();
  • 252. NestedBuilder - The Beginning 58 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build(); WheelBuilder
  • 253. NestedBuilder - The Beginning 58 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build(); WheelBuilder
  • 254. NestedBuilder - The Beginning 58 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build(); WheelBuilder WheelListBuilder
  • 255. NestedBuilder - The Beginning 59 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build();
  • 256. NestedBuilder - The Beginning 59 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); Engine engine = Engine.newBuilder().withPower(100).withType(5).build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build(); now we have to combine all
  • 257. NestedBuilder - The Beginning 59 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); List<Wheel> wheels = wheelListBuilder
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .build(); now we have to combine all
  • 258. NestedBuilder - The Beginning 59 @SvenRuppert Car car = Car.newBuilder()
 .withEngine(engine)
 .withWheelList(wheels)
 .build(); now we have to combine all
  • 259. NestedBuilder - The Beginning 59 @SvenRuppert now we have to combine all
  • 260. NestedBuilder - The Beginning 59 @SvenRuppert now we have to combine all
  • 261. NestedBuilder - The Beginning 59 @SvenRuppert now we have to combine all Car car = Car.newBuilder()
 .addEngine().withPower(100).withType(5).done()
 .addWheels()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .done()
 .build();
  • 262. NestedBuilder - The Beginning 59 @SvenRuppert now we have to combine all Car car = Car.newBuilder()
 .addEngine().withPower(100).withType(5).done()
 .addWheels()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .addWheel().withType(1).withSize(2).withColour(2).addWheelToList()
 .done()
 .build();
  • 263. NestedBuilder - The Pattern 60 @SvenRuppert public abstract class NestedBuilder<T, V> { protected T parent; public abstract V build() public <P extends NestedBuilder<T, V>> P 
 withParentBuilder(T parent) {
 this.parent = parent;
 return (P) this;
 }
  • 264. NestedBuilder - The Pattern 60 @SvenRuppert public abstract class NestedBuilder<T, V> { protected T parent; public abstract V build() public <P extends NestedBuilder<T, V>> P 
 withParentBuilder(T parent) {
 this.parent = parent;
 return (P) this;
 }
  • 265. NestedBuilder - The Pattern 60 @SvenRuppert public abstract class NestedBuilder<T, V> { protected T parent; public abstract V build() public <P extends NestedBuilder<T, V>> P 
 withParentBuilder(T parent) {
 this.parent = parent;
 return (P) this;
 } parent will connect itself…
  • 266. NestedBuilder - The Pattern 61 @SvenRuppert
  • 267. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> {
  • 268. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> { public T done() {
  • 269. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> { public T done() { Class<?> parentClass = parent.getClass();
  • 270. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> { public T done() { Class<?> parentClass = parent.getClass(); try {
 V build = this.build();
 String methodname = "with" + build.getClass().getSimpleName();
 Method method = parentClass.getDeclaredMethod(
 methodname, build.getClass());
 method.invoke(parent, build);
 } catch (NoSuchMethodException 
 | IllegalAccessException | InvocationTargetException e) {
 e.printStackTrace();
 }
  • 271. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> { public T done() { Class<?> parentClass = parent.getClass(); try {
 V build = this.build();
 String methodname = "with" + build.getClass().getSimpleName();
 Method method = parentClass.getDeclaredMethod(
 methodname, build.getClass());
 method.invoke(parent, build);
 } catch (NoSuchMethodException 
 | IllegalAccessException | InvocationTargetException e) {
 e.printStackTrace();
 } }
  • 272. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> { public T done() { Class<?> parentClass = parent.getClass(); try {
 V build = this.build();
 String methodname = "with" + build.getClass().getSimpleName();
 Method method = parentClass.getDeclaredMethod(
 methodname, build.getClass());
 method.invoke(parent, build);
 } catch (NoSuchMethodException 
 | IllegalAccessException | InvocationTargetException e) {
 e.printStackTrace();
 } }
  • 273. NestedBuilder - The Pattern 61 @SvenRuppert public abstract class NestedBuilder<T, V> { public T done() { Class<?> parentClass = parent.getClass(); try {
 V build = this.build();
 String methodname = "with" + build.getClass().getSimpleName();
 Method method = parentClass.getDeclaredMethod(
 methodname, build.getClass());
 method.invoke(parent, build);
 } catch (NoSuchMethodException 
 | IllegalAccessException | InvocationTargetException e) {
 e.printStackTrace();
 } } connect itself with parent
  • 274. NestedBuilder - The Pattern 62 @SvenRuppert
  • 275. NestedBuilder - The Pattern 62 @SvenRuppert the basic steps in short words
  • 276. NestedBuilder - The Pattern 62 @SvenRuppert the basic steps in short words the Parent-Builder will hold the Child-Builder
  • 277. NestedBuilder - The Pattern 62 @SvenRuppert the basic steps in short words the Parent-Builder will hold the Child-Builder the Parent-Builder will have a addChild - Method
  • 278. NestedBuilder - The Pattern 62 @SvenRuppert the basic steps in short words the Parent-Builder will hold the Child-Builder the Parent-Builder will have a addChild - Method the Child-Builder will extend the NestedBuilder
  • 279. NestedBuilder - The Pattern 62 @SvenRuppert the basic steps in short words the Parent-Builder will hold the Child-Builder the Parent-Builder will have a addChild - Method the Child-Builder will extend the NestedBuilder the rest could be generated with a 
 default Builder-Generator
  • 280. NestedBuilder - The Pattern 63 @SvenRuppert public class Parent {
 private KidA kidA;
 private KidB kidB;
 //snipp.....
 public static final class Builder {
 private KidA kidA;
 private KidB kidB;
 // to add manually
 private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);
 private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);
 
 public KidA.Builder addKidA() {
 return this.builderKidA;
 }
 public KidB.Builder addKidB() {
 return this.builderKidB;
 }
 //---------
  • 281. NestedBuilder - The Pattern 63 @SvenRuppert public class Parent {
 private KidA kidA;
 private KidB kidB;
 //snipp.....
 public static final class Builder {
 private KidA kidA;
 private KidB kidB;
 // to add manually
 private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);
 private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);
 
 public KidA.Builder addKidA() {
 return this.builderKidA;
 }
 public KidB.Builder addKidB() {
 return this.builderKidB;
 }
 //---------
  • 282. NestedBuilder - The Pattern 63 @SvenRuppert public class Parent {
 private KidA kidA;
 private KidB kidB;
 //snipp.....
 public static final class Builder {
 private KidA kidA;
 private KidB kidB;
 // to add manually
 private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);
 private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);
 
 public KidA.Builder addKidA() {
 return this.builderKidA;
 }
 public KidB.Builder addKidB() {
 return this.builderKidB;
 }
 //--------- connect itself with child
  • 283. NestedBuilder - The Pattern 63 @SvenRuppert public class Parent {
 private KidA kidA;
 private KidB kidB;
 //snipp.....
 public static final class Builder {
 private KidA kidA;
 private KidB kidB;
 // to add manually
 private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);
 private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);
 
 public KidA.Builder addKidA() {
 return this.builderKidA;
 }
 public KidB.Builder addKidB() {
 return this.builderKidB;
 }
 //--------- connect itself with child
  • 284. NestedBuilder - The Pattern 63 @SvenRuppert public class Parent {
 private KidA kidA;
 private KidB kidB;
 //snipp.....
 public static final class Builder {
 private KidA kidA;
 private KidB kidB;
 // to add manually
 private KidA.Builder builderKidA = KidA.newBuilder().withParentBuilder(this);
 private KidB.Builder builderKidB = KidB.newBuilder().withParentBuilder(this);
 
 public KidA.Builder addKidA() {
 return this.builderKidA;
 }
 public KidB.Builder addKidB() {
 return this.builderKidB;
 }
 //--------- connect itself with child switch to Child-Builder
  • 285. NestedBuilder - The Pattern 64 @SvenRuppert public static final class Builder 
 extends NestedBuilder<Parent.Builder, KidA> {
  • 286. NestedBuilder - The Pattern 64 @SvenRuppert public static final class Builder 
 extends NestedBuilder<Parent.Builder, KidA> {
  • 287. NestedBuilder - The Pattern 64 @SvenRuppert only extends on Child-Builder public static final class Builder 
 extends NestedBuilder<Parent.Builder, KidA> {
  • 288. NestedBuilder - The Pattern 65 @SvenRuppert
  • 289. NestedBuilder - The Pattern 65 @SvenRuppert Parent build = Parent.newBuilder()
 
 .addKidA().withNote("A").done()
 .addKidB().withNote("B").done()
 
 .build();
 System.out.println("build = " + build);
  • 290. NestedBuilder - The Pattern 65 @SvenRuppert Parent build = Parent.newBuilder()
 
 .addKidA().withNote("A").done()
 .addKidB().withNote("B").done()
 
 .build();
 System.out.println("build = " + build); and a child could be a parent in the same time
  • 291. NestedBuilder - The Pattern 65 @SvenRuppert Parent build = Parent.newBuilder()
 
 .addKidA().withNote("A").done()
 .addKidB().withNote("B").done()
 
 .build();
 System.out.println("build = " + build); and a child could be a parent in the same time Parent build = Parent.newBuilder()
 .addKidA().withNote("A") .addKidB().withNote("B").done() .done()
 .build(); System.out.println("build = " + build);

  • 292. NestedProxyBuilder - The Goal 66 @SvenRuppert handwritten , params for diff Proxies
  • 293. Combining Proxies 67 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 294. Combining Proxies 67 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 295. Combining Proxies 67 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 296. Combining Proxies 67 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 297. Combining Proxies 67 @SvenRuppert Security Proxy Virtual ProxyRemote Proxy Security ProxyVirtual ProxyRemote Proxy Security ProxyVirtual Proxy Remote Proxy VirtualProxies are mostly the last one combining Proxies is easy SecurityProxies are mostly the first one ?
  • 298. DynamicProxyBuilder 68 @SvenRuppert One functionality leads to one Proxy Instance runtime overhead for every Proxy - but make the first design simple.. optimize later
  • 299. DynamicObjectAdapter - orig Version 69 @SvenRuppert the original Version of the DynamicObjectAdapter was written by Dr. Heinz Kabutz
  • 300. DynamicObjectAdapter - orig Version 70 @SvenRuppert
  • 301. DynamicObjectAdapter - orig Version 70 @SvenRuppert
  • 302. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy
  • 303. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy orig / adapter
  • 304. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy orig / adapter
  • 305. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy orig / adapter invoke orig
  • 306. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy orig / adapter invoke orig
  • 307. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy orig / adapter invoke orig invoke adapter
  • 308. DynamicObjectAdapter - orig Version 70 @SvenRuppert Proxy orig / adapter invoke orig invoke adapter Goal: do not need to write an Adapter with Delegator
  • 309. DynamicObjectAdapter - orig Version 71 @SvenRuppert core concept: switching between method implementations
  • 310. DynamicObjectAdapter - orig Version 71 @SvenRuppert core concept: switching between method implementations how to identify a method?
  • 311. DynamicObjectAdapter - orig. Version 72 @SvenRuppert public interface Service {
 String doWork_A();
 }
 
 public static class ServiceImpl implements Service {
 public String doWork_A() {
 return ServiceImpl.class.getSimpleName();
 }
 }
  • 312. DynamicObjectAdapter - orig Version 73 @SvenRuppert how to identify a method? public class MethodIdentifier {
 private final String name;
 private final Class[] parameters;
 
 public MethodIdentifier(Method m) {
 name = m.getName();
 parameters = m.getParameterTypes();
 }
 // we can save time by assuming that we only compare against
 // other MethodIdentifier objects
 public boolean equals(Object o) {
 MethodIdentifier mid = (MethodIdentifier) o;
 return name.equals(mid.name) &&
 Arrays.equals(parameters, mid.parameters);
 }
 public int hashCode() { return name.hashCode(); }
 }
  • 313. DynamicObjectAdapter - orig Version 73 @SvenRuppert how to identify a method? public class MethodIdentifier {
 private final String name;
 private final Class[] parameters;
 
 public MethodIdentifier(Method m) {
 name = m.getName();
 parameters = m.getParameterTypes();
 }
 // we can save time by assuming that we only compare against
 // other MethodIdentifier objects
 public boolean equals(Object o) {
 MethodIdentifier mid = (MethodIdentifier) o;
 return name.equals(mid.name) &&
 Arrays.equals(parameters, mid.parameters);
 }
 public int hashCode() { return name.hashCode(); }
 }