-
Notifications
You must be signed in to change notification settings - Fork 5
/
tutorial.html
126 lines (97 loc) · 5.81 KB
/
tutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
(c) 2005, 2009, 2010 ThoughtWorks Ltd. All rights reserved.
(c) 2015 ProxyToys Committers. All rights reserved.
The software in this package is published under the terms of the BSD
style license a copy of which has been included with this distribution in
the LICENSE.txt file.
Created on 07-May-2005
-->
<head>
<title>ProxyToys - One minute Tutorial</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="banner">
<a href="index.html"><img id="logo" src="logo.gif" alt="ProxyToys Logo"/></a>
<div id="title">One minute Tutorial</div>
</div>
<div id="center" class="Content2Column">
<div id="content">
<p>ProxyToys is not difficult. If you are familiar with Reflection's Proxy class of the JDK, then you already know how to
use a ProxyFactory.</p>
<h1 id="delegating">Simple delegating example</h1>
<p>See this simple example, that uses a <a href="apidocs/com/thoughtworks/proxy/ProxyFactory.html">ProxyFactory</a>
based on the Java's Reflection capability called <a href="apidocs/com/thoughtworks/proxy/factory/StandardProxyFactory.html">StandardProxyFactory</a>.
The ArrayList passed in implementation-hidden behind the List interface:</p>
<div class="Source Java">
<pre>List proxy = Delegating.proxy(List.class).with(new ArrayList()).build(new StandardProxyFactory())
proxy.add("Hello World");
System.out.println("Size of list: " + proxy.size());
System.out.println("First element of list: " + proxy.get(0));</pre></div>
<p><b>Note:</b> You can only use the methods of the List interface, to access the instance,
methods like <code>java.lang.ArrayList.ensureCapacity(int)</code> are not accessible.
You cannot cast back to implementation.</p>
<p>Just replace the instance of the
<a href="apidocs/com/thoughtworks/proxy/factory/StandardProxyFactory.html">StandardProxyFactory</a>
with <a href="apidocs/com/thoughtworks/proxy/factory/CglibProxyFactory.html">CglibProxyFactory</a> to use a different
implementation. You have to put the cglib-nodep-2.2.jar (or above) into your classpath though. Also note that StandardProxyFactory
is also the default, meaning you can used build() without parameters.</p>
<h1 id="echoing">Simple Echoing example</h1>
<p>Each toy is a factory for proxy instances, that solve a common usage pattern of a proxy instance.
Every toy has a factory with a builder style API:</p>
<div class="highlight"><code><Toy>.proxy(...).build();</code></div>
<p>As example see a usage of the <a href="apidocs/com/thoughtworks/proxy/toys/echo/Echoing.html">Echo</a> toy:</p>
<div class="Source Java"><pre>Map<String, String> map = Echoing.proxy(Map.class)
.with(new HashMap<String, String>())
.to(new PrintWriter(System.out))
.build();
map.put("hello", "world");
</pre></div>
<p>This proxy is generated by a StandardProxyFactory, implements the Map interface and any call done to the
underlying object will be printed to <code>System.out</code>.</p>
<p>There are many different ProxyToy types. See <a href="toys.html">Toys</a></p>
<br/><br/>
</div>
</div>
<div class="SidePanel" id="left">
<div class="MenuGroup">
<h1>ProxyToxy in General</h1>
<ul>
<li><a href="index.html">About ProxyToys</a></li>
<li><a href="news.html">News</a></li>
<li><a href="changes.html">Change History</a></li>
<li><a href="license.html">License</a></li>
<li><a href="download.html">Download</a></li>
<li><a href="versioning.html">About Versioning</a></li>
</ul>
</div>
<div class="MenuGroup">
<h1>Using ProxyToys</h1>
<ul>
<li class="currentLink">One minute Tutorial</li>
<li><a href="factories.html">Proxy Factories</a></li>
<li><a href="toys.html">Toys</a></li>
<li><a href="faq.html">Frequently Asked Questions</a></li>
<li><a href="example-code.html">Example Code</a></li>
<li><a href="mailing-lists.html">Mailing Lists</a></li>
<li><a href="apidocs/index.html">Javadoc</a></li>
<li><a href="issues.html">Reporting Issues</a></li>
</ul>
</div>
<div class="MenuGroup">
<h1>Developing ProxyToys</h1>
<ul>
<li><a href="architecture.html">Architecture Overview</a></li>
<li><a href="how-to-contribute.html">How to Contribute</a></li>
<li><a href="team.html">Development Team</a></li>
<li><a href="repository.html">Source Repository</a></li>
<li><a href="https://github.com/proxytoys/proxytoys">GitHub Project</a></li>
<li><a href="http://ci.codehaus.org/browse/PROXYTOYS">Build Status</a></li>
<li><a href="https://www.openhub.net/p/10075">Code Statistics</a></li>
</ul>
</div>
</div>
</body>
</html>