redoom opened a new pull request, #15767:
URL: https://github.com/apache/dubbo/pull/15767

   
   
   ## Motivation
   
   https://github.com/apache/dubbo/issues/15600#event-20779028745
   
   ## The Problem
   
   ```
   public abstract class AbstractServer {
       public AbstractServer(URL url, ChannelHandler handler) {
           super(url, handler);
           doOpen();
       }
       protected abstract void doOpen() throws Throwable;
   }
   
   public class NettyServer extends AbstractServer {
       private ServerBootstrap bootstrap;  // Still null when doOpen() called
   
       @Override
       protected void doOpen() {
           bootstrap.group(...);
       }
   }
   ```
   
   
   
   ## Solution
   
   Two-phase initialization with static factory methods:
   
   ```
   // ✅ After: Separate construction from initialization
   protected AbstractServer(URL url, ChannelHandler handler) {
       super(url, handler);
       // Only field initialization
   }
   
   protected final void init() throws RemotingException {
       doOpen();  // object fully constructed
   }
   
   public static NettyServer create(URL url, ChannelHandler handler) {
       NettyServer server = new NettyServer(url, handler);
       server.init();
       return server;
   }
   
   private NettyServer(URL url, ChannelHandler handler) {
       super(url, ChannelHandlers.wrap(handler, url));
   }
   ```
   
   ## Changes
   
   - Split initialization: construction (fields only) + init() (resources)
   - Private constructors + static factory methods
   - Final init() methods prevent subclass override
   
   
   ```
   // ✅
   NettyServer server = NettyServer.create(url, handler);
   
   // ❌
   // NettyServer server = new NettyServer(url, handler);
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to