Dev./객체지향

Interface segregation principle(인터페이스 분리 원칙)

hotpotato0 2022. 3. 29. 21:16

인터페이스 분리 원칙은 클라이언트가 자신이 이용하지 않는 메서드에 의존하지 않아야 한다는 원칙이다.[1] 인터페이스 분리 원칙은 큰 덩어리의 인터페이스들을 구체적이고 작은 단위들로 분리시킴으로써 클라이언트들이 꼭 필요한 메서드들만 이용할 수 있게 한다. 이와 같은 작은 단위들을 역할 인터페이스라고도 부른다.

 

//Large Interface
Interface ICarBoatInterface
{
	void drive();
	void turnLeft();
	void turnRight();
	void steer();
	void steerLeft();
	void steerRight();
}



//Interface Segregation Principle
//two small interfaces (Car, Boat)
Interface ICarInterface
{
	void drive();
	void turnLeft();
	void turnRight();
}

Interface IBoatInterface
{
	void steer();
	void steerLeft();
	void steerRight();
}


class Avante : ICarInterface
{
	public void drive()
	{
		//implemenetation
	}
	public void turnLeft()
	{
		//implmementation
	}
	public void turnRight()
	{
		//implementation
	}
}


class CarBoat :ICarInterface , IBoatInterface
{
	public void drive()
	{
		//implemenetation
	}
	public void turnLeft()
	{
		//implmementation
	}
	public void turnRight()
	{
		//implementation
	}
	public void steer()
	{
		//implemenetation
	}
	public void steerLeft()
	{
		//implmementation
	}
	public void steerRight()
	{
		//implementation
	}
}