Structured programming. transition from structural to OOP

Lecture



Historically, several fundamentally different methodologies can be distinguished in the development of programming.

Initially, the concept of technology as such appeared in the period of “spontaneous” programming (this is the 60s of the last century). During this period, there was no concept of program structure, data types, etc. As a result, the code turned out to be confusing, contradictory. Programming those years was considered art. The end of the 60s - a crisis in programming.

The way out of this crisis is a transition to a structural programming paradigm. Structural programming approach is a set of recommended technological methods, covering the implementation of all stages of software development. The structural approach is based on decomposition (partitioning) of complex systems for the purpose of subsequent implementation in the form of separate small subprograms. With the advent of other principles of decomposition (object, logical, etc.), this method is called procedural decomposition.

Another basic principle of structured programming is the use of only basic algorithmic structures in programming (see ticket number 4), a ban on the use of the GOTO operator.

The structural approach required the presentation of the task as a hierarchy of subtasks of the simplest structure. The design was carried out “from top to bottom” and implied the realization of a common idea, ensuring the elaboration of subprogram interfaces. At the same time, restrictions on the design of algorithms were introduced, formal models of their description were recommended, as well as a special method for designing algorithms — a step-by-step method of detailing.

Support for the principles of structured programming was the basis of the so-called procedural programming languages. As a rule, they included the main “structural” control transfer operators, supported nesting of routines, localization and restriction of data “visibility”. Among the most famous languages ​​of this group are PL / 1, ALGOL-68, Pascal, C.

A further increase in the complexity and size of the software being developed required the development of data structuring. As a result of this, it is possible in languages ​​to define custom data types. At the same time, the desire to delimit access to the global data of the program intensified in order to reduce the number of errors arising when working with global data. As a result, modular programming technology emerged and began to develop.

Modular programming involves the allocation of groups of subroutines that use the same global data into separately compiled modules (subroutine libraries), for example, a module of graphic resources. Communication between modules using this technology is carried out through a special interface, while access to the implementation of the module (subprogram bodies and some “internal” variables) is denied. This technology is supported by modern versions of the languages ​​Pascal and C (C ++), the languages ​​of Hell and Modula.

Object-oriented programming (OOP) is defined as a technology for creating complex software based on the representation of a program as a set of objects, each of which is an instance of a certain type (class), and the classes form a hierarchy with property inheritance. The interaction of software objects in such a system is carried out by sending messages.

The main advantage of object-oriented programming in comparison with modular programming is “more natural” software decomposition, which greatly simplifies its development. This leads to a more complete localization of data and their integration with processing routines, which allows for the almost independent development of individual parts (objects) of the program. In addition, the object approach offers new ways to organize programs based on the mechanisms of inheritance, polymorphism, composition, and content. These mechanisms allow the construction of complex objects from relatively simple ones. As a result, the code reuse rate increases significantly and it becomes possible to create class libraries for various applications.

The rapid development of object-based programming technologies made it possible to solve many problems. So, environments that support visual programming have been created, for example, Delphi, C ++ Builder, Visual C ++, etc. When using the visual environment, the programmer has the opportunity to design some of it, for example, the interfaces of the future product, using visual tools to add and configure special library components. The result of visual design is the preparation of a future program, in which the corresponding codes have already been entered.

A general definition can be given: an OOP object is a collection of state variables and associated methods (operations). These methods determine how an object interacts with the outside world.

Object methods are procedures and functions whose declaration is included in the object description and which perform actions. The ability to manage the states of an object by calling methods in the end determines the object's behavior. This collection of methods is often referred to as an object interface.

Encapsulation is a mechanism that combines data and methods that manipulate this data, and protects both from external interference or misuse. When methods and data are combined in this way, an object is created.

By using encapsulation, we protect the data belonging to the object from possible errors that can occur during direct access to this data. In addition, the application of this principle very often helps to localize possible errors in the program code. And this greatly simplifies the process of finding and correcting these errors. We can say that encapsulation implies data hiding, which allows you to protect this data. However, the use of encapsulation leads to a decrease in the efficiency of access to the elements of the object. This is due to the need to call methods to change the internal elements (variables) of the object. But at the present level of development of computer technology, these losses in efficiency do not play a significant role.

Inheritance is the process by which one object can inherit the properties of another object and add to them features that are specific only to it. As a result, a hierarchy of object types is created, where the fields of the data and methods of the “ancestors” are automatically the fields of the data and methods of the descendants.

The meaning and universality of inheritance is that it is not necessary every time to re-write (“from scratch”) a new object, but you can specify a “parent” (base class) and describe the distinctive features of the new class. As a result, the new object will have all the properties of the parent class plus its own distinctive features.

Polymorphism is a property that allows the same name to be used to solve several technically different tasks. Polymorphism implies such a definition of methods in a type hierarchy, in which a method with the same name can be applied to various related objects. In a general sense, the concept of polymorphism is the idea of ​​“one interface - many methods”. The advantage of polymorphism is that it helps reduce the complexity of programs by allowing the use of a single interface for a single class of actions. The choice of a specific action, depending on the situation, rests with the compiler.

