Delphi object-oriented programming ideas
Liu Yizhu
Preface
The first time I knew Delphi was not because of Borland's Delphi software, but because I read the word in Plato's classic work "Plato's Dialogues".
A friend of mine . . . went to
Delphi is where the oracle, the god of prophecy, lives. "Plato's Dialogues" records that a friend of Socrates went to Delphi to ask Pythian, the goddess of prophecy, who was the wisest person. The Pythians said that no one was wiser than Socrates. Socrates was deeply puzzled because he found himself surrounded by politicians, poets, philosophers, and artists. Aren’t these “experts” and “authorities” wiser? I am much worse than them.
Socrates visited these "experts" and "authorities" one by one, but found that they were often self-righteous, deceiving themselves and pretending to know things they did not understand. Through the visit, Socrates also discovered that he knew very little in many aspects. But Socrates did not pretend to understand, he confessed his ignorance. This is what Pythian, the goddess of prophecy, calls true "wisdom."
In fact, this kind of wisdom was not first proposed by the ancient Greeks. Long before Socrates, our country’s Lao Tzu had already summed it up. In the Tao Te Ching, it is said that "he who knows others is wise, and he who knows himself is wise." This is what it means.
Now, the Delphi we use is already an excellent programming language and software development tool. However, facing the profound and constantly developing Delphi, we still know very little about many aspects. But when learning and using Delphi, do we also have Socrates' self-knowledge attitude and the wisdom of knowing everything?
There are many friends who choose Delphi. The initial thought may be because Delphi is powerful, easy to learn and use. They even claimed to be proficient in Delphi in three months, and dared to develop software independently in half a year. In fact, all they can do is drag and drop controls. When they are intoxicated by their own programs, they are actually intoxicated by Delphi's exquisite wisdom and other people's controls. This kind of Delphi programmer is often nicknamed "procrastinator" by others. But they seem to regard themselves as Delphi masters.
There are also many friends who have given up on Delphi. The initial thought may be because they think Delphi is just a RAD tool similar to VB. In their view, Delphi is just control programming and cannot truly implement OOP like C++ or Java. They doubt whether Delphi has the characteristics of an object-oriented programming language and whether it can implement object-oriented technologies such as polymorphism and patterns. In their eyes, only those who use C++ or Java are real masters.
So, what is a real master, and how can one become one?
First, let us take a look at what a martial arts master is, maybe we can get inspiration from it.
Anyone who loves martial arts or martial arts novels knows that there are two ways to practice martial arts: external training and internal training. External training of fists, kicks and weapons, the quality of fists and kicks and weapons are the key; internal training of Qi, training of refining Qi, training of spirit and returning weakness is the foundation. The biggest difference between these two cultivation methods lies in the functional relationship between time and skill, as shown in Figure 1. In layman's terms, in the first three years, those who practice external skills can easily defeat those who practice internal skills; in the 10th year, the two sides can only draw a tie; after 15 years, no matter how you practice external skills, you will not be able to compete with those who practice internal skills. 20 years later, the master of internal skills is invincible in the world. The reason lies in the dialectical relationship between "Qi" and "Qi".
"Weapons" are tangible objects, and swords, guns, swords and halberds are all tangible weapons. Practicing martial arts outside is inseparable from these tangible weapons. Practicing martial arts is training weapons. Therefore, martial arts masters who use guns and swords often rely on the quality of the weapons.
"Qi" is an invisible substance, and all wisdom, magic power, and thoughts are all invisible substances. Internal Qigong cultivation is inseparable from these intangible qualities. Practicing Qigong is Qi training. Therefore, true martial arts masters often do not need to rely on specific weapons. For them, any tangible object can be transformed into a weapon according to the spirit. A folding fan or a fly whisk in his hand is as powerful as a famous spear or sword, which is due to gaining Qi.
Figure 1 Time and skill function diagrams of two cultivation methods
In software development, programming tools are "instruments" and programming ideas are "qi".
Among programming tools, easy-to-use tools such as VB and PB; difficult-to-use tools such as assembly and C++; ancient tools such as FORTRAN and COBOL; and fashionable tools such as Java and C#.
In programming thinking, there are object-oriented and process-oriented, which are both worldview and methodology. The former reflects the way people think about objective objects, and the latter reflects the way machines think about instructions. In the continuous practice of software development, the superiority of the former has been continuously reflected and confirmed.
Mastering object-oriented programming ideas is like gaining the true meaning of Qi training. Its importance often trumps the choice of programming language!
Even if someone chooses object-oriented tools, they still cannot become a real master. Because what he values is the quality of the "qi" and ignores the cultivation of "qi".
In fact, "It doesn't matter whether a system or language is object-oriented. What matters is how it can be object-oriented and what methods are used to achieve related benefits." ("Object-oriented Method: Principles and Practice" Machinery Industry Press, March 2003)
Although it is easy to practice weapons, it is difficult to become a master. Although Qi training is good, the results are slow and the loneliness is unbearable. It is not possible for ordinary people to clearly understand one's mind and take the right path. Therefore, many martial arts masters practice weapons first and then Qi. They practice both internally and externally to achieve success.
For newcomers to the martial arts world, they need to use the power of weapons to make up for their lack of internal strength. The quality of the weapons is often very important. But as martial arts increases and internal strength develops, dependence on weapons should decrease. "The Legend of the Condor Heroes" Yang Guo practices swordsmanship. When he first learns, he likes to use sharp swords. After he becomes proficient in swordsmanship, he likes to use blunt swords. After he masters his internal skills, tree branches can also be used as swords. So for real masters, the quality of the sword often doesn't matter.
Similarly, the growth of software masters also goes through this process. Beginners to programming need to choose a good language, which can achieve twice the result with half the effort, while stimulating interest and enhancing confidence. Once you become familiar with a language, you should use it as an opportunity to master object-oriented programming ideas. At this time, what you are familiar with is no longer the grammar, functions, and class libraries of the language itself, but thinking methods such as binding, polymorphism, patterns, etc., and then by analogy, it is not difficult to learn other object-oriented languages. Real software masters who work hard on their internal skills, have the courage to practice, and ultimately succeed are not limited by programming languages. They may be familiar with a development tool, but it is just a tool that carries their invisible spirit. They are open-minded, eager to learn, and good at summarizing. Their ideas, methods, models and even philosophies not only transcend programming languages, but can also guide the practice of programming languages.
Delphi provides a shortcut for the growth of software masters both internally and externally. When learning and practicing Delphi, you can use its advantages of RAD and control to quickly defeat your opponents when you are just starting out; when you learn and practice Delphi, you can also use its OOP capabilities, the strength of VCL, and the strength of bacon to achieve invincible success.
The world-famous Huashan Sword Sect was once divided into "Sword Sect" and "Qi Sect". The former only practiced weapons and taught moves; the latter also practiced Qi and rebuilt the foundation.
Learning Delphi is like practicing Huashan Swordsmanship. Taking the RAD path is "Sword Master", and starting from OOP is "Air Master". The former likes to use controls and is interested in strange techniques and tricks; the latter is fond of objects and is keen on method patterns. The former pursues quick results, while the latter has high aspirations.
I think that friends who choose Delphi for RAD or give up Delphi because of OOP do not really understand Delphi. Delphi is a good RAD development software, but it is difficult to become a real master without learning OOP and going deep into VCL. Similarly, Delphi is an authentic OOP programming tool. Combined with Delphi's powerful RAD and efficient compiler, it can have more advantages and higher efficiency than other OOP languages. If we can break away from sectarian opinions, practice "qi" and "qi" together, and practice both internal and external skills, I believe it will not be difficult for a Delphi programmer to grow from a RAD quickie to an OOP master, and eventually be proud of the world and dominate the world.
Earlier I briefly discussed the dialectical relationship between "qi" and "qi". In programming, one of the keys to cultivating and improving internal strength is to master the idea of object-oriented programming. In fact, I think that the idea of object-oriented programming is the most in line with the beauty of the invisible truth of the road.
Why do you say that? "The great transformation of ancient times is born with the invisible" (the second reaction in "Guiguzi"). The mystery of Qi first lies in its "great transformation". The great transformation is the great creation of heaven and earth, which gathers all the power of creation and change. Object-oriented programming thinking has this quality.
Laozi said, "The nameless beginning of heaven and earth, the nameless mother of all things." Nameless means indescribable and undefinable. The most mysterious thing about the so-called object-oriented way of thinking is how to identify and define objects from "unnamed" and how to construct and use objects from "named".
For software developers, the process of understanding objective entities and analyzing and designing user needs is the process of discovering and defining objects, and it is a process from nameless to famous. However, the objects here are different from variables or functions in process-oriented processes. Objects are created by classes. Classes are abstract concepts, definable "names", and are the mother of objects.
As a result, Tai Chi gives birth to Liang Yi, and Liang Yi gives birth to Bagua. Through the inheritance and derivation of classes, all things are born and systems are formed.
Even the "tool" as an object-oriented programming tool also embodies and carries the "qi" of object-oriented programming ideas.
The first sentence in the opening chapter of "Zhou Yi Shen Tong Qi", a classic work of internal energy training known as the "King of the Eternal Alchemy Sutra", is "Qian and Kun are the gates of Yi and the parents of all hexagrams. Kan Li Kuang Guo, the main axis of the movement hub, female The four hexagrams of Mu are thought to be "Pu".
From the perspective of software development, although object-oriented programming tools provide the possibility of building infinite kinds of software systems, this infinity is based on the limited framework of its own class library. Whether it is Delphi's VCL, Java's class library, or .net framework, they are all built on such a structure similar to the Zhouyi Bagua. "It is easy to have Tai Chi, Tai Chi produces two forms, two forms produce four images, and four images produce Bagua." This structure is perfect and has infinite creativity.
Taiji is TObject in Delphi, it is the atom of building system, it is the ancestor of all classes, it has the basic characteristics of all classes. In the Delphi programming world, the root class TObject generates the persistent object class TPersistent, and the persistent object class TPersistent generates the component object class TComponent, which provides rich controls and powerful functions for developing applications.
However, the structural framework of the class library not only gives us components that can be used as "instruments", but more importantly, this structure realizes the construction and change of "qi" through the relationship and related functions between classes, reflecting object-oriented The essence of programming thinking. Provides an excellent demonstration for us to create our own systems.
The second mystery of Qi lies in its "invisible origin". Invisibility means its freedom, openness and adaptability. The idea of object-oriented programming is full of the invisible wisdom of "qi".
For example, polymorphism in object-oriented programming allows programmers to write more versatile and open programs. Programmers can write a pure virtual abstract method Stop() for the Vehicle object. Such a universal Stop() method has nothing to do with what car is driven. Programmers can let derived classes worry about completing the stop() method and continue writing their own general procedures at a higher level of abstraction. Even if the stop() method of the Car object is completely different from the stop() method of the Bicycle object, the programmer can also use Vehicle.stop(). Polymorphism allows the created object to automatically know which appropriate method will be called. . This makes the program possess the openness and adaptability of "qi".
In practicing qigong, it is emphasized that "the superior virtue is inaction, and it is not sought through observation. The lower virtue is practiced, and its use is endless." ("Zhou Yi Shen Tong Qi")
In the thinking of object-oriented programming, "Shangde" is a late-binding pure virtual abstract method, an object interface that remains unchanged and adapts to ever-changing changes, it is a high degree of abstraction of things, and it is metaphysics. "Shangde Wuwei" means that at the abstract level, the "virtual" side of programming is reflected through inaction, because at this time, the real object actually used cannot be determined (it may be a Car object or a Bicycle object, or it may be invented later) New vehicle object), "not based on observation" requires us to jump out of the constraints of specific needs and not consider the specific implementation code. Therefore, there is no code implementation in pure virtual abstract methods or object interfaces.
In the thinking of object-oriented programming, "Xiaode" refers to the coverage of pure virtual abstract methods and the implementation of object interfaces. "Do it with morality" provides real code implementation. "It is used endlessly" to meet the ever-changing needs.
Polymorphism allows programmers to derive objects and implement programs later without much effort. It doesn't matter if the programmer is building applications for Cars and Bicycles and doesn't know that Trucks exist. Programmers can write overriding stop() methods for the Car and Bicycle classes that inherit the Vehicle class. In this way, in the program, as long as the created Car and Bicycle objects are converted to the Vehicle type, and the Stop() method of the Vehicle is used, the Car and Bicycle objects can be dynamically bound to the stop() method that meets their own requirements. Even if a Truck object is added later, the Stop() method of Vehicle is still called, and no further changes to the program are required.
"Things are natural, and things are combined and separated. Some are close but cannot be seen, and some are far away and can be known. Those that are close but cannot be seen cannot be understood; those that are far away can be seen, but they can be seen in the future." ("Guiguzi") Arrive fourth)
Although objective things are complex and user needs change, there are also certain inherent laws.
Those who are close but invisible only look at the realization of specific functions in front of them and do not observe the general development laws of things. They only have isolated data and mechanical processes in their minds. Once "things come together", they will be caught off guard and find it difficult to deal with it. Such programming is static, mechanical, and difficult to maintain and extend.
Those who can see from a distance are good at discovering patterns, value code reuse, and see organic objects and harmonious relationships. Even if the needs change, they can deal with them calmly and with ease. Such programming is dynamic, flexible, maintainable and extensible.
Paul Kimmel said in "Delphi6 Application Development Guide" "It is a mistake to use object-oriented tools in a non-object-oriented way. Using Delphi to write structured programs can quickly reach the beta version... Your program may never be able to escape beta. Edition. Get the wrong answer quickly and still be wrong.”
Also using Delphi, if there is no object-oriented programming idea, it will be like "not noticing what is being said". In the end, it will still be "near but invisible", making it difficult to develop an excellent system. Only by practicing hard, summarizing carefully, and mastering the broad and profound ideas of object-oriented programming can we be able to control our "qi" freely, "reverse what we have learned, what we have learned", and finally reach the state of "knowing what is far away".
It has been more than 30 years since the first truly object-oriented language Smalltalk (1972) appeared. However, most of the object-oriented programming books in bookstores are C++ and Java. It seems that there are only these two object-oriented languages. In fact, there are 4 basic branches of real object-oriented languages and nearly 20 kinds. Since there are very few books on Delphi object-oriented programming, many programmers have to give up Delphi in order to learn OOP. This is really a great tragedy of Delphi. When I read Bruce Eckel's "Thinking in Java", I lamented why there is no such Delphi masterpiece?
In fact, Delphi is from a well-known family. It was developed by Borland Company on the basis of Object Pascal. Now, Borland Company uses Delphi language to replace Object Pascal [1] starting from Delphi7. Delphi is actually not inferior to C++ and Java in terms of OOP. Readers can refer to the appendix of this book "Comparison of Object-Oriented Programming Languages: Java, C++ and Delphi".
To this end, I have always planned to write a book on Delphi object-oriented programming to summarize my learning and practical experience in Delphi object-oriented programming. However, this is a very difficult task. The whole book took a long time from conception to writing, and it was not officially completed until May this year. It just so happens that this year is also the 20th anniversary of the founding of Borland. As a user of Borland's product Delphi, the publication of my book can be regarded as a commemoration of this.
This is a book that purely discusses object-oriented programming in Delphi. Object-oriented is not a fashionable embellishment of this book, but the core and entirety of this book.
Starting from Chapter 1 "Building New Object-Oriented Thinking", this book attempts to start with the history and current situation of object-oriented programming, expounding the origin, development and basic concepts of object-oriented programming ideas, as well as the application of object-oriented modeling methods and UML. This chapter is to help readers establish the basic concepts of object-oriented and understand the object-oriented thinking method.
Chapter 2 "Delphi Object Model" introduces the basic knowledge of Delphi object-oriented programming and its object model structure system.
Chapter 3 "Understanding Objects" discusses the internal mechanism, life cycle, and interaction of objects in depth from three aspects: the nature, life and death, and relationships of objects, laying a foundation for readers to understand and master objects.
Chapter 4 "Using Objects" explains how to use objects efficiently in Delphi object-oriented programming. Here we focus on the usage methods and techniques of interface objects, component objects, object sets and object parameters, and conduct in-depth thinking on common issues in the use and development of VCL components.
Chapter 5, "In-depth Polymorphism," introduces the concept of polymorphism and its application in programming. Through a large number of examples, important concepts and thinking methods such as overloading and overwriting, virtual methods and dynamic methods, abstract classes and abstract methods, and class type conversion are explained.
Chapter 6 "Analyzing Interfaces" comprehensively introduces the programming knowledge and application skills of object interfaces. It explains the important role of interfaces in realizing dynamic binding and multiple inheritance, and demonstrates the actual use of interfaces in object-oriented programming.
Chapter 7 "Research on Encapsulation" clarifies the importance and application principles of encapsulation in object-oriented programming, and further discusses the implementation methods and application techniques of encapsulation from the perspective of logical encapsulation and physical encapsulation respectively.
Chapter 8 "Realizing the Separation of Interface and Business" applies object-oriented programming to a new level. This chapter explains how to use object-oriented design to evolve a desktop program into a distributed multi-layer system through an evolutionary example of separation of interface and business. Integrating the latest Web technology of Delphi, it introduces how to use Web Service to encapsulate business objects, use Web Form to encapsulate interface objects, and use new technologies to encapsulate old objects, thereby realizing cross-platform applications.
Finally, Chapters 9 and 10 of this book, "In-depth Introduction to VCL", study the internal mechanism of VCL and analyze the object usage of important categories of VCL, providing a reference for readers who are eager to improve their programming level in depth.
Judging from the structure of this book, it can be divided into five parts.
Part 1, the first two chapters of the book are an introduction to Delphi object-oriented programming. Readers who have already mastered the basic concepts of object-oriented and have experience in Delphi programming can skip these two chapters.
Part 2, Chapters 3 and 4 are the key to object-oriented programming in Delphi. If you don't grasp the essence of the object, you can't use the object well.
Part 3, Chapters 5 and 6 are an in-depth introduction to object-oriented programming in Delphi. Advanced object-oriented techniques are all based on dynamic binding mechanisms such as virtual methods, abstract methods, and object interfaces, and type conversion mechanisms such as upward transformation, downward transformation, and interface transformation.
Part 4, Chapters 7 and 8 are the applications of Delphi object-oriented programming. In order to achieve program maintainability, scalability and reusability, encapsulation has become one of the important ideas of object-oriented programming. Through encapsulation, the interface and business objects are separated, and the distributed multi-layer architecture is gradually realized from the separation of interface and business, and then the cross-platform of interface and business applications is realized. Solutions from general applications to enterprise-level applications based on object-oriented programming ideas are demonstrated here.
Part 5, concluding Chapters 9 and 10, is a reference to object-oriented programming in Delphi. Familiarity with VCL and learning VCL are very helpful for mastering Delphi. In view of the current lack of insider information on VCL, what this part provides to readers may be limited, but it is rare.
To be precise, this book is not written for "experts", but for programmers who want to change from RAD to OOP, and for friends who want to learn OOP through Delphi. I think it is more suitable as a practical intermediate Delphi technical book. Therefore, in the writing of the whole book, we strive to be easy to understand, with pictures and texts, and a large number of example programs are carefully written (the source code on the CD-ROM accompanying the book exceeds 50 MB) for readers to study. This book focuses on core OOP, not all aspects of Delphi. Reading this book requires a certain basic knowledge of Delphi. The book involves some specialized knowledge (such as COM+, etc.) and requires readers to refer to related books for further reference.
There may be some "experts" who will be disappointed with this book. I feel that I am not suitable to write a book for "experts" to read, because I am not an expert. I feel that I will always be a novice. Different from other novices, I have been using Delphi for a longer time and have a little more experience and experience. Therefore, if you find any inaccuracies in this book, please correct me. I hope you will forgive me for any unsatisfactory things.
Netizen xzh2000 put it well, "The life of a book is very important. Only if the author can spend time revising and supplementing it frequently can it become a classic!" It is true that a good book needs to be revised many times before it can become a classic, so I am willing to listen to all readers' valuable suggestions and hope that this book can be continuously revised and reprinted.