java-based (a) - Object Oriented

Foreword

java is an object-oriented language, before learning java, you need to understand the concept of object-oriented, process-oriented language such distinguished c, object-oriented features, like what has it? First on a mind map it:

Object-oriented mind map


Objects

What is the object

Elements in the problem space and their representations in the solution space of

This sentence excerpt from java programming ideas to speak, is a difficult to understand (really summarize Gangster summary is not an ordinary abstract). So what is the problem space? What is the solution space?

  1. Space problems: the problem space is a problem solved by the entire state of knowledge of a problem to achieve, it is made to solve the problem by using the information contained in the problem and information have been actively stored configuration. Generally it includes three parts:

    • Initial state - incomplete information or unsatisfactory situation at the beginning,
    • Target state - you want to get the information or status or that the target state
    • Operation - steps to move towards the target state from the initial state, you might take

    If you take this approach to analog contents, i.e. the initial state into the reference, or target state is a result of the return value, the operation method thereof it is.

  2. In the solution space: emmmm not find this particular explanation, but the explanation solvability space, should be similar: Homogeneous Solution Set of n linear equations S = {x | Ax = 0 } is a vector space, called homogeneous solution space of linear equations. The result is equivalent to the problem set.
    Rough idea of complete explanation, seemingly still not well understood by the individual's words: problem space is actually our problem, the problem is what conditions, the process of operation, and the state after the problem is solved. Elements of the problem space can be an object. Objects may be characteristic of a country with an independent entity. The solution space is the result set. The problem is the result of an object. Simply put, in fact, a physical object can be described, an entity described can be an object

Five basic characteristics of the object

  • Everything is objects (that is, it is possible to describe the entity can be an object)
  • Program is a collection of objects, they are told what to do with each other by sending messages (sending messages is actually calling a method, or notice)
  • Each object has its own memory and other objects consisting of (constituting the object may be a combination of other objects, or of themselves what combination of characteristics)
  • Each object has its type: the object has its own vested marked, such as it belongs to the kind of objects, human or other categories can be described. Each object is an instance of its home marked
  • All objects of a particular type can receive the same information (that is, the development of external communication the way they are consistent)

That's all concept of the object, the object over interpretation, it would have to explain the ownership of an object under the label, called java in this class

What is the class

In the above-mentioned each object has its own type, which is referred to in the java classes, class having the same characteristics (data elements and behavior) of the set of objects

  • Data elements: as we generally characterized in that the object has those properties described, such as the length and width of the rectangle.
  • Behavior: is a general term approach, if we compare an individual subject, the method is what an individual can do, with the behavior

Explains the concept of the object above, but we mainly explain object-oriented, object-oriented at all to explain the contents of it.

Object-Oriented

What is object-oriented

Object-oriented thinking can be seen as a call to include a variety of independent and objects of each other's ideas in the program. And process-oriented approach different, we can be understood as a process-oriented assembly line-like operation, and we are now similar machine operation, corresponding to a particular process, the development of specific methods. And different object-oriented, object-oriented object-based, that is to say by the object of mutual exchange, collaboration is done to solve the problem, the equivalent of our workers to do some work, they have their own skills that can be applied to different scene, cross-matching to achieve.

Object-oriented and process-oriented difference
At first glance looks like all the same, nothing more than one more object-oriented maintenance man multi-concept, yes, the maintenance man is actually an object, the object-oriented implementation process are encapsulated into the object, by the behavior of the object or property solving issues. Is classified as a maintenance man, maintenance man is different specific performance. For the scene, the process will not change, the change is more detail, in the process-oriented, the right change is the modification process, and object-oriented is to modify their behavior, like the behavior of the object can fit process. It has a strong flexibility and maintainability

Three characteristics of object-oriented

  • Hide all packages can be hidden details, exposed only the most simple act. That communication party does not need to know the parties communicate only need to know how to communicate, which is implemented in terms of the communicating party is transparent to the communicating parties in java, we usually only exposed api class, but it does not tell is how to achieve, for the caller, the implementation details are transparent.

  • Inheritance refers to inheritance ability, is a class derived from the process of this new class , a new class can be constructed on the basis of the existing class. In java: new classes created by the inheritance we referred to as a subclass / derived class; and inherited class is called the parent class / base class. Object is the parent of all classes. Another point java, is a single inheritance model.

  • Polymorphism is the ability to have different manifestations of the same behavior of living forms, is a manifestation of a variety of performance capabilities of the object. Polymorphic base is inherited, allowing the child object parent object given different capacity. Polymorphic forming conditions are:

    1. Inherit the base class
    2. Rewrite
    3. References to parent child class object

    In java associated with the polymorphism is rewritten: redefine behavior by subclasses of the parent class, which is the condition 2. And it is not overloaded, overload behavior is the same plurality of internal class, a diversity of different parameters. And polymorphism does not matter.

The five principles of object-oriented

SOLID 5 Acronym object-oriented principle, namely: a single principle, the principle of opening the closure, Richter substitution principle, the interface segregation principle, Dependency Inversion Principle. While most of us in the preparation of the program are less likely to remember these principles, these principles should not be used on programming, but in the design process it. Accurate, this is a basic principle of optimization.

  • A single principle

Refers to a certain class of functions, responsibilities to a single, can not be all-inclusive.

That is enough for the class to be refined, clear enough for behavior, isolation unwanted behavior.

  • Open Closed Principle

A module in scalability should be open, and in the change of the above should be closed.

This fact is well understood, for example, we need to add a feature (not scalable) in one module, and the new features and functions are very similar to the one part, under normal circumstances, we modify the previous interface, more written in the form of a special sentence, in fact, we can solve the problem, but this way do not meet the open closed principle. We should not change at the top is open, the logic before the changes, but should open a new interface extensions, and the same place in two parts abstracted.

  • Richter substitution principle

Subclasses can appear anywhere in the parent class can appear, and place it in place of the phrase is the parent can use a subclass can replace it.

  • Dependency Inversion Principle

    Entity must rely on the abstract rather than the concrete realization

This is not very good understanding, and another way of saying:

  1. High-level modules should not depend on low-level implementation
  2. High-level modules should rely on abstract

emmm more difficult to understand the simplest terms, it is that the program should rely on abstract interface, rather than relying on the specific implementation. Take a very simple example, we declared at the time of the code object, whether all XXX x = new XXX (), in fact, this is dependent on the specific implementation, because the back of the pending XXX is the specific class, It should be kind of how? Here is necessary to use the abstract factory pattern design mode (note the abstract Oh, because they do not depend on the specific implementation), using the factory pattern instead of a direct statement like this do have a big advantage, once one day to be like XXX replaced, and that before the installation, we are not trying to place all the XXX category where? Omission like this can easily occur. The use of the abstract factory pattern, in fact, as long as we realize that the interior of the place like a factory pattern, if you want to replace the factory model, but also a direct replacement for the factory model to achieve just fine (for the thing is so abstract factory pattern a), this program has a good scalability and flexibility.

  • Interface Segregation Principle

Split very bloated interface into smaller and more specific interfaces

Personal understanding of it, this is equivalent to the principle of a single interface, the interface is the responsibility to refine enough, single function, like this does not need to rely on the consumer to realize it is not the method used by the

to sum up

Just like that described in general content is over, this chapter describes the basic object-oriented thinking part of the object inside, talked about the concept of full-text, good boring Yo.

Welcome attention to my blog -FightCrap

Guess you like

Origin juejin.im/post/5cffa5556fb9a07eb74b33a5