Design Architecture/Refactoring

Separate Query From Modifier

lipnus 2022. 4. 20. 12:06
반응형

매소드가 값도 반환하고 상태도 변화시킨다

->둘로 쪼갬

 

 

before

public class SeparateQueryFromModifier_before {

	static void checkSecurity(String[] people) {
		String found = foundMiscreant(people);
		someLaterCode(found);
	}
	
	static String foundMiscreant(String[] people) {
		for (int i = 0; i < people.length; i++) {
			if (people[i].equals("Don")) {
				sendAlert();
				return "Don";
			}
			if (people[i].equals("John")) {
				sendAlert();
				return "John";
			}
		}
		return "";
	}
	
	static public void sendAlert() {
		System.out.println("alert received ....!");
	}
	
	static public void someLaterCode(String found) {
		System.out.println("someLaterCode is Running - " + found);
	}
	
	public static void main(String[] args) {
		String[] people = {"kitty", "Don", "John", "Smith","David", "Olive" };
		
		checkSecurity(people);
	}

}

 

after

public class SeparateQueryFromModifier_before {

	static void checkSecurity(String[] people) {
		String found = foundMiscreant(people);

		if(!found.equals("")) {
			sendAlert();
			someLaterCode(found);

		}
	}
	
	static String foundMiscreant(String[] people) {
		for (int i = 0; i < people.length; i++) {
			if (people[i].equals("Don")) {
				return "Don";
			}
			if (people[i].equals("John")) {
				return "John";
			}
		}
		return "";
	}

	static public void sendAlert() {
		System.out.println("alert received ....!");
	}
	
	static public void someLaterCode(String found) {
		System.out.println("someLaterCode is Running - " + found);
	}
	
	public static void main(String[] args) {
		String[] people = {"kitty", "Don", "John", "Smith","David", "Olive" };
		
		checkSecurity(people);
	}

}

 

반응형

'Design Architecture > Refactoring' 카테고리의 다른 글

Introduce Parameter Object  (0) 2022.04.20
Replace Parameter with Method Call  (0) 2022.04.20
Replace Nested Conditional With Guard Clauses  (0) 2022.04.20
Decompose Conditional  (0) 2022.04.20
Replace Type Code with Subclass  (0) 2022.04.20