N
N
NoMoneyException2016-12-16 03:30:07
Java
NoMoneyException, 2016-12-16 03:30:07

How to properly develop an interface and a factory?

Hello. There is an interface

public interface AuthorizationService {
    void send(User user) throws IOException;

    Respond getRespond() throws IOException, ClassNotFoundException;

    ObjectOutputStream getOut();

    void setOut(ObjectOutputStream out);

    ObjectInputStream getIn();

   void setIn(ObjectInputStream in);
}

It is implemented by the class
package com.eleshchinskiy.service.impl;

import com.eleshchinskiy.service.AuthorizationService;
import com.eleshchinskiy.service.bean.Respond;
import com.eleshchinskiy.service.bean.real.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class AuthorizationServiceImpl implements AuthorizationService {
    private ObjectOutputStream out;
    private ObjectInputStream in;

    public AuthorizationServiceImpl(){
    }

    @Override
    public void send(User user) throws IOException {
        out.writeObject(user);
        out.flush();
    }

    @Override
    public Respond getRespond() throws IOException, ClassNotFoundException {
        return (Respond) in.readObject();
    }

    @Override
    public ObjectOutputStream getOut() {
        return out;
    }

    @Override
    public void setOut(ObjectOutputStream out) {
        this.out = out;
    }

    @Override
    public ObjectInputStream getIn() {
        return in;
    }

    @Override
    public void setIn(ObjectInputStream in) {
        this.in = in;
    }
}

This object can be obtained from the factory
package com.eleshchinskiy.service.factory;

import com.eleshchinskiy.service.AdminMenuService;
import com.eleshchinskiy.service.AuthorizationService;
import com.eleshchinskiy.service.UserMenuService;
import com.eleshchinskiy.service.impl.AdminMenuServiceImpl;
import com.eleshchinskiy.service.impl.AuthorizationServiceImpl;
import com.eleshchinskiy.service.impl.UserMenuServiceImpl;

public class ServiceFactory {
    private static final ServiceFactory instance = new ServiceFactory();

    private AuthorizationService authorizationService = new AuthorizationServiceImpl();
    private UserMenuService userMenuService = new UserMenuServiceImpl();
    private AdminMenuService adminMenuService = new AdminMenuServiceImpl();

    private ServiceFactory() {
    }

    public AuthorizationService getAuthorizationService() {
        return authorizationService;
    }

    public UserMenuService getUserMenuService() {
        return userMenuService;
    }

    public AdminMenuService getAdminMenuService() {
        return adminMenuService;
    }

    public static ServiceFactory getInstance() {
        return instance;
    }
}

Everything seems to be fine, but getters and setters cannot be written in the interface. However, if I don't do this, then when I get an interface type object, I won't be able to set/get its fields. How is it solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AtomKrieg, 2016-12-16
@AtomKrieg

The factory looks like this.

public interface Service {}
public class AuthorizationService implements Service {}
public class UserMenuService implements Service {}
public class AdminMenuService implements Service {}

public class ServiceFactory {
    public static final int AUTHORIZATION = 1;
    public static final int USERMENU = 2;
    public static final int ADMINMENU = 3;

    public static Service create(int id) throws Exception {
        switch (id) {
            case AUTHORIZATION: return new AuthorizationService();
            case USERMENU: return new UserMenuService();
            case ADMINMENU: return new AdminMenuService();
            default: throw new Exception("service factory wrong id:" + id);
        }
    }
}

Then you work on the general methods declared in the interface. When specific implementation methods are needed, instanceof is used. If instanceof is used too often, then the interface and factory are unnecessary abstractions.
Maybe you need a builder, or maybe you need an abstract factory, or maybe 3 separate factories.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question