You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One more thing to improve is using builder pattern for options:
Code
packageio.github.centrifugal.centrifuge;
importjava.net.Proxy;
importjava.util.Map;
/** * Configuration for a {@link Client} instance. */publicclassOptions {
privatefinalStringtoken;
privatefinalConnectionTokenGettertokenGetter;
privatefinalStringname;
privatefinalStringversion;
privatefinalbyte[] data;
privatefinalMap<String, String> headers;
privatefinalinttimeout;
privatefinalintminReconnectDelay;
privatefinalintmaxReconnectDelay;
privatefinalintmaxServerPingDelay;
privatefinalProxyproxy;
privatefinalStringproxyLogin;
privatefinalStringproxyPassword;
privatefinalDnsdns;
publicStringgetToken() {
returntoken;
}
publicConnectionTokenGettergetTokenGetter() {
returntokenGetter;
}
publicStringgetName() {
returnname;
}
publicStringgetVersion() {
returnversion;
}
publicbyte[] getData() {
returndata;
}
publicMap<String, String> getHeaders() {
returnheaders;
}
publicintgetTimeout() {
returntimeout;
}
publicintgetMinReconnectDelay() {
returnminReconnectDelay;
}
publicintgetMaxReconnectDelay() {
returnmaxReconnectDelay;
}
publicintgetMaxServerPingDelay() {
returnmaxServerPingDelay;
}
publicProxygetProxy() {
returnproxy;
}
publicStringgetProxyLogin() {
returnproxyLogin;
}
publicStringgetProxyPassword() {
returnproxyPassword;
}
publicDnsgetDns() {
returnthis.dns;
}
privateOptions(Builderbuilder) {
this.token = builder.token;
this.tokenGetter = builder.tokenGetter;
this.name = builder.name;
this.version = builder.version;
this.data = builder.data;
this.headers = builder.headers;
this.timeout = builder.timeout;
this.minReconnectDelay = builder.minReconnectDelay;
this.maxReconnectDelay = builder.maxReconnectDelay;
this.maxServerPingDelay = builder.maxServerPingDelay;
this.proxy = builder.proxy;
this.proxyLogin = builder.proxyLogin;
this.proxyPassword = builder.proxyPassword;
this.dns = builder.dns;
}
publicstaticclassBuilder {
privateStringtoken = "";
privateConnectionTokenGettertokenGetter;
privateStringname = "java";
privateStringversion = "";
privatebyte[] data;
privateMap<String, String> headers;
privateinttimeout = 5000;
privateintminReconnectDelay = 500;
privateintmaxReconnectDelay = 20000;
privateintmaxServerPingDelay = 10000;
privateProxyproxy;
privateStringproxyLogin;
privateStringproxyPassword;
privateDnsdns;
/** * Set connection token. This is a token you have to receive from your application backend. * If your tokens expire and you want SDK to automatically refresh tokens then set * ConnectionTokenGetter (see below). */publicBuildersetToken(Stringtoken) {
this.token = token;
returnthis;
}
/** * Set a method to extract new connection token upon expiration. */publicBuildersetTokenGetter(ConnectionTokenGettertokenGetter) {
this.tokenGetter = tokenGetter;
returnthis;
}
/** * Set client name - name of this client. This should not be unique per client – it * identifies client application name actually, so name should have a limited * number of possible values. By default this client uses "java" as a name. */publicBuildersetName(Stringname) {
this.name = name;
returnthis;
}
/** * Set client version - version of application. This may be used for observability * on the server (for example in analytics). */publicBuildersetVersion(Stringversion) {
this.version = version;
returnthis;
}
/** * Set custom connection data. This data will be delivered to server in Connect command. * For Centrifugo this may be useful in case of using connect proxy. */publicBuildersetData(byte[] data) {
this.data = data;
returnthis;
}
/** * Set custom headers for WebSocket Upgrade request. */publicBuildersetHeaders(Map<String, String> headers) {
this.headers = headers;
returnthis;
}
/** * Set custom timeout for requests in milliseconds. By default, 5000 is used. */publicBuildersetTimeout(inttimeout) {
this.timeout = timeout;
returnthis;
}
/** * Set minimal time before reconnect attempt in milliseconds. By default, 500 is used. */publicBuildersetMinReconnectDelay(intminReconnectDelay) {
this.minReconnectDelay = minReconnectDelay;
returnthis;
}
/** * Set max time between reconnect attempts in milliseconds. By default, 20000 is used. */publicBuildersetMaxReconnectDelay(intmaxReconnectDelay) {
this.maxReconnectDelay = maxReconnectDelay;
returnthis;
}
/** * Set max time of ping delay from server in milliseconds. By default, 10000 is used. */publicBuildersetMaxServerPingDelay(intmaxServerPingDelay) {
this.maxServerPingDelay = maxServerPingDelay;
returnthis;
}
/** * Set proxy to use. */publicBuildersetProxy(Proxyproxy) {
this.proxy = proxy;
returnthis;
}
/** * Set proxy credentials. */publicBuildersetProxyCredentials(Stringlogin, Stringpassword) {
this.proxyLogin = login;
this.proxyPassword = password;
returnthis;
}
/** * Set custom DNS resolver. */publicBuildersetDns(Dnsdns) {
this.dns = dns;
returnthis;
}
publicOptionsbuild() {
returnnewOptions(this);
}
}
}
Possibly we could have both current (for compatibility) and builder approach. Though the main benefit of Builder is immutability - combining both approaches makes the benefit less obvious (as we could just return this from current setters).
The text was updated successfully, but these errors were encountered:
One more thing to improve is using builder pattern for options:
Code
Possibly we could have both current (for compatibility) and builder approach. Though the main benefit of Builder is immutability - combining both approaches makes the benefit less obvious (as we could just return
this
from current setters).The text was updated successfully, but these errors were encountered: