Python Interfaces and abstract classes

Source: Internet
Author: User

first, interface and normalization of design

1. What is an interface

  1) is a set of feature sets

2) The function of the interface is used for interactive

3) The interface only defines functions, but does not involve the implementation of functions

  4) These functions are relevant

2, why to use the interface

The interface extracts a group of common functions, and then allows subclasses to implement the functions in that interface, that is, the interface can be used as a collection of functions.

The point of doing this is normalization, and normalization is that as long as the classes are implemented based on the same interface, all of these classes produce objects that are used in the same way.

Benefits of Normalization:

1, normalization so that users do not care about the object of the class is what, only need to know that these objects have certain functions on the line, greatly reducing the user's difficulty.

2. Normalization allows the external user of the upper layer to handle the collection of objects that are compatible with all interfaces without distinction

2.1: Just like the pan-file concept of Linux, everything can be processed as a file, without concern whether it is memory, disk, network or screen (of course, for the underlying designers, of course, can also distinguish between "character devices" and "block devices", and then make a targeted design: to what extent, depending on the needs and In the same setting).

2.2: Another example: We have a car interface, which defines the car all the functions, and then by the Honda Car class, Audi car class, Volkswagen class, they all realize the car interface, so it is good to do, we just have to learn how to drive a car, then whether it is Honda, Audi, or VW We're all going to drive, we don't have to worry about what kind of car I drive, how it's done (function calls) are the same

  3. Imitation interface

There is no interface keyword in Python, and third-party modules are available if you want to mimic the concept of an interface:

    Http://pypi.python.org/pypi/zope.interface

Twisted in the twisted\internet\interface.py, use Zope.interface.

Document https://zopeinterface.readthedocs.io/en/latest/

Design mode: Https://github.com/faif/python-patterns

You can also use inheritance for two purposes:

One: Inheriting the method of the base class and making its own changes or extensions (code reuse): In practice, this use of inheritance is not very significant, and often harmful. Because it causes the subclass to be strongly coupled to the base class.

Two: Declare a subclass is compatible with a base class, define an interface class (imitate Java interface), the interface class defines some interface name (that is, the function name) and does not implement the functions of the interface, subclass inherits the interface class, and implements the function in the interface

classInterface:#defining an interface interface class to mimic the concept of an interface, there is no interface keyword in Python to define an interface.     defRead (self):#Fixed interface function Read        Pass    defWrite (self):#defining an interface function write        PassclassTXT (Interface):#text that implements read and write specifically    defRead (self):Print('How to read text data')    defWrite (self):Print('How to read text data')classSata (Interface):#disk, specifically implemented read and write    defRead (self):Print('How to read hard disk data')    defWrite (self):Print('How to read hard disk data')classProcess (Interface):defRead (self):Print('method of reading process data')    defWrite (self):Print('method of reading process data')

   The above code just looks like interface, actually does not play the role of interface, subclass can not implement interface completely, this uses the abstract class

first, abstract class

1 What abstract class

    An abstract class is a special class that can only be inherited and cannot be instantiated.

  2 Why abstract classes are needed

If a class extracts the same content from a bunch of objects, the abstract class extracts the same content from a bunch of classes, including data properties and function properties.

For example, we have banana class, Apple class, Peach class, from these classes to extract the same content is the abstract fruit of the class, you eat fruit, either to eat a specific banana, or to eat a specific peach ... You can never eat something called a fruit.

From a design perspective, if a class is abstracted from a real object, the abstract class is based on class abstraction.

From an implementation perspective, abstract classes differ from ordinary classes in that abstract classes can only have abstract methods (without implementing functionality), that classes cannot be instantiated, can only be inherited, and subclasses must implement abstract methods. This is a bit similar to the interface, but it's actually different,

3 Implementing abstract classes in Python

#_ *_coding:utf-8_*_# All documents import ABC #利用abc模块实现抽象类class all_file (metaclass=abc. Abcmeta): all_type= ' file ' @abc. Abstractmethod #定义抽象方法, no function def read (self): ' subclass must define read function ' Pass @a Bc.abstractmethod #定义抽象方法, no function def write required: ' Subclass must define write function ' pass# class TXT (all_file): # pass## T1=t XT () #报错, subclass does not define abstract method class Txt (All_file): #子类继承抽象类, but must define the read and write Methods def read (self): print (' Read method of text data ') def WR ITE (self): print (' Read method of text data ') class Sata (All_file): #子类继承抽象类, but must define the read and write Methods def read (self): print (' Number of drives Read method ') def write: print (' Read method of hard disk data ') class Process (All_file): #子类继承抽象类, but the read and write Method def read (SEL) must be defined f): Print (' Read method of Process data ') def write (self): print (' Read method of process data ') Wenbenwenjian=txt () Yingpanwenjian=sata () Jinchen Gwenjian=process () #这样大家都是被归一化了, the thought of all Documents Wenbenwenjian.read () Yingpanwenjian.write () Jinchengwenjian.read () Print (wenbenwenjian.all_type) print (yingpanwenjian.all_type) print (Jinchengwenjian.all_type) 

4. Abstract classes and interfaces

The essence of an abstract class is also a class, which refers to the similarity of a set of classes, including data attributes (such as All_type) and function properties (such as read, write), while the interface only emphasizes the similarity of function properties.

An abstract class is a concept between a class and an interface, with some features of classes and interfaces that can be used to implement a normalized design

Python Interfaces and abstract classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.