There is no interface in Python. How do I write the design pattern?

Source: Internet
Author: User
Recently, I have been studying the design pattern. I am reading the big talk design pattern and the simple design pattern (that is, headfirst). I use C # and JAVA. All have interfaces. (When a class implements an interface, all the methods in the interface must be implemented. Otherwise, an error is reported .) (Well... I know that both C # and Java do not support multi-inheritance. It is supported in Python.) I want to implement these design patterns in Python. Python has no interfaces. How to Implement the design model? What is the method of multi-inheritance? (I don't have to use inte to learn the design model recently. I'm reading the big talk design model and the deep dive into the design model (head first)
One uses C # And the other uses JAVA. All have interfaces.
(When a class implements an interface, all the methods in the interface must be implemented. Otherwise, an error is reported .)
(Well... I know that both C # and Java do not support multiple inheritance, which is supported in Python)
I want to use Python to implement these design patterns.

Python has no interfaces. How to Implement the design model? What is the method of multi-inheritance?
(I don't have to use the interface. I just want to know how to solve it without the interface language .)
(The question has already been searched = I can't find the answer here, rather than reaching out ..)Reply content:

Do not look at the java design mode textbooks and feel that the design mode is the design interface. Obviously, this is not the case at all. The design pattern tells you how to decouple your code in various situations so that your code can be combined at runtime. This is the same as the art of war. Is it useless if you have an airplane, a cannon, or an army?


Of course, Dynamic Language and functional language have their own models. They look different from things written by the masses, because some patterns have already been converted into syntax, while some syntaxes have been converted into patterns. It is hard to figure out the architecture. This is not a good result.


The design pattern is to patch the language, so the same things are different in Java, C #, and C ++. C ++ does not have some good things in python, and some things in C ++ can only be patched if they can write python smoothly. If there are many people who encounter this problem, in this way, a new python-based mode is created.

You have mentioned a lot about the design mode and interface. I won't join. It has always been a topic that is easy to cause saliva. Since I want to use Python to implement these design patterns.
I recommend a great project to you. Someone has already done this, and it is great. When you use python to implement these design patterns, you can refer to others' practices ~
Python-patterns --- A collection of design patterns/idioms in Python
For Python design patterns, refer This book.
I re-examined the more than 20 models from the Python perspective. (However, many of them are not used. The author will explain why they are not used.
It looks like the only one on the market talking about the Python design pattern. http://pypi.python.org/pypi/zope.interface

In addition, I don't understand why the design mode must depend on interfaces? Look at peter norvig's article, Design Patterns in Dynamic ages In a language that supports the first class function, most of the design patterns are gone.
A large part of the reason why the java design pattern is so heavy is that the language's expression ability is too weak. The design pattern itself does not describe the business logic and hides the business logic under one pattern, it's like fighting against this language.

Another article by Mark Dominus, The Universe of Discourse: Design patterns of 1972
I think it is well written. Many Java/c ++ models are not used to solve the recurring problem in a specific situation. A large part of the model is used to make up for the lack of self-expression ability. Dynamic type, first-class function, Macro... can easily implement many modes

Visitor Pattern-> Generic Functions (Multiple Dispatch)
Factory Pattern-> Meta Classes, closures
Singleton Pattern-> Meta Classes
Iterator Pattern-> Anonymous Functions (used with Higher Order Functions)
Mediator, Observer-> Method combination
Facade Pattern-> Modules (Norvig)
...

Misuse of dynamic features, high-order functions, macros... can easily make programs difficult to maintain and may be efficient.
Java allows you to design interfaces more precisely. Of course, many of them are over-designed, and they can be less "bypassed ..

The design mode is to better design interfaces (interfaces between classes, objects, modules, and functions), encapsulate changes, and facilitate expansion.
Languages with advanced features can be implemented more directly. In other words, it is easier to abuse the "design model ".. Use zope. interface

From zope. interface import Interface, implementsclass IMan: def talk (self): "comments" class Geek (): implements (IMan) def talk (self ): print "I know you don't want to make a noise. I know you are taking a nap." or @ implementer (IMan): class Geek (): def talk (self): print "I know you can't make a noise, strange Shu is taking a nap"
Seeing this problem, I suddenly felt that the design pattern was really easy to misunderstand.

In fact, the software design only needs to follow the following principles to design a very elegant structure:
Single Responsibility Principle
Open and closed Principle
Rys replacement principle
Dependency inversion principle
Interface isolation principle

The so-called design patterns are only the product of applying these principles to specific language features. If you know this, you won't stick to the design pattern. You can find two methods to understand the representation (implementation) of interfaces in Python.
1. @ fan Pei's zope. interface is used a lot in twisted.
2. the built-in ABC module in Pyhton. Here some documents can see the http://dev.pocoo.org /~ Mitsuhiko/PythonBasket.pdf Flash used internally
Here is an article comparing Deciphering Glyph: Explaining Why Interfaces Are Great All duck typing programming languages do not require design patterns.

Design Patterns are helpful,
However, it is unnecessary to use Python.

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.