Answer the question
In order to leave comments, you need to log in
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);
}
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;
}
}
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;
}
}
Answer the question
In order to leave comments, you need to log in
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);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question