eajni IT μ΄ˆλ³΄μ‚¬μ „ πŸ’¦πŸ’¦

(DesignPattern) Strategy


*content

λ‹€μ–‘ν•œ μ•Œκ³ λ¦¬μ¦˜μ„ 각각의 클래슀둜 μΊ‘μŠν™”ν•˜μ—¬ ν•˜λ‚˜μ˜ 좔상적인 접근점에 μœ„μž„, ν΄λΌμ΄μ–ΈνŠΈκ°€ μ μ ˆν•œ μ „λž΅μ„ 취사 μ„ νƒν•˜μ—¬ λ‘œμ§μ„ μˆ˜ν–‰ν•  수 μžˆλŠ” νŒ¨ν„΄

Tools

public interface Tools{
	public void write();
}

Pencil

public class Pencil implements Tools{
	@Override
	public void write(){
		System.out.println("By pencil");
	}
}

BallpointPen

public class BallpointPen implements Tools{
	@Override
	public void write(){
		System.out.println("By BallpointPen");
	}
}

FountainPen

public class FountainPen implements Tools{
	@Override
	public void write(){
		System.out.println("By FountainPen");
	}
}

Student

public class Student{
	private Tools tools;

	public void change(Tools tools){
		this.tools = tools;
	}

	public void write(){
		if(tools == null){
			System.out.println("without tools");
		}else{
			tools.write();
		}
	}

}

Main

public class main{
	publi static void main(String[] args){
		Student student = new Student();
		student.write();   //without tools

		student.change(new Pencil());
		student.write();   //by pencil

		student.change(new BallpointPen());
		student.write();   //by ballpointpen
	}
}

μž₯점

  • ν™•μž₯μ„±

Comments

Content