: A class should have one reason to change, just its single responsibility.
Player 스크립트를 Player, PlayerSound, PlayerInput, PlayerMovement로 분리, Player에서 하위 컴포넌트를 컨트롤
: A classes must be open for extension but closed for modification.
public class AreaCalculator
{
public float GetRectangleArea(Rectangle rectangle)
{
return rectangle.width * rectangle.height;
}
public float GetCircleArea(Circle circle)
{
return circle.radius * circle.radius * Mathf.PI;
}
}
public class Rectangle
{
public float width;
public float height;
}
public class Circle
{
public float radius;
}
public abstract class Shape
{
public abstract float CalculateArea();
}
public class Rectangle : Shape
{
public float width;
public float height;
public override float CalculateArea()
{
return width * height;
}
}
public class Circle : Shape
{
public float radius;
public override float CalculateArea()
{
return radius * radius * Mathf.PI;
}
}
public class AreaCalculator
{
public float GetArea(Shape shape)
{
return shape.CalculateArea();
}
}
: A derived classes must be substitutable for their base class.
This way of thinking can be counterintuitive because you have certain assumptions about the real world . In software development, this is called the circle–ellipse problem . Not every actual “is a” relationship translates into inheritance . Remember, you want your software design to drive your class hierarchy, not your prior knowledge of reality .
: No Clientr should be forced to depend on methos it does not use.
: High-level modules should not import anything directly from low-level modules
public class Switch : MonoBehavior{
public Door door;
public bool isActived;
public void Toggle(){
if(isActived){
isActived = false;
door.Close();
}
else{
isActived = true;
door.Open();
}
}
}
public class Door : MonoBehavior{
public void Open(){}
public void Close(){}
}
public interface ISwitchable{
public bool IsActive { get; }
public void Active();
public void Deactive();
}
// 스위치 클래스
public class Switch : MonoBehavior{
public ISwitchable client;
public void Toggle(){
if(client.IsActive){
client.Deactivate();
}
else{
client.Activate();
}
}
}
// 문 클래스
public class Door : MonoBehavior, ISwitchable{
private bool isActive;
private bool IsActive => isActive;
public void Activate(){
isActive = true;
}
public void Deactivate(){
isActive = false;
}
}