poltfu.blogg.se

Interface segregation principle
Interface segregation principle





  1. #Interface segregation principle how to#
  2. #Interface segregation principle software#
  3. #Interface segregation principle code#
  4. #Interface segregation principle tv#

Here is what each letter in the acronym stands for: Each letter in the acronym SOLID stands for a specific principle.

#Interface segregation principle software#

Clients should not be forced to implement interfaces they do not use.The SOLID design principles help us create maintainable, reusable, and flexible software designs. Make fine grained interfaces that are client-specific.The interface segregation principle advises that the interfaces should be small in terms of cohesions.It doesn’t need to implement the fly() method that it doesn’t use. In this design, the Car only need to implement the go() method that it needs.

#Interface segregation principle code#

Print( "Going") Code language: Python ( python ) Third, inherit the Movable class from the Car class: class Car (Movable): def go (self): Second, inherits from the Flyable class from the Aircraft class: class Aircraft (Flyable): def go (self): To fix this, you need to split the Vehicle class into small ones and inherits from these classes from the Aircraft and Car classes:įirst, split the Vehicle interface into two smaller interfaces: Movable and Flyable, and inherits the Movable class from the Flyable class: class Movable (ABC): def go (self): pass class Flyable (Movable): def fly (self): pass Code language: Python ( python ) Therefore, this design violates the interface segregation principle. In this design the Car class must implement the fly() method from the Vehicle class that the Car class doesn’t use. Since a car cannot fly, we raise an exception in the fly() method: class Car (Vehicle): def go (self):ĭef fly (self): raise Exception( 'The car cannot fly') Code language: Python ( python ) Third, define the Car class that inherits from the Vehicle class. Print( "Flying") Code language: Python ( python ) Second, define the Aircraft class that inherits from the Vehicle class and implement both go() and fly() methods: class Aircraft (Vehicle): def go (self): Clients should not be forced to implement interfaces they do not use.” Interface segregation principle exampleįirst, define a Vehicle abstract class that has two abstract methods, go() and fly(): from abc import ABC, abstractmethodĬlass Vehicle (ABC): def go (self): pass def fly (self): pass Code language: Python ( python ) Uncle Bob, who is the originator of the SOLID term, explains the interface segregation principle by advising that “Make fine-grained interfaces that are client-specific. If the Database interface doesn’t use both methods, it’ll be incomplete. An interface can have multiple cohesive methods.įor example, the Database interface can have the connect() and disconnect() methods because they must go together. It doesn’t mean that the interface should have one method. The interface segregation principle states that an interface should be as small a possible in terms of cohesion. The duck typing principle states that “if it walks like a duck and quacks like a duck, it must be a duck.” In other words, the methods of a class determine what its objects will be, not the type of the class. Python uses abstract classes as interfaces because it follows the so-called duck typing principle.

interface segregation principle

The purpose of interfaces is to allow clients to request the correct methods of an object via its interface. In object-oriented programming, an interface is a set of methods an object must-have.

#Interface segregation principle tv#

For example, when you press the power button on the TV remote control, it turns the TV on, and you don’t need to care how. The interface segregation principle is one of five SOLID principles in object-oriented programming:Īn interface is a description of behaviors that an object can do. Introduction to the interface segregation principle

#Interface segregation principle how to#

Summary: in this tutorial, you’ll learn about the interface segregation principle and how to apply it in Python.







Interface segregation principle