jeudi 10 septembre 2020

Java - multiple static nested classes design

I have a service called AService which needs B and C services but BService and CService is only needed inside AService. B & C does not need to access AService so i thought to use static nested classes. CService extends a public library class(APublicLibClass) and overrides a method (methodC). And BService and CService instance gets created inside AService's constructor.

I want to know if this is good from design perspective or there is a better way to do/design it ?

class AService {

    private final BService bService;
    private final CService cService;

    public AService() {
        this.bService = new BService();
        this.cService = new CService(bService);
    }

    private static class BService {
    
        methodA() {}
        methodB() {}
    }
    
    private static class CService extends APublicLibClass {
        BService bService;
        
        
        CService(BService bService) {
            this.bService = bService;
        }
        
        **@Override**
        methodC() {
        
        }
        
    }
    
    aServiceMethod() {
        // use nested classes
        this.bService.methodA();
    }

}

Aucun commentaire:

Enregistrer un commentaire