Commit 10acd291 authored by amir.yosef's avatar amir.yosef

make CommandFactory singleton

parent e58fd43b
package command;
import model.Command;
import util.Response;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class InfoCommand implements CommandHandler {
private final Configuration configuration = Configuration.getInstance();
@Override
public void execute(List<String> args, OutputStream os) {
System.out.println(configuration.getInfo());
String command = args.getFirst();
if (command.equalsIgnoreCase(Command.REPLICATION.getValue())) {
Map<String, String> info = configuration.getInfo();
String response = info.entrySet()
.stream()
.map(data -> data.getKey() + ":" + data.getValue())
.collect(Collectors.joining());
try {
os.write(Response.getResponse(response));
os.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
\ No newline at end of file
......@@ -4,19 +4,26 @@ import command.*;
import model.Command;
public class CommandFactory implements Factory {
private final Command command;
public CommandFactory(Command command) {
this.command = command;
private static final class FactoryHolder {
private static final CommandFactory factory = new CommandFactory();
}
public CommandHandler getInstance() {
public CommandFactory() {
}
public static CommandFactory getInstance() {
return FactoryHolder.factory;
}
@Override
public CommandHandler getCommand(Command command) {
return switch (command) {
case PING -> new PingCommand();
case ECHO -> new EchoCommand();
case SET -> new SetCommand();
case GET -> new GetCommand();
case INFO -> new InfoCommand();
// case REPLCONF -> new ReplConfCommand(replicaReceiver);
// case PSYNC -> new FullResyncCommandProcessor(replicaSender);
// case WAIT -> new WaitCommandProcessor(replicaSender, replicaReceiver);
......
......@@ -2,7 +2,8 @@ package factory;
import command.CommandHandler;
import model.Command;
public interface Factory {
CommandHandler getInstance();
CommandHandler getCommand(Command command);
}
......@@ -4,7 +4,6 @@ import command.CommandHandler;
import command.CommandInvoker;
import factory.CommandFactory;
import model.Command;
import parser.CommandParser;
import util.CommandUtil;
import java.io.IOException;
......@@ -14,24 +13,23 @@ import java.util.List;
public class ClientCommandHandler {
private final List<String> commands;
private final OutputStream os;
private final CommandFactory factory = CommandFactory.getInstance();
public ClientCommandHandler(List<String> commands, OutputStream outputStream) {
this.commands = commands;
this.os = outputStream;
CommandParser commandParser = new CommandParser();
}
public boolean execute() {
public void execute() {
Command command = CommandUtil.getCommand(commands.getFirst());
commands.removeFirst();
CommandHandler commandProcessor = new CommandFactory(command).getInstance();
CommandHandler commandProcessor = factory.getCommand(command);
try {
CommandInvoker.invoke(commandProcessor, commands, os);
} catch (IOException e) {
throw new RuntimeException(e);
}
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment