Recently Learning design patterns, is looking at "Big Talk design Mode" and "in-depth design mode" (that is, head first)
One is used in C #, one in Java. All have interfaces.
(When a class implements an interface, it must implement all the methods in the interface.) Otherwise you will get an error. )
(Hmm.. I know that both C # and Java do not support multiple inheritance, Python support)
I want to use Python to implement these design patterns again.
There is no interface in Python. How do I implement design patterns? In the form of multiple inheritance?
(I don't have to use interface.) I just want to know how to solve it without the interface language. )
(the problem has been searched = = Can't find the answer to come here, not to reach.) )
Reply content:
Do not look at the Java design pattern textbooks think that the design pattern is designed interface. This is obviously not the case at all. Design patterns There are so many things that are telling you how to decouple your code in various situations, so that your code can be combined with each other at run time. This is the same as the art of war. Is it useless to have the art of war on a plane?
Of course, dynamic type languages and functional languages have their own patterns, and they grow to be different from what gang of four writes, because some patterns have been translated into grammar, and some have become patterns. Make the architecture remember mechanically, this is not good results.
The design pattern is to patch the language, so the same thing is different in Java, C # and C + +. C + + Some very good things python does not, some things C + + can be fluent in writing Python can only be patched, if there are many people encounter this problem, so that a new Python-oriented mode
About design patterns and interfaces, the upstairs guys said a lot. I will not participating. has always been a topic of saliva. Since the landlord
want to use Python to implement these design patterns again.
I recommend a great project to you, already someone has done this, and do a great job, you can use Python to implement these design patterns jams, you may refer to other people's practice ~
python-patterns
---A collection of design patterns/idioms in Python
Python design pattern problems can be referenced in The book.
The more than 20 patterns were revisited from a Python perspective (but many do not, and the author explains why
It seems that this is the only one of the python design patterns on the market. / http Pypi.python.org/pypi/zo Pe.interface
In addition I do not understand why design patterns must rely on interface? Look at Peter Norvig's article, Design Patterns in Dynamic Languages
, in a language that supports first class function, most of the design patterns are gone.
The Java design pattern is so heavy, a large part of the reason is because the language is too weak to express, the design pattern itself is not to describe the business logic, the business logic is hidden in a pattern, it feels like in the struggle with this language.
Share a piece of Mark Dominus's article The Universe of discourse:design patterns of 1972
I think it is very good, many patterns in java/c++ are not used to solve recurring problem in a given situation. A large part of the pattern is used to compensate for the lack of language's ability to express itself. Dynamic type, first-class function, macro ... It's easy to implement many patterns
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, combination
Facade Pattern-Modules (Norvig)
...
Misuse of dynamic properties, high-order functions, macros, and so on, can easily cause the program to be difficult to maintain and may have an efficiency trap.
Java allows you to do the interface design more carefully, of course, there are many are over-design, and originally can not so "around".
Design patterns are designed to better interface design (classes, objects, modules, functions between the interface), packaging changes, easy to expand.
Languages with advanced features are more straightforward to implement. In other words, it is also easier to abuse the "design pattern". Using Zope.interface
from Zope.interface Import Interface, Implementsclass IMan: def Talk( Self): "" " Comments " ""class Geek(): Implements(IMan) def Talk( Self): Print "Cicada Cicada You don't quarrel, blame sorghum is taking a nap"or@implementer(IMan):class Geek(): def Talk( Self): Print "Cicada Cicada You don't quarrel, blame sorghum is taking a nap"
Seeing this problem, I suddenly felt that design patterns were something that could easily be misunderstood.
In fact, software design only needs to follow the principle, you can design a very elegant structure:
Single principle of responsibility
Open Closure principle
The principle of the Richter replacement
Dependency Inversion principle
Interface Isolation principle
The so-called design pattern, in fact, is simply a product of applying these principles to specific language features. Knowing this, you will not be obsessed with designing patterns. There are two ways to know the representation (implementation) of an interface in Python.
1. @ Fan Pei said Zope.interface This is used much in twisted
2. The ABC module is built into the Pyhton. Here are some documents to look at / http Dev.pocoo.org/~mitsuhik o/pythonbasket.pdf
Flash internal use of
Then here's an article comparing deciphering Glyph:: Explaining why Interfaces is great
No design patterns are required for all duck typing programming languages.
"Design mode" You see, it helps.
But it's not necessary to use it in Python.