Chapter 1 Objective Getting Started "Why is object-oriented programming cause so shocked impact on software development?" Object-oriented programming (OOP) has many appeals. For managers, it achieves faster and cheaper development and maintenance processes. For analysis and designers, modeling processing becomes simpler, can generate clear and easy maintenance design. For programmers, the object model is so elegant and shallow. In addition, the huge power for object-oriented tools and the library makes programming become a more pleasant task. Everyone can benefit from it, at least the surface. If it has a shortcomings, it is to master it needs to pay. When thinking about the object, you need to use image thinking, not programmed thinking. Compared with programming design, the object's design process is more challenging - especially when attempting to create reusable (renewable) objects. In the past, those who initially targeted object-oriented programming must conduct a painful choice: (1) Select a language such as SmallTalk, and must master a giant library before "outper". (2) Choose C (annotation 1) that does not have the library, then learn from this language until you can write it yourself. 1: Fortunately, this situation has changed significantly. There is now a third-party library and the standard C library for use. In fact, it is difficult to design an object - it is difficult to design anything. Therefore, only the number of "experts" can design the best object and then let others enjoy. For successful OOP languages, they not only integrate this language syntax and a compiler (compiler), and there is a successful development environment, which contains excellent design, easy to use. Therefore, most programmers's primary tasks are to solve their application problems with existing objects. The goal of this chapter is to reveal the concept of the programming of objects to everyone and prove how simple it is. This chapter will explain the number of Java's multi-design ideas and interpreted object-oriented programming. However, pay attention to the reading of this chapter after reading this chapter, you cannot immediately write a full-featured Java program. All detailed descriptions and examples will slowly walk in other chapters of this book. 1.1 Abstract progress All programming language is to provide an "abstract" approach. A more controversial statement is: solving the complexity of the problem directly depends on the type and quality of the abstraction. What kind of "species" here refers to what is "abstracted"? The assembly language is a small amount of abstraction to the basic machine. Later, many "command" languages (such as Fortran, Basic and C) are an abstraction of assembly language. These languages have made great progress than assembly language, but their abstract principles still require us to consider the structure of the computer, not the structure of the problem itself. The programmer must establish a connection between the machine model (located in the "Scenarius") and the actual solution (located in "problem space"). This process requires people to pay greater energy, and since it leaves the scope of the programming language itself, it is difficult to write, and it takes a large cost to maintain maintenance. The side effect caused by this is a perfect "programming method" discipline. Another way to model the machine is to make a model for problem solving. For some early language, such as LISP and APL, their approach is "Observe the world from different angles" - "All issues are summarized as a list" or "all issues are summarized as algorithms." ProLog summarizes all issues as a decision chain. For these languages, we believe that part of them is facing "mandatory" programming, and the other is designed for processing graphic symbols. Each method has its own special purpose, suitable for solving a problem. But as long as it exceeds the scope of their strength, it will be very awkward. Object-oriented programming On this basis, the programmer can use some tools to express the elements in the problem space.
Because this expression is very common, it is not necessary to limit the problem of specific types. We use the elements in the problem space and their representations in the program space (Object). Of course, there are some other objects that have no corresponding body in the problem space. By adding new object types, the program can make flexible adjustments to cooperate with specific issues. Therefore, when the code of the reading scheme is read, the discourse to the problem is read. Compared with our previous opinions, this is undoubtedly a more flexible, more powerful language abstract method. In summary, OOP allows us to describe problems based on the problem, not based on the solution. However, there is still a connection to the computer. Each object is similar to a small computer; they have their own state and can require them to perform specific operations. Compared with the "object" or "object" of the real world, the programming "object" is also commonplace: they all have their own characteristics and behavior. Alan Kay summarizes the five basic features of SmallTalk. This is the first successful object-oriented programming language and the basics of Java. Through these features, we understand what is the "pure" object-oriented programming method:
(1) Everything is an object. Objects can be imagined into a new variable; it saves data, but it can be required to operate itself. In theory, all conceptual components can be proposed from issues to be resolved, and then express them into an object in the program. (2) The program is a combination of a large number of objects; through message delivery, each object knows what you should do. In order to issue a request to an object, you need to "send a message" to that object. More specifically, you can imagine the message as a call request, which calls a subroutine or function from the target object. (3) Each object has its own storage space, which can accommodate other objects. Or, by encapsulating existing objects, new objects can be produced. So, although the concept of object is very simple, it can reach any high complexity in the program. (4) Each object has a type. Depending on the syntax, each object is a "instance" of a "class". Among them, "class" is a synonym of "type" (Type). A class is the most important feature is "what message can I send it?". (5) All objects can receive the same message. This is actually a saying that there is something else, you can understand soon. Since an object of the type "circle" is also an object of the type "Shape" (Shape), a circle can fully receive shape messages. This means that the program code will command "shape" to automatically control all objects that meet the "shape", which naturally include "circle". This feature is called "replaceability" of the object, one of the most important concepts of OOP. Some language designers think that object-oriented programming itself is not enough to facilitate all forms of program problems, advocating different methods into "multi-shaped programming language" (annotation 2). 2: See "Multiparadigm Programming in LEDA" edited by Timothy Budd, Addison-Wesley published in 1995. 1.2 Interface of the object Aristotle may be the first person to carefully study the concept of "Type", and he once talked about "fish and birds". In the world's first object-oriented SIMULA-67, the first time I use this concept: all objects - although each characteristic-part is part of a series of objects, these objects have general features and behavior. In SIMULA-67, the keyword for Class first uses, introduced a new type for the program (CLAS and TYPE usually interchangeable; annotation 3). 3: Some people have further distinguished, they emphasize that "type" determines the interface, and "class" is a special implementation of the interface. SIMULA is a good example. As this name is implicit, its role is "Simulate" like "bank out" such a classic problem. In this example, we have a series of people, customers, accounts, and transactions. Each type of member (element) has some universal characteristics: each account has a certain balance; each of the wigs can receive the deposit of the customer; At the same time, each member has its own state; each account has different balances; each with a name. So in a computer program, you can use a unique entity to indicate the NA, customer, account, and transaction, respectively. This entity is "object", and each object is affiliated with a specific "class", which has its own general feature and behavior. Therefore, in the object-oriented programming, although we really want to do a new various data "type" (Type), almost all object-oriented programming languages use "Class" keywords.
When you see the word "Type", please think "Class" at the same time; vice versa. After building a class, many objects can be generated according to the situation. Subsequently, those objects can be processed as elements to solve problems. In fact, when we perform an object-oriented program design, the biggest challenge is: How to "problem space" in "problem space" (where problems actually exist) Establish an ideal "one-to-one" corresponding or mapping relationship between the elements of the computer. How do I use the object to complete the truly useful job? There must be a way to make a request to the object, making it some practical things, such as completing a transaction, draw something on the screen or open a switch, etc. Each object can only accept a specific request. Our request to objects is in its interface form by "interface" defined by its "interface" (type "or class, or class". The equivalence or correspondence of "Type" and "Interface" is the basis for object-oriented programming. Let us take a light bulb as an example: Light LT = New Light (); lt.on (); In this example, the name of the type / class is light, and the request to the Light object includes open (ON), close (OFF), becoming brighten or darker (DIM). By simply declaring a name (LT), we created a "handle" for the Light object. Then create an object with the NEW keyword to the Light. Use the equal sign to assign it to the handle. In order to send a message to the object, we list the handle name (LT), and then connect it with a message name (ON) with a period symbol (.). It can be seen from it. When using some predefined classes, our code we use is very simple and intuitive. 1.3 Realization of the hidden discussion of the future, let us first classify the practitioners in this area. Fundamentally, there are two people who are involved in object-oriented programming: "Create a new data type person) and" customer programmer "(in its own application); Note 4). For client programmers, the most important goal is to collect a programming "toolbox" full of various classes to quickly develop applications that meet yourself. For class creators, their goal is to build a class from the head, and only all the options (interfaces) open to the customer programmer, all other details are hidden. Why do you do this? After hidden, the customer programmer can not contact and change those details, so the original person does not have to worry about illegal modification, ensuring that they will not affect others. 4: Thanks to my friend Scott Meyers, he helped me got this name. "Interface" specifies which requests can be issued to a particular object. However, some code must be present in some place to meet these requests. These codes are called "hidden implementations" with those hidden data. Standing at the perspective of procedural programming, the entire problem is not complicated. One type contains functions associated with each possible request. Once a specific request is sent to an object, that function is called. We usually summarize this process to "send a message" to the object (propose a request).
The responsibility of the object is to determine how to respond to this message (perform the corresponding code). For any relationship, it is important to make all members who are involved in the same rules. When you create a library, it is equivalent to establishing a relationship with the customer programmer. The other party is also a programmer, but their goal is to combine a specific application (program), or build a larger library with your library. If anyone can use all the members of a class, then the customer programmer can do anything about that class, there is no way to force them to comply with any constraints. Even if you are very reluctant to use some members you contained in the class, if you don't have access control, there is no way to stop this situation - all things will expose.
There are two reasons to control our access to members. The first reason is to prevent programmers from touching things they should not be in touch - usually the design idea of internal data types. If only to solve a specific problem, users only need to operate the interface, do not need to understand this. We are actually a service to users because they can easily see which are very important to themselves, and which can be ignored. The second reason for access control is to allow library designers to modify internal structures, do not worry about what impact on customer programmers. For example, we have started a simple class that can be designed to simplify development. In the future, it will decide to rewrite so far. If the interface and implementation methods have already been separated, they can be assured to do this, and they can be re-linked to it. Java uses three explicit (clarified) keywords and an implicit (hint) keyword to set class boundaries: public, private, protected, and hintted Friendly. If other keywords are not explicitly specified, the latter is silently. The use and meaning of these keywords is quite intuitive, and they determine who can use subsequent definitions. "Public" means that the subsequent definition can be used. On the other hand, "private" means that anyone else can access subsequent definition information in addition to your own, type of creator and the type of internal function member. Private is a wall with a wall between your programmers. If someone tries to access private members, it will get a compile period error. "Friendly" involves the concept of "packaging" or "package" - JAVA is used to build a library. If something is "friendly", it means that it can only be used within this packaging (so this access level is sometimes called "packaging access"). "Protected" is similar to "private", just a inherited class to access protected members, but cannot access private members. The inherited problem will soon be talked. 1.4 Reuse of the program creates and tests a class, it should (from the ideal angle) represent a useful code unit. However, it is not like many people hope, this reuse ability is not easy to implement; it requires more experience and insight, so that a good program can be designed to use it. Many people think that the code or design is reused is the greatest lever for object-oriented programming. To reuse a class, the easiest way is to use only the object of that class directly. But it can also place an object of that class into a new class. We call this called "Create a member of the object." New categories can be constructed from other objects of any quantity and type. In any case, as long as the new category has reached the design requirements. This concept is called "Organization" - organizes a new class on the basis of existing classes. Sometimes, we also tested "including" relationships, such as "a car contains a gearbox." The organization of the subject has great flexibility. The new class "Member Object" is usually set to "private" (private "(private", using this class customer programmer cannot access them. In this way, we can modify those members without interfering with the customer code. You can also change members in the "running period", which further increases flexibility. The "inheritance" to be told later does not have this flexibility because the compiler must limit the class created by inheritance. Due to the importance of inheritance, it is often emphasized in the object-oriented programming. As a newly joined programmer, perhaps, "Inheritance should be seen everywhere." The design that is generated along this idea will be very awkward and will greatly increase the complexity of the program.
On the contrary, when new categories, the "Organization" object should be considered; so it is more simple and flexible. With the organization of the object, our design can be kept refreshing. Once you need to use inheritance, you will be able to realize this. 1.5 Inheritance: Re-use the interface, the concept of objects can bring us great convenience. It is conceptually allowed to package a wide range of data and features together. This will properly express the concept of "problem space" without deliberately follow the expression of the base machine. In programming languages, these concepts are reflected to specific data types (using Class Keywords). After we spend his heart, then after making a data type, if you have to create a type, it will make it roughly the same function, which will be a very discouraged thing. However, if you can use the ready-made data type, it is "clone", and then add and modify according to the situation, the situation is much more ideal. "Inherited" is designed for this goal. But inherit is not completely equivalent to cloning. During the inheritance process, if the original class (the official name is called the basic class, superclass or parent class), the modified "clone" class (official name is called inheritance or subclass) will also reflect this Variety. In Java language, inheritance is equivalent to creating a new class when the use inheritance implemented by an Extends keyword. This new category not only contains all the existing types of members (although the Private member is hidden and cannot be accessed), but more importantly, it copies the basic class interface. That is, all messages that can be transmitted to the base class can also be sent to the object of the derived class. According to the message you can send, we can know the type of class. This means that the derived class has the same type as the base class! In order to truly understand the meaning of the design of the target program, we must first recognize the equivalent relationship of this type. So the interface must be specially designed due to the identical interface of the base class and derivative classes. That is, after the object receives a specific message, there must be a "method" to be executed. If it simply inherits a class, do not do anything else, the method from the basic class interface will be moved directly to the derived class. This means that the object of derivatives not only has the same type, but also the same behavior, this consequence is usually we don't want to see. There are two ways to divide new derivatives to distinguish between the original basic class. The first practice is very simple: add new functions (functions) for derivative classes. These new functions are not part of the base interface. When making this treatment, it is generally realized that the basic class cannot meet our requirements, so more functions need to be added. This is the simplest, most basic inheritance usage, most of which can perfectly solve our problems perfect. However, in advance, you should carefully investigate whether these extra functions are really required. 1.5.1 Improve the basic class Although the Extends keyword suggests that we have to "expand" new features for the interface, the truth is not certain. To distinguish our new class, the second method is to change the behavior of an existing function of the base class. We call it "improvement" function. To improve a function, just create a new definition for the function of the derived class. Our goal is: "Although the function interface used is not changed, its new version has different performances." 1.5.2 Isometric and similar relationships The inheritance may produce such an argument: Can the inheritance can only improve the original basic function? If the answer is yes, the derivative type is identical to the basic class, because all have exactly the same interface. The result of this is: We can completely replace an object of the derived class into a basic class! It can be imagined into a "pure replacement". In a sense, this is an ideal way for inheritance. At this point, we usually believe that there is a "equivalent" relationship between the basic and derivatives - because we can talk to straight: "Circle is a geometric shape."
In order to test inheritance, one means to see if he can set them into this "equivalent" relationship to see if it makes sense. But in many times, we must add new interface elements for derivative types. So not only expand the interface, but also created a new type. This new type can still replace into a basic type, but this replace is not perfect because it is not accessible to the basic class. We call it "similar" relationship; new types have old types of interfaces, but also contain other functions, so they can't say that they are completely equivalent. For example, let's consider the situation of the refrigerator. Assuming our room has a variety of controllers for refrigeration; that is, we have necessary "interface" to control the cooling. Now suppose the machine has failed, we change it into a new type of cold, hot dual-use air conditioner, winter and summer can be used. Cold, hot air conditioners "similar" refrigerator, but you can do more things. Since our room is only installed with a refrigerated device, they are limited to the refrigeration part of the new machine. The interface of the new machine has been expanded, but the existing system does not know anything other than the original interface. When you understand the differences and similar differences, you will have much more. Although most of the "pure replacement" is enough, you will find that in some cases, there is still a significant reason to add new features on the basis of derivatives. By discussing the previous two cases, I believe that everyone has had a number of people. 1.6 Interchange of multi-shaped objects Usually, inherits will eventually create a series of classes, all classes are based on unified interface. We use an upside down tree map to clarify this (Note 5): 5: This uses "unified mark", this book will mainly use this method. An important process we want to do with such a series of classes is to treat a derived object as an object of the basic class. This is very important because it means we only need to write a single code, to ignore the specific details of the type, only deal with the base class. In this way, those code can be separated from the type information. So it is more easier to write, it is also more easy to understand. In addition, if a new type is added by inheritance, such as "triangle", then our code written for the "geometric shape" will work as good as in the old type. Therefore, the program has "extended ability" and has "scalability". Based on the example above, suppose we write such a function with Java: void dostuff (shape s) {
S.RASE ();
// ...
s.draw ();
}
This function can communicate with any "geometric shape", so it is completely independent of it to depict (DRAW) and delete any particular type of object. If we use the dostuff () function in other programs:
Circle C = New circle ();
Triangle T = New Triangle ();
Line L = new line ();
Dostuff (c);
Dostuff (t);
Dostuff (L);
Then the call to dostuff () will automatically work well, regardless of the specific type of object.
This is actually a very useful programming skill. Consider the following line of code:
Dostuff (c);
At this point, a CIRCLE handle is passed to a function that is expected to look forward to the Shape handle. Since the circle is a geometric shape, dostuff () can be processed correctly. That is, any dostuff () can be sent to a shape message, Circle can also receive. So doing this is safe and will not cause errors.
We use this process of derivating the derivative as the basic type of the basic type of "UPCASTING". Among them, "CAST" means creating according to an outgoing model; and "UP" (up) indicates that the direction of inheritance is from "above" - that is, the base class is on top, and the derived class is expanded below. Therefore, according to the base class is a process from above, that is, "Upcasting". In object-oriented programs, top-up techniques are usually used. This is a good way to avoid investigating accurate types. Take a look at the code in dostuff ():
S.RASE ();
// ...
s.draw ();
Note that it doesn't express this: "If you are a circle, you do this; if you are a Square, you do it; etc.". If you write a code, you need to check all possible types, such as circular, rectangular, and more. This is obviously very troublesome, and each time a new Shape type is added, it must be modified accordingly. Here, we only need to say: "You are a geometric shape, I know you can delete yourself, ie ERASE (); please take that action, and to control all the details."
1.6.1 Dynamic Binding
In the code of dostuff (), most surprisingly, although we did not make any special instructions, the operation taken is completely correct and appropriate. We know that the code executed when calling DRAW () for the Circle Different when calling DRAW () for a Square or Line Draw (). However, when the Draw () message is sent to an anonymous Shape, the correct operation is taken according to the actual type of connection at the time of the Shape handle. This is of course surprising, because when the Java compiler is compiled for dostuff (), it doesn't know what is the exact type you have to operate. Although we do can guarantee that ERASE () will eventually call your exiled (), call Draw (), but does not guarantee what is specific Circle, Square or Line call what is. However, the last operation taken is equally correct, how did this do it?
When a message is sent to an object, if it doesn't know what the specific type of the other party is, the action taken is the same, which is called "Polymorphism). For the programming language of the object-oriented object, they are called "dynamic binding" methods for achieving polymorphism. The compiler and the runtime system will be responsible for the control of all details; we only need to know what will happen, and more importantly, how to use it to help you design the program.
Some languages require us to use a special keyword to allow dynamic binding. In C , this keyword is Virtual. In Java, we don't have to remember to add a keyword because the dynamic binding of the function is automatically made. So when you send a message to the object, we can affirm the object at all, even though it involves processing such as winding.
1.6.2 Abstract basic and interface
When designing programs, we often want the basic class to provide an interface for our derivative class. That is, we don't want anyone else to actually create an object of the basic class, just shape it into it in order to use their interfaces. To achieve this, you need to turn that class into "abstraction" - use the Abstract keyword. If someone tries to create an object of an abstract class, the compiler will block them. This tool can effectively enforce a special design.
It is also possible to describe a method that has not been implemented - as a "root", pointing out: "This is an interface function for all types of interfaces that inherited from this class, but there is currently no form of implementation. "Abstract method may only be created in an abstract class. After inheriting a class, that method must be implemented, otherwise the inherited class will also become an "abstract" class. By creating an abstract method, we can place a method into an interface without having to provide the possible body code for that method. The Interface keyword extends the concept of abstract classes more step, it is completely disabled all of the function definitions. "Interface" is a quite effective and common tool. In addition, if you are willing, you can also merge multiple interfaces together (you cannot inherit from multiple ordinary Class or Abstract Class).
1.7 Object creation and existence time
From a technical point of view, OOP (object-oriented programming) is only involved in abstract data types, inheritance, and versatility, but others may be very important. This section will discuss these issues.
One of the most important issues is the creation and destruction of objects. Where is the data required by the object, how to control the "existing time" of the object? In response to this problem, the solution solution is everything. C believes that the execution efficiency of the program is one of the most important issues, so it allows programmers to make a selection. To get the fastest running speed, storage, and existence time can be determined when writing a program, simply placing the object in the stack (sometimes called an automatic or domain variable) or the static storage area. This provides a priority for allocation and release of storage space. In some cases, this priority control is very valuable. However, we also sacrificed flexibility because when writing programs, you must know the accurate quantity of objects, time, and types. If you want to solve a more conventional problem, such as computer-aided design, storage management, or air traffic control, this method is too limited.
The second method is to dynamically create an object in a memory pool, which is also called "heap" or "memory". If this is used, unless you enter the running period, you don't know how many objects needed at all, nor do you know how long they have time, and what is the exact type. These parameters are determined when the program is officially run. To need a new object, just simply create it in the memory pile when you need it. Since the management of storage space is dynamically performed during operation, the time to allocate storage space in memory is much longer than in the stack (create a storage space in the stack, only a simple instruction is required, and the stack pointer will be required. Move down or down). Since the dynamic creation method makes the object it will tend to be complex, find the stored space and the additional overhead they need will not have a significant impact on the creation of the object. In addition, greater flexibility is critical to conventional programming problems.
C allows us to decide whether to create an object when writing a program, or is created during operation, which is more flexible. Everyone may think that since it is so flexible, you should create an object in memory in memory, not in the stack. However, it is also necessary to consider another problem, that is, the "existing time" or "Lifetime" of the object. If an object is created in the stack or static storage space, the compiler will determine how long the duration of the object is automatically "destroyed" or "clear" it. Programmers can use two ways to destroy an object: use programmatic ways to decide when to destroy objects, or use a "garbage collector" feature provided by the operating environment to automatically find objects that are no longer used, and will Clear. Of course, the garbage collector is much convenient, but all applications must tolerate the existence of garbage collectors and can pass the additional overhead brought by garbage collection. But this does not meet the design tenet of C language, so it is not included in C . However, Java did provide a garbage collector (SmallTalk has such a design; although Delphi defaults to no garbage collector, you can choose to install; while C can also use some garbage collection products developed by other companies). The remaining parts of this section will discuss additional factors to consider when manipulating objects.
1.7.1 Collection and Inheritors
For a specific problem solving, if you don't know how many objects needed in advance, or how long your duration is, you don't know how to save those objects. In this case, how can I know how many spaces? I can't know in advance, unless you enter the running period.
In object-oriented design, the solution to most problems seems to be a little probability - just simply creates another type of object. Novel objects for solving a particular problem accommodate handle to other objects. Of course, you can also use the array to do the same thing, that is a function of most languages. But you can't just see this. This new object is typically called "collection" (also called a "container", but AWT applies this term in different occasions, so this book will always use the "collection" name. When needed, the collection will automatically Expand yourself in order to adapt to anything we are in place. So we don't have to know how much things are going to have a collection in advance. Just create a collection, the future work makes it accountable.
Fortunately, design excellent OOP language supports a collection of collections. In C , they are provided in the form of "Standard Template" (STL). Object Pascal provides a collection with its own "visual component library" (VCL). SmallTalk offers a very complete collection. Java also provides a collection with its own standard library. In some libraries, a regular collection can meet most of the requirements; in other libraries (especially C libraries), different types of collection are provided for different needs. For example, you can use a vector unified access to all elements; a link list is used to ensure that all elements are unified. So we can choose the appropriate type according to your needs. These include sets, queues, hash tables, trees, stacks, and more.
All collections provide corresponding read and write functions. The way to use something in the collection, the way is very obvious. There is a function called "Push" (Push), "Add" (add) or other similar names for this. However, when the data is removed from the collection, the way is not always so obvious. If it is an entity in an array, such as a vector, then it may be able to use an index operator or function. But in many cases, this will tend to return. In addition, the function of single-selection functions is very limited. If you want to manipulate or compare a series of elements in the collection, not just one, what should I do? The way is to use a "ender" that belongs to an object, which is responsible for selecting the elements within the collection and supplies them to the user. As a class, it also provides levels of abstraction. With this level, the collection details can be separated from the code used to access the set. The collection is abstract into a simple sequence through the actuator. The heir allows us to traverse that sequence, while doing care for the infrastructure - in other words, no matter whether it is a vector, a link list, a stack, or something else. In this way, we can flexibly change the basic data, and will not interfere with the code in the program. Java is the first (in version 1.0 and 1.1), providing a standard heir, named Enumeration, serving all of its collection class. Java 1.2 adds a more complex collection library that contains a hemerry named Iterator that can do more than vintage enumeration.
From the design perspective, we need a full-featured sequence. By manipulating it, you should solve your own problems. If a type of sequence can meet all our requirements, there is no need to replace different types again. There are two reasons to prompt us to choose a collection. First, the collection provides different interface types as well as external behavior. The interface between the stack is different from the behavior and the queue, and the interface between the queue is different from a set (SET) or list. With this feature, we have greater flexibility when we solve the problem.
Second, different collections often have different efficiency when performing specific operations. The best example is the difference between vector (vector) and list (list). They all belong to a simple sequence, with a fully consistent interface and external behavior. But when performing some specific tasks, it is completely different. Random access (access) for elements in the vector is a normal operation; whether we choose what is selected, the amount of time required is the same. But in a list of links, if you want to move, and randomly select an element, you need to pay the price of "heavy". And assume that an element is located far from the list, it will live a lot of time it takes. But on the other hand, if you want to insert an element in the middle of the sequence, use a list more than the vector. These and other operations have different execution efficiency, which depends on what is the infrastructure of the sequence. In the design phase, we can start with a list first. In the final adjustment, change it to the vector according to the situation. Since the abstraction is made through the hemer, it can be easily handled in both the two, and the impact on the code is negligible.
Finally, remember that the collection is just a storage center for placing an object. If the storage can meet all our needs, it is completely unnecessary to care about how it is implemented (this is a basic concept of most type objects). If you work in a programming environment, it produces internal overhead due to other factors (such as running under Windows, or brought overhead by the garbage collector), the difference between the vector and the link list is perhaps Not a big problem. We may only need a type of sequence. I can even imagine a "perfect" collection, it can automatically change the realization of the base layer according to their own way. 1.7.2 single structure
In object-oriented programming, a problem with especially since the introduction of C is: All classes will be inherited from a single basic class. In Java (like other almost all OOP languages), the answer to this question is certain, and the name of this final base class is very simple, that is, "Object". This "single structure" has many advantages.
All objects in a single structure have a general interface, so they are ultimately the same type. Another solution (like C ) is that we cannot guarantee that all things belong to the same basic type. From a backward-compatible perspective, this solution can be better coordinated with the C model, and it can be considered to limit less. But the holiday we want to make pure object-oriented programming, then you must build your own structure, in order to get the same convenience in the other OOP language. You need to add the new categories we have to use, but also use other incompatible interfaces. It is naturally necessary to pay additional energy to bring new interfaces with their own design (possibly need multiple inheritance). Do you have such a price to get this cost for C extra "flexibility"? Of course, if you really need - if you have already been C expert, if you have an advice to C, it is really worth it. But if you are a newbie, the first contact such design, like a replacement scheme like Java, may save more effort.
All objects in a single structure (such as all Java objects) guarantee that there are some specific functions. In your own system, we know some basic operations for each object. A single structure, plus all objects are created in the memory, which can greatly simplify the passage of the parameters (this is a complex concept in C ).
Using a single structure, we can make it easier to implement a garbage collector. The necessary support related to this can be installed in the base class, while the garbage collector can send the appropriate message to any object within the system. If there is no such single structure, and the system can manipulate the object through a handle, the way to achieve the garbage collector is very different, and it will face many obstacles.
Since the type of runtime is affirmed in all objects, it will never encounter a case where it is not judged. This is particularly important for system-level operations, such as violation control; and get greater flexibility in programming.
But everyone may also have questions. Since you say the benefits so the sky, why does C use single structure? In fact, this is a result of early efficiency and control. Single structure will bring some restrictions on programming. Moreover, more importantly, it increases the difficulty of new procedures and original C code. Although these restrictions only cause problems only in a particular occasion, in order to obtain the maximum flexibility, C finally decided to give up the practice of single structure. And Java does not have the above problems, it is a language that is new design, does not have to maintain so-called "backward compatibility" with existing languages. So naturally, like other major object-oriented programming languages, single structure is soon implemented in Java's design. 1.7.3 Collection Library and Easy Use Collection
Since the collection is a tool we often use, a collection library is very necessary, it should be easily reused. In this way, we can easily take a variety of collections and insert them into your own procedure. Java offers such a library, although it is very limited in Java 1.0 and 1.1 (Java 1.2's collection library is undoubtedly a masterpiece).
Drop-style and template / versatility
In order to make these sets can be reused, or "regenerate", Java provides a general type, which previously called "Object". Single structure means that everything is an object in the end of the root.
To use such a collection, simply add the object handle pointing to it, you can reuse the object by the handle. However, since the collection can only accommodate Object, when we add an object handle to the collection, it will trace the Object, which lost its identity or identification information. When using it again, you will get an Object handle, not to the type of handle we have earlier. So how can I return its original appearance, call the useful interface of the object that earlier in first,?
Here, we use it again. But this time is not traceable in a hierarchical structure into a more "universal" type. Instead, discontinuation is a more type of "special". This type of styling is called "Downcasting". For example, we know that when the shape is traced, the Circle is a type of Shape (geometric shape), so the upper traceability is safe. But we don't know that an Object is Circle or Shape, so it is difficult to ensure the safety of the discrete, unless it knows what you have to operate.
But this is not absolutely dangerous, because if you are traceable, you will get an running error we call "illegal" (Exception). We will explain this later. But when you extract the object handle from a collection, you must use some way to accurately remember what they are in order to ensure the correct shape.
Drop-style and runtime tests require additional time to run procedures, and programmers must pay additional energy. In this case, can we create a "smart" collection, let it know the type of yourself? This is necessary to eliminate the necessary and potential errors. The answer is certain, we can use "parameterization type", which is class that can be customized automatically and can cooperate with specific types. For example, by using a parameterized set, the compiler can customize the collection to only accept Shape and only the Shape is extracted.
The parameterization type is an important part of C , which is C no single structure. In C , the keyword for implementing the parameterized type is Template. Java has not yet provided parametric types because it is a bit awkward because of the use of a single structure. But this does not guarantee that the later version will not be implemented, because the word "generic" has been implemented by Java "(in the ADA language," generic "is used to implement its template). This keyword reservation mechanism taken by Java is actually often impressed, it is difficult to determine what will happen. 1.7.4 Difficulties in clear: Who is responsible for clear?
Each object requires resources to "survive", where the most striking resource is memory. If you no longer need to use an object, you must clear it to release these resources so that other objects are used. If you want to solve very simple questions, how to clean an object is not very prominent: We create an object, call it when you need it, then clear or "destroy". But on the other hand, the problems we usually encounter are often more complicated than this.
For example, suppose we want to design a system, use it to manage air traffic in an airport (the same model may also be suitable for managing containers in a warehouse, or a set of zip rental systems, or pet stores This first looks like it is very simple: constructing a collection to accommodate the plane, then create a new aircraft, put it into a collection. All the aircraft entering the air traffic control area is treated so. As for the clearance, leave in a plane This area can simply delete it.
But things are not as simple, there may also need another system to record data related to the aircraft. Of course, the main functions of the controller are different, and the importance of these data may not be revealed at the beginning. For example, this record reflects the flight plan of all small aircrafts leaving the airport. So we got another collection of small aircraft. Once an aircraft object is created, if it is a small plane, then it must also put it into this collection. The objects in this collection are then subject to some background processing in the system free period.
The problem is now more complicated: How can I know when to delete an object? After using the object, some other parts of the system may still have to play. The same problem will also appear in other places, and in the programming system (such as C ), it must be deleted after using an object, so the problem will become abnormal (note 6).
6: Note that this is only established on the object created in the memory (created with the new command). But on the other hand, it requires objects to create in memory stacks on the other hand and all other common programming issues.
In Java, the garbage collector has taken into account the release problem of memory (although this does not include clearing other aspects involved). The garbage collector "knows" when an object is no longer used, then automatically releases the memory space occupied by the object. In this way, the facts that are inherited from a single root type Object, and because we can only create objects in one way in memory stacks, Java programming is much easier than C . We only need to make a small amount of choices, you can overcome the original existing obstacles.
1. Influence of garbage collector on efficiency and flexibility
Since this is such a good means, why didn't you get full in C ? Of course, we must pay a certain amount of convenience for this programming, the price is the cost period overhead. As mentioned earlier, in C , we can create an object in the stack. In this case, the object is automatically cleared (but does not have flexibility to create objects during operation). Creating an object in the stack is the most effective way to assign a storage space for an object, but also to release those rooms most effective. Creating an object in the HEAP may have to pay much more expensive. If you always inherit from the same basic class, all function calls have the "homogeneous" feature, so inevitably need to pay a certain price. But garbage collectors are a special problem because we can never determine when it starts or spends more time. This means that there is an incoherent factor during the execution of the Java program. So in some special occasions, we must avoid it - such as the execution of a program must remain stable, while calling them as "real-time programs", although not all real-time programming issues must be Requirements - Note 7). 7: According to some technical readers of this book, there is a ready-made real-time Java system (www.newmonics.com), it is indeed possible to ensure the efficacy of the garbage collector.
The designer of the C language once issued a request to the C programmer (but also very successful), do not want to add any features that may have an impact on C speed or use anywhere in C, not to use C . This purpose is reached, but the price is C programming inevitably complicated. Java is simpler than C , but the cost of pay is efficient and a certain degree of flexibility. But for most programming problems, Java is undoubtedly our preferred.
1.8 violation control: solve the error
Starting from the oldest programming language, error control has always been a big problem that designers need to solve. Since it is difficult to design a set of perfect error control schemes, many languages simply ignore problems, pass it to the library designers. For most error control schemes, the most important problem is that they have seriously dependent on programmers, rather than relying on the language itself. If the programmer is not alert enough - if it is more in a hurry, this is almost certain-that the error control scheme depends on the program will be invalid.
"Violation Control" sets the error control scheme to the programming language, sometimes even built into the operating system. The "illegal" (Exception) belongs to a special object, which will "throw" or "throw" from the generated place. Subsequently, this violation will be designed to control the "violation controller" capture for specific types of errors. When the situation becomes unsatisfactory, there may be several violation controllers to capture the corresponding violation objects in parallel. Since the use of a separate executive path, we will not interfere with our conventional execution code. This makes the writing of code easier because you don't have to force the code. In addition, a violation of "throw" is different from the error value returned from the function, and is different from a flag set by the function. The role of those error values or logos is to indicate an error status, which is negligible. But the violation cannot be ignored, so it will definitely get an arrangement in some place. Finally, the use of violations can reliably recover from a bad environment. At this point, you can generally don't need to exit, we can take some processing and the regular execution of the recovery program. Obviously, the program prepared is more reliable.
Java's violation control mechanism is different from most programming languages. Because in Java, the violation control module is packaged from the beginning, so it must be used! If you don't write some code to correctly control your violation, you will get a compile time error message. This ensures the coherence of the program, making the error control easier. Note that violation control is not object-oriented, although in an object-oriented programming language, the violation is usually represented by an object. As early as an object-oriented language, violation control already exists.
1.9 multi-threaded
In computer programming, a basic concept is to control multiple tasks simultaneously. Many programming problems require the program to stop the work of the hand, change to other problems, return to the main process. This goal can be achieved by a variety of ways. At the beginning, those programmers who have the lower level of the machine have written some "interrupt service routines", and the pause of the main process is implemented by the hardware level interrupt. Although this is a useful method, it is difficult to transplant, resulting in a high cost of another class.
Sometimes, it is necessary to interrupt it for those tasks that are very real-time. However, there are many other problems that only require the problem into independently running program, so that the entire program can respond more quickly to the user's request. In a program, these independently running pieces are called "Thread", which is called "multi-threaded processing" using the concept it programmed. A common example of multi-threading process is the user interface. With threads, users can press the next button, then the program will respond immediately, rather than let the user wait for the program to start responding in the current task.
At the beginning, the thread is only a tool for processing a single processor. However, if the operating system itself supports multiple processors, each thread can be assigned to a different processor, truly enter the "parallel operation" state. From the perspective of programming language, one of the most valuable features of multi-threaded operation is that the programmer does not have to care how many processors have been used. The program is split into several threads in the logical sense; if the machine itself has multiple processors installed, the program will run faster and do not require any special adjustment.
According to the previous discussion, everyone may feel that the thread is very simple. But you must pay attention to a question: share resources! If there are multiple threads run simultaneously, and they try to access the same resources, they will encounter a problem. For example, the two processes cannot send information to a printer at the same time. To solve this problem, for those that can be shared (such as printers), they must enter the lock state during use. So a thread can lock the resource. After completing its task, unlock (release) this lock so that other threads can then use the same resources.
The multi-threaded mechanism of Java has been built into the language, which makes a more complex problem simple. Support for multi-threaded tension is supported at the level of object, so an execution thread can be expressed as an object. Java also provides a limited resource lock scheme. It can lock any object occupied by any object (the memory is actually one of a variety of shared resources), so there is only one thread to use a specific memory space at the same time. For this purpose, you need to use the synchronized keyword. Other types of resources must be explicitly locked by programmers, which usually requires programmers to create an object, with it represents a lock, all threads must check this lock when accessing that resource.
1.10 permanent
After creating an object, as long as we need, it will always exist. However, when the program ends run, the "survival" of the object will also declare the end. Although this phenomenon is very reasonable, it will find that in depth, it will find that the object can continue to exist after the program stops, and can retain all of its information, then in some cases a very valuable Best. When the program is started next, the object is still there, and the information reserved inside is still the information of the program last time. Of course, you can write information into a file or database to achieve the same effect. But although all things can be seen as an object, if the object can be declared into "permanent", it is undoubtedly a quite convenient thing to look at all other details. Java 1.1 provides support for "limited permanent", which means that we can simply save the object to disk, and can be retrieved any time in the future. The reason why it is "limited" is because we still need to make a call, and the preservation and retrieval of objects are made. These work cannot be carried out automatically. In the future version of Java, it is expected to be more comprehensive for "permanent" support.
1.11 Java and Internet
Since Java is not another type of programming language, everyone may be strange why it is worth such attention, why is there so many people think it is a milestone for computer programming? If you are from a traditional programming background, then the answer is not very obvious when you start. In addition to solving traditional programming problems, Java can resolve programming issues on World Wide Web (World Wide Web).
1.11.1 What is web?
The word Web just started to appear, it seems to "surf", "online existence" and "homepage" and so on. There is even a "Internet Syndrome" statement, questioning many people's fanatic Internet behavior. We must make some in-depth discussions here, but before this, we must understand the concept of client / server systems, which is a computational field that is full of confusing issues.
1. Client / Server Calculation
The basic idea of the client / server system is that we can centrally store information resources in a unified place. The data is typically stored in a database, and the information is delivered to each other according to the request of other people or machines. A key to the client / server overview is that the information is "centrally stored". So we can easily change the information and then distribute the modified information to the consumer of information. Connect various elements to together, information warehouses, software for delivery information, and the machine where information and software are located, they are called "Server". For those that reside on the remote machine, they need to communicate with the server, retrieve information, perform appropriate processing, and then display it on the remote machine, which is called "client".
In this way, the basic concept of client / server is not complicated. One of the main issues you should pay attention to here is that a single server needs to serve multiple customers. In this mechanism, a set of database management systems is usually minored to encapsulate data layout into the form to achieve optimal use. In addition, the system often allows customers to insert new information into a server. This means that it is necessary to ensure that customers' new data will not conflict with other customers, or need to ensure that those data will not be lost (using the terminology of the database, this is called "transaction"). After the customer software changes, they must build, debug and install on the client machine. All of this will make problems more complicated than we generally imagine. In addition, support for multiple types of computers and operating systems is also a big problem. Finally, performance problems are especially important: hundreds of customers may have a request to the server at the same time. So any tiny delay cannot be ignored. In order to alleviate the latent problems, the programmer needs to be cautiously dispersed. Generally consider allowing client affordable partial processing tasks, but sometimes it can also be assigned to other machines where the server is located, and those machines are also called "middleware" (middleware is also used to improve the maintenance of the system). Therefore, when implementation, other people release information such a simple concept may become abnormal. Sometimes it will even make people feel completely unable to start feelings. The concept of client / server can be bigger. In fact, approximately half of the programming activities can use the structure of the client / server. Such systems can be responsible for transactions with handling orders and credit cards, until all aspects of all types of data - stock market, scientific research, government operation, etc. In the past, we generally take separate solutions for separate issues; a new solution is designed each time. These programs are more difficult to create or use, users must learn and adapt to the new interface each time. Client / server issues need to be simply changed!
2. Web is a huge server
The Web is actually a set of huge client / server systems. But it is more complicated because all servers and customers are existing on a single network. But we don't have to know more details, because the only thing to care is to establish the same server connection with it, and deal with it (even if you may have to search the correct server within the world).
At the beginning, this is a simple one-way operation. We send a request to a server, return to us a file, due to this machine's browser software ("Customer" or "Customer") is responsible for explaining and formatting, and correctly displaying on the screen in front of us come out. But people will not be satisfied with only from one server. They want to get a complete client / server capability, allowing customers (programs) to feed back some information to the server. For example, I want to retrieve the database on the server, add new information to the server, or the next order, etc. (this also provides a higher security requirement than the previous system). In the development of the Web, we can clearly see that these people will change.
The development of a web browser finally took an important step: a message can be displayed on any type of computer, no change. However, the browser is still very primitive, and there is a bit of a strong heart in front of the user's rapid increase in users. Their interaction ability is not strong, and the server and the Internet have caused a certain degree of interference. This is because each time you take some requirements programmed operation, you must feed back the information back to the server and processed at one end of the server. Therefore, it is entirely necessary to wait a few seconds or minutes to find that I have just spell a word. Since the browser is just a purely viewing program, even the simplest computing task cannot be performed (of course on the other hand, it is very secure, because any program cannot be performed on this machine, avoiding program errors or viruses Harassment). In order to solve this problem, people have taken many different ways. At the beginning, people have improved the graphics standard to make the browser to display better animations and videos. In order to solve the remaining problems, the only way is to run the program in the client (browser). This is called "client programming", which is a very important extension of traditional "server-side programming".
1.11.2 Client Programming (Notes 8)
The Web initially adopted "Server-Browser" scheme provides interactive content, but this interaction ability is fully provided by the server, which has brought a small burden to the server and the Internet. The server typically generates a static web page for the client browser, which is simply explained by the latter and displayed. The basic HTML language provides a simple data collection mechanism: text input box, check box, single tab, list, drop-down list, etc., there is another button, can only reset the data in the form by the program to return data Give the server. The information submitted by the user is back to the server through all the "General Gateway Interface" (CGI) supported by all web servers. The text included in the submitted data indicates how CGI is operated. The most common action is to run a program located in the server. That program is generally saved in a directory called "cgi-bin" (when pressing a button in the web page, note the address window at the top of the browser, often discover the words "cgi-bin"). Most languages can be used to compile these programs, but the most common is Perl. This is because Perl is designed for text processing and interpretation, so it can be installed and used on any server, no matter what the processor or operating system is used.
8: This section adapts an article from a certain author. That article appeared in the mainspring located at www.mainspring.com. This section adopts the consent of the other party.
Many of the Web sites today are strictly established on the basis of CGI, in fact, almost everything can be done with CGI. The only problem is the response time. The response of the CGI program depends on how much data is required, and the burden on both the server and the Internet has multiple (and the CGI program is starting). The early designers of the Web did not expect that the more than enough bandwidth became unique, this is exactly the result of a lot of money. For example, any form of dynamic graphic display is hardly displayed, because a GIF file must be created at this time, and each change of the graph is passed to the customer. And everyone should have a profound experience on the data validation on the input form. The original method is to press the submit button (Submit) on the web page; data back to the server; the server launches a CGI program, check if the user input is wrong; format an HTML page, notify the possible mistakes, and will This page returns to us; then you must go back to the original table, and then enter it again. This method is not only very slow, but also very cumbersome.
The solution is to design the client's programming. Most of the machines running a web browser have a strong ability to do other work. At the same time, the original static HTML method can still be adopted, it will wait until the server will go back to the next page. Client programming means that a web browser can get more use and effectively improve the interaction of the web server (interactive) capabilities. Discussions on client programming and discussions in regular programming have nothing great. The parameters used are definitely the same, but the platform is running different: the web browser is like a limited operating system. In any case, we still need to program, still encounter a lot of problems in the client program, and there are many solutions to solve. In the remaining parts of this section, we will summarize these issues and introduce the countermeasures taken in client programming.
Plugin
When the client programming is moving forward, the most important question is the design of the plugin. With plug-ins, programmers can easily add new features to the browser, users only need to download some code, "insert" the appropriate location of the browser. The role of these codes is to tell the browser "From now on, you can do these new events" (only to download these inserts once). Some fast and powerful behaviors are added to the browser through plugins. But the plugin is written is not a simple task. When we build a specific site, it may not want to involve this work. For client programming, the value of the plugin is that it allows professional programmers to design a new language and add the language to the browser, and do not have to pass the browser original person. It can be seen that the plugin is actually a "back door" of the browser, allowing to create a new client programming language (although not all languages are implemented as plugins).
2. Scripting language
The plugin causes the explosive growth of script preparation language. With this scripting language, the source code for your own client program can be inserted directly into the HTML page, and the plugin explained to the language will be automatically activated when the HTML page is displayed. Scripting language is generally tended to simplify as much as possible and easy to understand. And because they are some simple text from the HTML page, just issue a request to the server to be loaded very quickly. The disadvantage is that our code is exposed to people. On the other hand, this problem can be placed on only because it is usually necessary to make over-complicated things with a script.
The scripting language is truly directed by a specific type of problem, which mainly involves how to create richer, more interactive graphical user interface (GUI). However, the scripting language may solve 80% of the client programming. The problem you encountered may be completely in that 80%. And because the purpose of the script is to simplify and quickly, before considering other more complex solutions (such as Java and ActiveX), you should first try the script language is feasible.
The most discussed scripting language includes JavaScript (which does not have any relationship with Java; the reason why the name is completely a market strategy), VBScript (similar to Visual Basic) and TCL / TK (from the popular cross Platform GUI constructing language). Of course there are many other languages, and there are many developments.
JavaScript may be used, and its support is also the most comprehensive. Regardless of NetScapenavigator, Microsoft Internet Explorer, or Opera, support for JavaScript is available. In addition, the books that describe JavaScript on the market are much more than the books that tell other languages. Some tools can also automatically generate web pages with JavaScript. Of course, if you already have Visual Basic or TCL / TK's deep skills, of course, you should use them simple, and at least you can avoid the troubles of learning new languages (Solving the Web problem is already enough). 3. Java
If a script is written, it can solve the 80% client programming problem, then what about the remaining 20%? Do they belong to some difficult problems? The most popular program is currently Java. It is not only a powerful, highly secure, can be used across platforms and internationally versatile programming languages, but also a language with strong vitality. The extension of Java is continuously carried out, the provided language features and libraries can solve problems in traditional languages, such as multi-threaded operation, database access, network programming, and distributed calculations. Java has subtly solved the client programming with "Applet).
The program (or "small application") is a very small program that can only be run in a web browser. As part of the web page, the block code will be downloaded back (this is similar to the image in the web page). After activating the server, it will execute a program. One advantage of the program is reflected in: Once the program is required, once the user needs the customer software, the software can automatically download it from the server. They can automatically obtain the latest version of the customer software, will not be wrong, and have not reinstalled trouble. Due to Java's design principle, programmers only need to create a program, which can run in almost all computers and the browser installed in the Java interpreter. Since Java is a full-featured programming language, we can do as much job in the client before sending a request to the server. For example, no need to transfer a request form via the Internet, and then determine if there is a spell or other parameter error. Most data checks can be done at the client, no need to sit in front of the computer anxiously waiting for the server response. In this way, not only the sensitivity of speed and response is greatly improved, the burden on the network and the server can also be significantly reduced, which is critical to the smooth flow of the Internet.
Another advantage of the Java block is another advantage of the Java block in comparative compilation, so the client does not see the source code. Of course, on the other hand, the anti-compilation Java program is not difficult, and the code hidden is generally not an important issue. Everyone should pay attention to the other two important issues. As this book, the compiled Java block may contain many modules, so many "hit" (access) servers to download (in Java 1.1, this problem has been effectively improved - Using Java compressed files, JAR files - it allows designers to encapsulate all necessary modules together for users to deliver. On the other hand, the scripker is integrated into the web page as part of the text of the web page. This approach is generally very small, which can effectively reduce the number of hits to the server. Another factor is the problem of learning. No matter what you usually listen to others, Java is not a language that is very easy to learn. If you were previously a Visual Basic programmer, then Turning VBScript would be the fastest solution. Since VBScript can solve most of the typical client / server problems, once the hand is up, it is difficult to make determined to learn Java. If the language is more familiar with the script, then before turning to Java, it is recommended to familiarize yourself with JavaScript or VBScript because they may already be able to meet your needs and don't have to experience the hardships of learning Java. 4. ActiveX
To a certain extent, a strong competitor of Java should be Microsoft ActiveX, although it is a completely different set of implementation mechanisms. ActiveX is the earliest plan for pure Windows. After a separate professional association, Activex has now had a cross-platform ability. In fact, ActiveX means "If your program is connected to its working environment, it can enter the web page, and run in the ActiveX browser" (IE cured support for ActiveX, and Netscape requires one Plugin). So, ActiveX does not limit our specific language. For example, suppose we are already an experienced Windows programmer, you can use the language like C , Visual Basic or BorlandDelphi, so you can create an ActiveX component almost no learning. In fact, ActiveX is the best way to use the History Lefts in our web page.
5. Safety
Automatic download and through the Internet running program sounds like a virus manufacturer's dream. In the client's programming, Activex brings the most headache security issues. When you click on a Web site, you may pass any number of things with the HTML page: GIF file, script code, compiled Java code, and ActiveX components. Some are harmless; the GIF file will not cause any harm to us, and the scripting language usually has a lot of restrictions on what they can do. Java is also designed to run in a safe "sandbox" in its block, which prevents operations from being operated outside the sandbox or memory area.
ActiveX is the most worried thing in all of these. Writing a program with ActiveX is like a Windows application - you can do anything you want to do. After downloading an ActiveX component, it is entirely possible to damage the files on our disk. Of course, for those downloads that are not limited to internal to Web browsers, they can also destroy our system. The virus downloaded from BBS has always been a big problem, but the speed of the Internet makes this problem more complicated.
The current solution is "Digital Sign", the code will be verified by the authority, showing who it is the author. The basis of this mechanism is that the reason why the virus will spread, because its preparatory is anonymous. Therefore, if you remove anonymous factors, all designers have to be responsible for their behavior. This seems to be a good idea because it makes the program more regular. But I have a skeptical attitude toward it to eliminate malicious factors, because if a program contains BUG, then it will also cause problems. Java will prevent these problems through the "sandbox". The Java interpreter is embedded in our local web browser, and all suspected instructions are checked when the program is loaded. In particular, the player does not have power to write the file into the disk, or delete the file (this is one of the most likely something that the virus is most). We usually believe that the program is safe. And because security is critical to the construction of a reliable client / server system, all errors that will leave vulnerabilities to the virus can be repaired (the browser software actually needs to be forced to comply with these safety rules; and some browsers Let us choose different levels of security to prevent access to different levels of accesses.
Everyone may doubt whether this restriction will hinder us to write the file to a local disk. For example, we sometimes need to build a local database, or save the data so that you will be offline. The earliest version seems to be any sensitive things online, but this will become very unreal (although the low-cost "Internet Tool" may meet the needs of most users). The solution is "Signed a named program", which verifies that the program is indeed comes from it with a public key encryption algorithm. Of course, after the verification, a program that signed the name can still start to clear your disk. But in theory, since it is now able to find the creation of "accounting", they generally don't do this kind of stupid thing. Java 1.1 provides a framework for digital signatures, allowing a program to "walk" to the exterior of the sandbox when necessary.
Digital signatures have missed an important issue, that is, the speed of moving on the Internet. Load back a wrong program, and it is very unfortunate to really do some stupid things, how long does it take to find this? This may be a few days, it may be a few weeks later. After discovering, how to track the procedures of the accident (how big it is at the time)?
6. Internet and Inline Network
The web is a common solution to the client / server problem, so it is best to solve some "subset" of such problems with the same technique, especially traditional client / server issues within the company. For traditional client / server patterns, the problem we face is to have a variety of different types of customer computers, and it is difficult to install new customer software. But through web browsers and client programming, these two types of problems can be solved very well. If an information network is limited to a specific company, it can be referred to as "intranet" after applying Web technology to it, so as to show your international "Internet" (Internet). The inline network provides a larger level than the Internet because it physically controls the use of the company's internal servers. Speaking of training, generally, as long as people understand the routine concept of the browser, they can easily grasp the differences between web pages and blocks, so the cost of learning the new system will be greatly reduced.
Security issues Introducing us into a client programming, a branch that seems to be automatically formed. If the program is running on the Internet, because there is no knowledge of knowing it, it is necessary to pay special attention when programming, and prevent possible programming errors. There is a need for some cross-platform processing, as well as appropriate security, such as a certain scripting language or Java.
But if you run in the Inland Network, some restrictive factors faced will change. All machines are all usual things for Intel / Windows platform. In the Inland Network, you need to be responsible for your own code. And once you find an error, you can correct it immediately. In addition, there may already have some "historical legacy" code, and use those code with more traditional client / server. But when upgrading, you must physically install a client program each time. Waste the time on the upgrade installation is an important reason for transferring to the browser. After using the browser, the upgrade becomes easy, and the entire process is transparent and automatically. If it is true to involve such an intranet, the most sensible method is to use ActiveX, rather than trying to use a new language to overwriting the program code. The best solution is to make an investment / return analysis first when we are confused by client programming issues. Please summary all the constraints of the problem, and what is the fastest program. Since the client programming still needs to be programmed, it should be taken to take the best development path in anyway. This is the best posture we can make when you are facing some inevitable problems in the development of procedures.
1.11.3 Server programming
Our entire discussion ignores the problem of server-side programming. What happens if a request is issued to the server? Most of the time request is a very simple "send this file to me." The browser will then explain this file in the appropriate form: as an HTML page, a picture, a Java block, a scriptor, and more. More complex requests from the server generally involve operational (transaction). The most common is to issue a database retrieval command. After getting the result, the server will format it into an HTML page and come back as a result (of course, if the customer has higher intelligence through Java or some scripting language, then the original Data can send and format in the client; so speed can be faster, and the burden on the server can be reduced. In addition, sometimes you need to register your own name in the database (for example, when you join a group), or send a order to the server, which involves modifications to that database. Such server requests must be performed by some of the code from the server, we call it "Server-end program". In the conventional sense, server-side programming is performed with Perl and CGI scripts, but more complex systems have appeared. These include Java-based web servers that allow us to program all server-side programming with Java, which is called "SERVLET".
1.11.4 A independent area: application
Most debates related to Java are related to the program. Java is actually a conventional procedure design language that solves any type of problem, at least in theory. And as long as it is pointed, most client / server issues can be solved in a more efficient manner. If you turn your sight from the program (simultaneously relaxing some limits, such as forbidden writing discs, etc.), you have entered the wide area of the application of conventional uses. This application can run independently, no browser, just like a normal executor. Here, Java features not only reacting in its graft ability, also reflected in the programming itself. As mentioned throughout the book, Java provides a number of useful features that enable us to create a more robust program that is more robust than the previous programming language in a shorter time.
But pay attention to anything is not perfect, we have to pay some costs for this. The most obvious is that the execution speed is slow (although it can be adjusted for this). Like any language, Java itself has some restrictions, making it not very suitable for solving certain special programming issues. But in any case, Java is a language that is growing rapidly. As each new version is released, it becomes more and more cute, and the problem that can be fully solved has become more and more. 1.12 analysis and design
Object-oriented paradigm is a new way to think about programming, and many people have a different way, and many people will have a brow on how to construct a project. In fact, we can make a "good" design that makes full use of all the advantages provided by OOP.
Most of the books related to OOP analysis and design are not satisfactory. Most of these books are full of inexplicable words, clumsy writes, and many statements that seem to be very important (annotation ⑨). I think this book is preferably compressed to a chapter, and I write to a very thin book. Ironing is that those who specially focus on complex things management often write some shallow and understand the book! If you can't be said simple and direct, there is not much man who likes to watch this. After all, all the purposes of OOP is to make the process of software development easier. Although this may affect the livelihoods that like to solve complex problems, why not make things simple from the beginning? Therefore, I hope that I can start a good foundation for everyone, and use a few paragraphs to clearly analyze and design problems.
⑨: The best entry is still the "Object-Oriented Design Withapplications, 2nd Version 2", Wiely & Sons published in 1996. This book is very depth, and it is easy to understand, although his mark method is unnecessarily complicated to most design.
1.12.1 Do not lose
The most important thing throughout the development process is: don't lose your own! But in fact, this kind of thing is easy. Most methods are designed to solve the problem of the maximum range. Of course, there are some special difficulties, and the authors need to pay more hard efforts or pay more. However, most projects are comparable "General", so they can generally make successful analysis and design, and only a small number of methods recommended. But no matter how limited, some forms of processing are always beneficial, which makes it easier for the development of the entire project, and it is better than the beginning of the code!
That is, if you are looking at a special method, there is a lot of details, and many steps and documents are recommended, so it is still difficult to correctly determine when you stop. Always remind yourself to pay attention to the following questions:
(1) What is the object? (How to divide your project into a series of separate components?)
(2) What is their interfaces? (What news is needed to send to every object?)
After determining the objects and their interfaces, you can do a program. For the consideration of many reasons, it may also need more instructions and documents than this, but the information required to master is absolutely not less than this.
The entire process can be divided into four phases, and the stage 0 has just begun to use certain forms of structures.
1.12.2 Stage 0: Pulling a plan
The first step is to determine which steps take in the later process. This seems to be very simple (in fact, everything we say here seems to be very simple), but a common situation is: Some people have not even entered the stage 1, so they are busy start writing code. If your plan is "directly starting coding", it is certainly unambiguous (if there is a thorough understanding of the problems you have to solve, you can think about it). But the lowest level should also agree to have a plan. At this stage, it may be necessary to decide some necessary additional processing structures. But very unfortunately, some programmers like to write the program, they like to think, they think "naturally will be completed when they are completed." In this way, don't have any problems, but I feel that if you can set a few flags throughout the process, or "road sign" will be more beneficial to you. This is probably much better than simply for "completing work". At least, after another goal, after passing one of the road signs, you can have a clear grasp of your progress, and the strength will also increase accordingly, and it will not generate the feeling of "roadmaking."
The seat I just started to learn the story. (I think one day can write this novel), I have been adhering to this practice, I feel like it is simple to let the text "stream" to the paper. When I wrote something related to the computer, I found that the structure is much more simpler than the novel, so there is no need to consider too many questions. But I still have developed the structure of the whole writing, so that I have to write something to write. Therefore, even if your plan is to start writing procedures, you still need to experience the following phases, and put forward some specific issues to yourself.
1.12.3 Stage 1: What should I do?
In the previous generation program design (ie "process or programming design"), this stage is called "establishing a demand analysis and system specifications". Of course, those operations are no longer needed today, or at least the form is changed. A large number of headache documentation has become history. But the original intention is good. The meaning of demand analysis is to "establish a range of rules. If it is determined by it, when it is done, and how the customer can satisfy." System specification means "Here is some specific instructions, let you know what the program needs to do (not what) can meet the requirements." Demand analysis is actually a contract between you and customers (even if the customer works within the company, or other objects and systems). The system specification is an disclosure of the highest level of the problem, which we determine if the task is completed and takes more time. Since these are all the unanimous consent of the participant, I recommend simplifying them as much as possible - it is best to use the list and basic charts in the form of saving time. It may also face other restrictions, you need to expand them into larger documents.
We must pay special attention to the core issue of this phase, do not entangle the end of the sub-branches. This core problem is: what system is decided. For this problem, the most valuable tool is a collection called "Use Condition". This is the most convincing answer for those in the form of "how to do it?", " For example, "If the customer needs to extract a cash check, but there is no so many cash reserves, then how do you respond to the ATM?" For this issue, "Use Condition" can indicate that the ATM is in that kind of "condition" Correct operation below.
It should be summarized as a complete set of "use conditions" or "application". Once this is completed, it is equivalent to finding the core task that wants to complete the system. Since it is focused on "use conditions", a good effect is that they can always put them in the most critical thing, and prevent themselves from distinguishing them on other things that are not big to complete the task. That is, as long as you have a complete set of "use conditions", you can make a clear description of your own system and transfer to the next phase. At this stage, it is also possible to fully grasp the various applications of the system in the future, but this has no relationship. As long as it is willing to spend time, all problems will naturally be exposed. Don't pay more about the "perfect" specifications of the system, otherwise it is easy to produce frustration and coke emotions. At this stage, it is best to use several simple paragraphs to describe your own system, then expand around them, add some "noun" and "verbs". "Noun" naturally became an object, and "verb" naturally becomes a "method" to be integrated into the object interface. As long as you try to do it, you will find how useful tools; sometimes, it can help you complete the vast majority of work.
Although it is still in the primary stage, some schedules at this time may also be used very well. We should now have a more comprehensive understanding of things that you have to build, so it may have felt that it will spend how long it will do. At this point, we must consider a large factor: if you estimate a longer schedule, then the company may decide no longer continue; or a supervisor has estimated this project to spend more time, and will try to affect you Estimate. But in any case, it is best to draft a "honest" schedule from the beginning, and then make some temporary difficult decisions. There are many technologies to help us calculate accurate schedule (just like techniques that predict stock market landing), but usually the best ways still rely on their own experience and intuition (don't forget, I don't want to build in experience) . I feel about it takes more time, and then doubles this time, plus 10%. Your feelings may be correct; "Maybe" can be done in that time. But "Double" makes that time, "10%" time is used to carry out the final scrutiny and deepening. But at the same time, it is necessary to make an appropriate explanation to the superior supervisor, regardless of the complaints and modifications of the other party, as long as it tells them: such a schedule, just one of my estimates!
1.12.4 Stage 2: How to build?
At this stage, a set of design must be taken and explain what the various objects contained in appearance are like, and how to communicate with each other. At this point, you can consider using a special chart tool: "Unified Modeling Language" (UML). Please download a UML specification to http://www.rational.com. As the description tool in the first phase, UML is also very helpful. In addition, it is also possible to handle some charts (such as flowcharts) in the second phase. Of course, it is not necessary to use UML, but it will be helpful to you, especially when you want to draw a detailed chart, so many people study together. In addition to UML, you can also choose to describe the object and their interfaces (just like I said in "Thinking In C ", but this method is very primitive, and the role is also limited.
I have had a very successful consultation experience, which involves a group of people's initial design. They have not yet constructed OOP (object-oriented programming) projects, draw objects on the whiteboard. We talk about how objects communicate with each other (communication) and delete some of them, and replace another object. This group (they know what is the purpose of this project) actually has developed design; they "own" design, rather than letting design naturally reveal. What I did there is to guide design, put forward some appropriate issues, try to make some assumptions and get feedback from the group to modify those assumptions. The most wonderful thing in this process is that the entire group is not an object-oriented design by learning some abstract examples, but through a real design to master OOP tricks, and that design is the work in his hand. ! After making an explanation of the object and their interfaces, it has completed the work of the 2nd stage. Of course, these work may not be complete. Some work may wait until the phase 3 can be known. But this is enough. We really need to care about all objects. It can be found earlier, but OOP provides a sufficiently perfect structure, and it is not too late to find it.
1.12.5 stage 3: Started
Reading this book may be a programmer, and now it is the stage you may be most interested. Since there is a plan on the hand - how brief it is, it has mastered the correct design structure before formal encoding, it will find that the next work will be much more simpler than the beginning of the start. And this is exactly what we want to achieve. Let the code do what we want to do, this is the ultimate goal of all program projects. But don't worry, otherwise you only have to get lost. According to my experience, I finally took out a more comprehensive plan to make it as much as possible to meet as much as possible. Give me the feeling of programming is more art, can't just look at the technology. All payments will eventually be rewarded. As a real programmer, this is not a good quality. Comprehensive thinking, careful preparation, good structure not only makes the program more easily and debug, but also makes it more easy to understand and maintain, but it is a necessary condition for a software profit.
After building a good system, after running, you must conduct actual inspections. The needs analysis and system specifications you have made can be used in the previously do. Fully examine your own procedures, and it is determined that all the requirements proposed are met. It seems to be over now? is it?
1.12.6 Stage 4: Califies
In fact, the entire development cycle has not ended yet, and now it is a stage called "maintenance" in the traditional sense. "Maintenance" is a more embarrassment, which can be used to express from "Run it according to the way to operate", "Joining the customer, the function of the declaration of the declaration" or more traditional "All bugs" and so on. . Therefore, everyone has a lot of misunderstandings about the word "maintenance", and some people think that things that need "maintenance" must not be good, or have a defect! Because this term means that you actually build a very "original" program, you need to make changes frequently, add new code or prevent it from backward, degrade. Therefore, we need to continue working with a more reasonable word.
This word is "reform". In other words, "The first thing you have done is not perfect, so you need to leave a deep study, a cognitive space, and then go back to make some changes." For problems to be solved, with the more deep depth of learning and understanding, it may be necessary to make a lot of changes. One of the powers of doing these work is to optimize the reform, and finally be rewarded from their own efforts, regardless of this requires a shorter or longer period.
When is it called "to achieve the ideal state"? This does not only mean that the program must work as required, and can adapt to various designated "use conditions", which also means that the internal structure of the code should be perfect. At least, we should feel that the entire structure can be well coordinated. Without a clumsy grammar, there is no bloated object, and there is no other thing. In addition, it is necessary to ensure that the program structure has strong vitality. Due to many reasons, the changes to procedural changes are essential. However, it must be determined that the change can be easily and clear. There is no shortage here. Not only do you need to understand what you build, but also understand how the procedure continues to evolve. Fortunately, object-oriented programming languages are especially suitable for such continuous modifications - the boundaries established by the object can effectively ensure the overall structure of the structure, and prevent unrelated objects that have unrelated objects, destruction. You can also do some intense changes to your own procedures, and do not destroy the overall performance of the program, and will not affect other code. In fact, the support of "Calibration" is a very important feature of OOP. Through the correction, you can create things that are close to your own ideas. Then observe your own work from the whole, and compare it to your own requirements. Then you can go back and go back, redesign and re-implement inappropriate part of the program (annotation ⑩). Before you finally get a proper set of programs, you may need to solve some problems that cannot be avoided, or at least one aspect of the problem. Moreover, it is generally necessary to "reform" several times ("Design Paradigm" can play a lot here. For discussion, please refer to Chapter 16 of this book).
When building a system, "the school" is almost inevitable. We need to constantly compare our needs and understand whether the system needs it actually. Sometimes only actually see the system, you can realize that you need to solve a different problem. If you think this form of the school will inevitably happen, then it is best to take out your own first version as soon as possible, check if it is hopeful, so that your mind continues to mature.
Repeated "Calibration" related to "incremental development" related to the secret relationship. Increasing development means first starting from the core of the system, implementing it as a framework, and then gradually establishing the remaining parts of the system on this framework. Subsequently, the various functions (characteristics) that are ready to be provided will be added one. The most test skill here is that the racks set a framework that can easily expand all target features (for this issue, you can refer to Chapter 16). The advantage of this is that once the core frameworks work, each feature to join is like a small project in itself, not a part of the big project. In addition, new features in development or maintenance phase can be added more conveniently. OOP provides support for incremental development because, if the program is designed, each increment can be a perfect object or object group.
⑩: This is a bit similar to "fast shape". At this point, you should focus on building a simple, clear version, so that you can have a clear grasp of the system. Then throw this prototype and officially build one. One of the trouble's most troublesome is that people do not throw away prototypes, but they are built directly on it. If you add the lack of "structure" in the programming design, it will cause a confusing system to increase maintenance costs.
1.12.7 render
If you don't have a careful design, it is certainly impossible to build a house. If the establishment is a dog house, although the design map can not be so detailed, there is still some sketch to do it in your heart. Software development is completely different, and its "design map" must be complete and complete. For a long time, people do not have much structure during their development, but those large projects will easily fail. Through continuous exploration, people have mastered the number of structures and details. But their use is to make people feel the hanging bile - it seems to have the need to spend most of their time on writing documents, and there is not much time to program (often this). I hope that everything you can tell for everyone can provide a compromised road. It is necessary to take a method that best suits yourself (and habits). No matter how small the plan is developed, some forms of plan will greatly improve your project compared to the complete plan. Remember: According to estimates, there is no more than 50% of the projects that will fail! 1.13 Java or C ?
Java is particularly like C ; thereby naturally a conclusion: C seems to be replaced by Java. But I have some questions about this logic. In any case, there are still some characteristics of C to be Java. And although there is already a large guarantee, claiming Java will reach or exceed C by one day. But this breakthrough has not been achieved so far (although Java's speed is indeed steadily, it has not reached the speed of C ). In addition, many areas have many C enthusiasts, so I don't think that the language will soon be replaced by another language (the power of lovers is neglected. For example, in my hosted " Advanced Java Seminar, Allen Holub claims that two most common languages are REXX and COBOL).
I feel that the power of Java is reflected in the field with C . C is a language that is absolutely not trying to cater to a mold. In particular, its form can vary multiple ends to address different types of problems. This is mainly reflected in the tools like Microsoft Visual C and Borland C Builder (I like this). They synthesize the library, component model, and code generation tools together to develop end-user applications (for Microsoft Windows operating systems). But on the other hand, what is the most commonly used by Windows developers? It is Microsoft's Visual Basic (VB). Of course, we are here to temporarily not mention VB's grammar to confuse the fact - even if a program is only a few pages, it is difficult to manage. From the perspective of language design, although VB is successful and popular, there is still a lot of shortcomings. It is best to have a powerful function and ease of use of VB simultaneously, and do not generate a code that is difficult to manage. And this is the most attractive place in Java: as a "next-generation VB". No matter what you feel after you hear this claim, please think about it anyway: People have done a lot of work to Java, so that it can facilitate programmers to solve the application level (such as connected network and cross-platform UI, etc.) Therefore, it is essentially allowing people to create very large and flexible code mains. At the same time, considering Java also has the most "robust" type inspection and error control system that I have not yet seen in any other language, Java does greatly improve our programming efficiency. This is not doubtful!
But do you change C to Java for your own specific project? In addition to the web block, there are two problems that need to be considered. First, if you want to use a large number of existing libraries (this must increase a lot of efficiency), or have a solid C or C code base, then replaced with Java, it reflects the development schedule, not accelerating it. speed. But if you want to build your own code from your head, Java's simple easier can effectively shorten the development time. The biggest problem is speed. In the original Java interpreter, the explanation of Java will slow 20 to 50 times higher than C. Despite long-term development, this speed has a certain degree of improvement, but it is still very disparied with C. The computer is most payingable to speed; if you can't work more quickly on a computer, it is not as good as hand (some people suggest that Java is used during development to shorten the development time. Then use a tool and support library. Convert to C , so you can get a faster execution speed).
In order for Java applications to most web development projects, the key is the improvement in speed. At this point, it is necessary to use the compiler called "Just-In Time, or JIT), even considering a lower code compiler (both written, there are two adventures). Of course, the low-level code compiler will make compiled programs cannot be executed cross-platform, but it also brings up speed improvement. This speed is even close to C and C . Moreover, the program cross-compilation in Java should be much simpler than C and C (theoretically requires recoiling, but it is still more difficult to implement; other languages have made a similar guarantee).