Python\ interface and normalized design

Source: Internet
Author: User

1 interface

The concept of interface:

The interface in the Java language is a good representation of what the interface means: Ianimal.java

/*

* Java interface is a good representation of the features of the interfaces we analyzed earlier:

* 1) is a set of functions, not a function

* 2) The function of the interface is used for interaction, all functions are public, that is, other objects can be manipulated

* 3) interface defines functions only, but does not involve function implementations

* 4) These functions are related, are animal-related functions, but photosynthesis is not suitable to put into the ianimal inside the * *

Package Com.oo.demo;

Public interface IAnimal {

public void eat ();

public void run ();

public void sleep ();

public void speak ();

}

================= the second part: Pig.java: Pig "class design, realizes the Iannimal interface

Package Com.oo.demo;

public class Pig implements ianimal{//each of the following functions needs to be implemented in detail

public void Eat () {

System.out.println ("Pig like to eat grass");

}

public void Run () {

System.out.println ("Pig run:front legs, back legs");

}

public void sleep () {

System.out.println ("Pig sleep hours every day");

}

public void Speak () {

System.out.println ("Pig can not speak"); }

}

================= Part III: Person2.java

/*

* Realized the IAnimal "people", there are a few points to explain:

* 1) also implemented the IAnimal interface, but "people" and "pig" implementation is different, in order to avoid too much code to affect reading, the code here is simplified to one line, but the output is different, the actual project in the same interface of the same function point, the implementation of the various classes are not exactly the same

* 2) This is also the "person" class, but it is not the same as the class "people" given in the previous class, because the same logic concept, in different scenarios, has the properties and functions are completely dissimilar */

Package Com.oo.demo;

public class Person2 implements IAnimal {

public void Eat () {

System.out.println ("Person like to eat meat");

}

public void Run () {

System.out.println ("Person run:left leg, right leg");

}

public void sleep () {

SYSTEM.OUT.PRINTLN ("person sleep 8 hours every Dat");

}

public void Speak () {

System.out.println ("Hellow World, I am a person");

}

}

================= Part IV: Tester03.java

Package Com.oo.demo;

public class Tester03 {

public static void Main (string[] args) {

System.out.println ("===this is a person===");

IAnimal person = new Person2 ();

Person.eat ();

Person.run ();

Person.sleep ();

Person.speak ();

System.out.println ("\n===this is a pig===");

IAnimal pig = new Pig ();

Pig.eat ();

Pig.run ();

Pig.sleep ();

Pig.speak ();

}

}

There are two uses of inheritance:

One: Inherit the methods of the base class and make your own changes or extensions (code reuse)

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

Class Interface: #定义接口Interface类来模仿接口的概念, there is no Interface keyword in python to define an interface.

def read (self): #定接口函数read

Pass

def write (self): #定义接口函数write

Pass

Class Txt (Interface): #文本, specifically implemented read and write

def read (self):

Print (' Read method of text data ')

def write (self):

Print (' Read method of text data ')

Class Sata (Interface): #磁盘, specifically implemented read and write

def read (self):

Print (' Read method of hard drive data ')

def write (self):

Print (' Read method of hard drive data ')

Class Process (All_file):

def read (self):

Print (' Read method of process data ')

def write (self):

Print (' Read method of process data ')

In practice, the first meaning of inheritance is not very significant, and often harmful. Because it causes the subclass to be strongly coupled to the base class.

The second meaning of inheritance is very important. It is also called "Interface inheritance."

Interface inheritance is essentially a requirement "to make a good abstraction, which prescribes a compatible interface so that the external caller does not have to care about the specifics, and treats all the objects of a particular interface in a non-discriminatory process"--this is called Normalization in program design.

Normalization allows high-level external users to handle the collection of objects that are compatible with all interfaces without distinction-just like the generic file concept of Linux, everything can be treated as a file, regardless of whether it is memory, disk, network, or screen (of course, for the underlying designer, it can also distinguish between "character devices" and " And then make a specific design: to what extent, depending on the requirements.

In Python there is no one is called interface keyword, the above code just looks like interface, in fact, does not play the role of interface, sub-class can not be used to implement the interface, if you want to imitate the concept of the interface, you can use the third-party module.

1.1 Why to use the interface

The interface extracts a group of common functions that can be used as a collection of functions.

Then let the subclass implement the functions in the interface

The significance of this is normalization, what is called normalization, that is, as long as it is based on the same interface implementation of the class, then all of these classes produce objects in use, from the usage of the same.

Normalization, so that users do not have to care about the object's class, only need to know that these objects have certain functions, which greatly reduces the user's use of difficulty.

For example: We define an animal interface, the interface defines a run, eat, breathe and other interface functions, so that the mouse class to achieve the interface, the class of squirrels to implement the interface, from the two to produce a mouse and a squirrel sent to you, even if you are not exactly what the mouse you must know that they will run, Will eat, can breathe.

For example: We have a car interface, which defines all the functions of the car, 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 Volkswagen we will open, There's no need to worry about what kind of car I drive, how it's done (function calls) are all the same

Python\ interface and normalized design

Related Article

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.