https://www.youtube.com/watch?v=DYmtue0k1cc&list=PLDV-cCQnUlIZcWXE4PrxJx6U3qKfRTJcK&index=6

Single Responsibility principle

class Cat:
  def __init__(self,age,name):
    self.age = age
    self.name = name
  
  def eat(self):
    print("eating..")

  def walk(self):
    print("walking..")

  def speak(self):
    print("meow~")
  
  def repr(self):
    return f"name:{self.name}, age:{self.age}"
  
kitty = Cat(3,"kitty")
kitty.eat()
kitty.walk()
kitty.speak()
print(kitty.repr())
#Logger.log(kitty.repr()) ,  If you have the logger object
eating..
walking..
meow~
name:kitty, age:3

Open closed principle

not Open closed principle

Cow와 Sheep을 추가하기위해 hey함수의 수정이 필요하다.

class Animal():
  def __init__(self,type):
    self.type = type


def hey(animal):
  if animal.type == 'Cat':
    print('meow')
  elif animal.type == 'Dog':
    print('bark')

bingo = Animal('Dog')
kitty = Animal('Cat')

hey(bingo)
hey(kitty)
bark
meow

use Open closed principle

상속을 이용한 Animal class. 추가되는 동물에 대해 hey함수의 수정을 필요로 하지 않는다

class Animal: 
  def speak(self):  #interface method
    pass

class Cat(Animal):
  def speak(self):
    print("meow")

class Dog(Animal):
  def speak(self):
    print("bark")

class Sheep(Animal):
  def speak(self):
    print("meh")

class Cow(Animal):
  def speak(self):
    print("moo")

def hey(animal):
  animal.speak();


bingo = Dog()
kitty = Cat()
sheep = Sheep()
cow = Cow()

hey(bingo)
hey(kitty)
hey(sheep)
hey(cow)
bark
meow
meh
moo

python/javascript같은 언어에서는 Open closed principle을 위해 꼭 상속 개념을 필요로 하지는 않음. (Animal class가 없어도 됨)

class Cat():
  def speak(self):
    print("meow")

class Dog():
  def speak(self):
    print("bark")

def hey(animal):
  animal.speak();

bingo = Dog()
kitty = Cat()

hey(bingo)
hey(kitty)
bark
meow

Liskov Substitution principle

class Cat:
  def speak(self):
    print("meow")

class BlackCat(Cat):
  def speak(self):
    print("black meow")

def speak(cat:Cat):
  cat.speak()


cat = Cat()
speak(cat)
meow
cat = BlackCat()
speak(cat)
black meow
class Fish(Cat):
  def speak(self):
    raise Exception("Fish cannot speak")

cat = Fish()
speak(cat)
error

Interface segregation

//No Interface Segregation Principle

//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
	}
}

Dependency inversion

Traditional Pattern

class Cat:
  def speak(self):
    print("meow")

class Dog:
  def speak(self):
    print("bark")


#Zoo depdns on Cat and Dog
class Zoo:
  def __init__(self):
    self.dog = Dog()
    self.cat = Cat()

  def speakAll(self):
    self.cat.speak()
    self.dog.speak()


zoo = Zoo()
zoo.speakAll()
meow
bark

Dependency Inversion Pattern

class Animal: #abstract module
  def speak(self): #interface method
    pass

class Cat(Animal):
  def speak(self):
    print("meow")

class Dog(Animal):
  def speak(self):
    print("bark")


#Zoo depends on Animal.   (Not Cat, Not Dog)
class Zoo:
  def __init__(self):
    self.animals = []

  def addAnimal(self,animal):
    self.animals.append(animal)
  
  def speakAll(self):
    for animal in self.animals:
      animal.speak()

zoo = Zoo()
zoo.addAnimal(Cat())
zoo.addAnimal(Dog())
zoo.speakAll()
              
meow
bark
profile
컨닝

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN