Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.play_ground.java.refactor_example;

public enum Identifier {
PARTNER_ONE,
PARTNER_TWO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.play_ground.java.refactor_example;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@RequiredArgsConstructor
@Service
public class LegacyService {

private final PartnerOne partnerOne;

private final PartnerTwo partnerTwo;

// ...

// 조회 (예시)
public String retrieve(Map<String, Object> objectMap){ //
// ...
String identifier = (String) objectMap.get("identifier");
switch (identifier){
case "giftiCon" : return partnerTwo.retrieve();
case "giftiShow" : return partnerOne.retrieve();
default: throw new RuntimeException("custom exception");
}
// ...
}

// 취소 (예시)
public String approve(Map<String, Object> objectMap){ //
// ...
String identifier = (String) objectMap.get("identifier");
switch (identifier){
case "giftiCon" : return partnerTwo.cancel(); // 어떤 고객사는 망 취소가 없으므로 여기서 명시적으로 cancel
case "giftiShow" : return partnerOne.netCancel();
default: throw new RuntimeException("custom exception");
}
// ...
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.play_ground.java.refactor_example;

import org.springframework.stereotype.Service;

@Service
public class PartnerOne implements PartnerService {
public String retrieve(){
return "retrive";
}

public String approve(){
return "approve";
}

public String cancel(){
return "cancel";
}

public String netCancel(){
return "netCancel";
}

public Identifier getIdentifier(){
return Identifier.PARTNER_ONE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.play_ground.java.refactor_example;

public interface PartnerService {
String retrieve();
String approve();
String cancel();
String netCancel();
Identifier getIdentifier();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.play_ground.java.refactor_example;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class PartnerServiceFactory {

private final Map<Identifier, PartnerService> partnerServiceBeans = new HashMap<>();

public PartnerServiceFactory(List<PartnerService> partnerServiceList) { // List 형태의 파트너서비스 beans 주입
partnerServiceList.forEach(each -> partnerServiceBeans.put(each.getIdentifier(), each));
}

public PartnerService getPartnerService(Identifier identifier) {
return partnerServiceBeans.get(identifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.play_ground.java.refactor_example;

import org.springframework.stereotype.Service;

@Service
public class PartnerTwo implements PartnerService {

public String retrieve(){
return "retrive";
}

public String approve(){
return "approve";
}

public String cancel(){
return "cancel";
}

public String netCancel(){
return "netCancel";
}

@Override
public Identifier getIdentifier() {
return Identifier.PARTNER_TWO;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.play_ground.java.refactor_example;

public record RequestDto(
Identifier identifier
// etc ...
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.play_ground.java.refactor_example;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RetrieveService {

private final PartnerServiceFactory partnerServiceFactory;

public String retrieve(RequestDto requestDto){
PartnerService partnerService = partnerServiceFactory.getPartnerService(requestDto.identifier());
return partnerService.retrieve();
}
}