Modern programming technology is a component approach that involves building software from separate components — physically separate existing parts of software that interact with each other through standardized binary interfaces. Unlike ordinary objects, component objects can be assembled into dynamically called libraries or executable files, distributed in binary form (without sources), and used in any programming language that supports the appropriate technology. Today the market of objects has become a reality. This allows programmers to create products that are at least partially composed of reused parts, i.e. use technology that is well proven in the design of equipment.

Component approach underlies the technologies developed on the basis of COM (Component Object Model - component object model), and the technology of creating distributed applications CORBA (Common Object Request Broker Architecture - a common architecture with an intermediary processing request objects). These technologies use similar principles and differ only in the peculiarities of their implementation.

The transition from structured programming to object-oriented is associated primarily with the increasing complexity of the software being created. The first programs did not exceed a few hundred lines. The increase in the program code to several thousand lines led to the introduction of methods of structured programming (functions appeared), allowing to create and maintain programs of up to one hundred thousand lines in size. The rapid development of software required the creation and maintenance of an even greater amount of code. The answer to this was the creation of object-oriented technology, which will be discussed in this chapter. With the help of this technology it is possible to create large-scale applications. It allows the programmer to mentally repel himself not from the computer architecture, but to operate with concepts denoting objects of the real world.

Over the last two decades, object-oriented approach (OOP) has become widely used and introduced into many programming languages, including PHP 5.0.

The concept of OOP. Comparison of structural and object-oriented approaches to programming

All programming languages ​​are built on abstraction. The first assemblers were abstractions of machine codes, the programming of which required a detailed knowledge of the computer architecture. The introduction of the assembler made it possible not to memorize a huge number of processor codes and the rules for their use. In turn, high-level languages, which appeared after the assembler, were an abstraction of the assembler. The emergence of a structural approach to programming is characterized, first of all, by the use of functions - a part of the code can be formalized as functions and used several times in different places of the program. This technique allows you not only to reuse the code, but also to debug it in small blocks that correspond to specific tasks. And although the functions are models of real-world processes, their use in the program still makes us think about the structure of the computer.

Assembler and high-level languages ​​simulate a computer, making it easier for the programmer to communicate with it. An alternative to computer modeling is modeling a solvable problem. Such programming languages ​​as LISP and PROLOGUE use this approach. However, with the help of these languages ​​it is rather difficult to solve problems for which they were not originally intended.

The object-oriented approach models not individual processes, but real-world objects and their behavior. The program can adapt to the complexity of the real world, creating new types of objects. Thus, OOP allows you to describe a problem in its terms, and not in terms of a computer. Each object represents a kind of small computer: it has states and functions that can change these states, thus describing the behavior of the object. This approach can significantly increase the amount of generated and maintained code.

Human consciousness operates with concepts that are abstractions of individual objects. It is clear that the "table" is satisfied with all the tables in the world. If further to clarify: "gray table", then this concept excludes all tables whose color is different from gray. The more refined concept of a “gray table with drawers” ​​still narrows the number of tables satisfying this definition. By characterizing the table object with an increasing number of parameters, from an abstract concept one can arrive at a completely specific subject.

This approach was adopted by the object-oriented technology object makers. The basis of this technology is a construct, called a class, which plays the role of "concept". A class consists of variables, called class members, that contain various characteristics of our concept. Subjects can have easily visible or hidden characteristics. When working with a TV, we can use the control panel to change the contrast and brightness of the image, the volume of the sound, and various microcircuits and other details of the TV set are hidden from us by the body and are not available for everyday use. Similarly, class members can be open and closed.

In addition to static characteristics similar to color, shape, volume, weight, etc., objects have characteristics related to their behavior or actions performed on them. For example, any table can be moved in different directions, put objects on it, etc. such dynamic characteristics of an object are modeled in a class by separate functions, called class methods.

Thus, classes are constructions that model our concepts. The class is used as an extended type of variable. Only when declaring such a “variable” do we get not an ordinary variable, but an object model, which we will call further simply an object. The object is divided into two parts: the closed one, which is accessible only to functions inside the object, and open, which can be called for this object from the text of the main program.

The refinement of a class (concept) occurs through a special mechanism called inheritance. When declaring a class, you can specify that the new class is a successor to an existing one. Thus, if a table class was previously created, which is characterized by the presence of a tabletop and the ability to move in space, then it can inherit the “gray table” class, in which the only variable responsible for color will be declared, with all methods and characteristics of the base class inherited automatically. Those. The gray table No. also has a tabletop and can move in space. This class can be inherited by a new type of class, for example, a "gray table with a drawer", in which you can add variables that characterize the volume of a table drawer, and two additional methods - extending and closing the drawer of the table.

In addition to inheritance, there is another type of relationship called aggregation. Classes can contain other objects as members. If the class "water transport" can be basic for a submarine, cruiser, liner, then, in turn, any of the ships may contain a deck, engine, navigation equipment, etc. Moreover, the number and type of objects contained in the class are unlimited.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Object oriented programming

Terms: Object oriented programming