Название: Beginning Programming All-in-One For Dummies
Автор: Wallace Wang
Издательство: John Wiley & Sons Limited
Жанр: Программы
isbn: 9781119884422
isbn:
Because programmers are always going to copy an existing program that works, object-oriented programming helps manage the copying process by using inheritance. The whole idea behind inheritance is that instead of making physical copies of code, you have only one copy of code (called a class) at all times.
Instead of physically copying all the code stored in a class, objects inherit all the code in a class by essentially pointing to the subprogram that they want to copy. This saves physical space by eliminating the need to make multiple copies of the same code.
Now instead of copying code (and risking creating duplicate copies), you can reuse existing code and add your own code. Now you can build more complicated programs by reusing existing objects like building blocks. If you modify code in one class, that modification automatically appears in any classes that inherit from that original class, as shown in Figure 2-8.
FIGURE 2-8: Object-oriented programming never physically copies code but “points to” or “inherits” code.
Object-oriented programming makes programs easier to write (by dividing a large program into parts), easier to understand (by organizing code into classes that mimic the actual problem the program is trying to solve), and easier to modify (by automatically updating any copies of code). All these advantages allow you, as the programmer, to focus more on solving problems and less on keeping track of trivial details.
Discover more about the details of object-oriented programming in Book 2, Chapter 7. For now, it’s just important that you understand why programmers use object-oriented programming. Then you can worry about figuring out how to use object-oriented programming.
Using Protocol-Oriented Programming
As computer scientists started using object-oriented programming techniques, they noticed the same limitations. Object-oriented programming encapsulates variables (properties) and functions (methods) that manipulate that data to model real-life objects like an engine. Object-oriented programming made it easy to copy and reuse code, but it also added complexity by creating objects that inherited more and more code that often wasn’t needed.
To help minimize the needless copying of code that would ultimately be ignored or modified, computer scientists created protocol-oriented programming. The main difference is that when you use object-oriented programming, you must define the method names and code to make that method work. With protocol-oriented programming, you can simply define the method name without writing any code within that method at all.
The purpose of simply defining the method name without writing any actual code is to create uniform method names that can be reused by your code just like objects. However, the advantage is that each class can adopt or conform to a protocol but write its own code for a particular method. Where object-oriented programming forces you to copy method names and the code that you may not need, protocol-oriented programming lets you just copy method names and customize the code to make it work the way you want. This reduces complexity and increases flexibility.
Protocol-oriented programming isn’t meant to replace object-oriented programming; it’s meant to work with it. Sometimes you may want to use object-oriented programming, and sometimes you may want to use protocol-oriented programming. Protocol-oriented programming is simply a way to reduce the complexity inherent in object-oriented programming.
Design Patterns
Although every program is different, all programs tend to require similar types of solutions. Rather than force programmers to reinvent solutions, computer scientists have identified common solutions to specific types of problems. These common solutions are called design patterns.
The main idea behind a design pattern is to show the best way to solve a specific type of problem. By following these best practices defined by a design pattern, you can spend less time thinking about the optimal way to solve a problem and simply use a design pattern to guide you into solving that particular problem using any programming language.
Design patterns focus on solving three types of common programming problems:
Creational: Defines the best way to create classes for different purposes
Structural: Defines the best way to design classes
Behavioral: Defines the best way for classes to share data and communicate with each other
Think of design patterns as cookie cutters to help you structure your code without telling you specifically how to write that code. By using design patterns, you can (hopefully) spend less time designing the structure of your program and more time writing reliable code faster than before.
DESIGNING PROGRAMS WITH TODAY’S METHODOLOGY
Each step — from spaghetti programming, to structured programming, to event-driven programming, to object-oriented programming, to protocol-oriented programming, to design patterns — is meant to guide programmers into writing better-organized programs that can be modified quickly and easily. Today, object-oriented programming, protocol-oriented programming, and design patterns are popular, but tomorrow, another programming methodology will likely arrive to deal with the shortcomings of object-oriented programming, protocol-oriented programming, and design patterns.
You want to avoid spaghetti programming, but structured programming, event-driven programming, object-oriented programming, and protocol-oriented programming are often used in the same program. You may use object-oriented programming and protocol-oriented programming to divide a program into objects, and then use structured programming to organize the commands you write and store inside each object. Finally, you may use event-driven programming to design a fancy UI so people know how to use your program.
By using each programming methodology’s strengths, you can create a well-designed program, on time, that actually works. Given the track record of government agencies and Fortune 500 corporations, creating working software on time is the closest thing to a miracle that most people will ever experience in a lifetime!
Chapter 3
Types of Programming Languages
IN THIS CHAPTER
Deciding on your first language
Understanding curly-bracket languages
Determining artificial intelligence languages