gogoWebsite

What is Object-Oriented

Updated to 6 months ago

Article Catalog

    • Object-Oriented (OOP) Overview
    • The Difference Between Process-Oriented and Object-Oriented
    • Three Object-Oriented Features and Five Principles
      • The three main characteristics of object orientation
      • The five basic principles
    • summarize

Object-Oriented (OOP) Overview

Ever since we started with the language Java, we have been told that it is an object oriented language. The most talked about is new an object, in fact, do not know what is an object. I thought that an object is a class, and I didn't think there was anything special about it. Until a long time later, the interviewer asked what is OOP, the mouth is a big O, OOP?WTF?The man said in disgust is object-oriented programming. I said: Java is object-oriented, that is, everything to the object as a carrier, to program, to face. Interviewer: go out ! now!

img

rolled back I quickly look at what is OOP, Object Oriented Programming, the original is object-oriented programming ah, and OOD (object-oriented design), OOA (object-oriented analysis). What is object-oriented? To understand this issue we need to first understand the process-oriented, so that we can understand the contrast.

A long, long time ago programming was process-oriented, such as the implementation of an arithmetic operation 1 + 1 = 2, through this simple algorithm can solve the problem. But with the progress of the times, people are not satisfied with the existing algorithm, because the problem is more and more complex, not 1 + 1 so simple, such as a class of students in the data analysis, so there is the concept of object, all things are objects. Abstract the real thing, pay attention to the abstraction of the word is the focus of ah, the real-life things as well as the relationship, abstracted into a class, through the inheritance, realization, combination of all things are accommodated. The abstraction and mathematical modeling of the real world is achieved. This is a leap forward.

img

Let's take the simplest example to differentiate between process-oriented and object-oriented.

What do you do one day when you're craving fish and chips? You have two choices.

1, buy their own materials, meat, fish and meat seasoning, garlic moss, carrots and so on and then cut vegetables and meat, open fried, served on a plate.

2. Go to a restaurant, open your mouth: Boss! Come to a share of fish flavor shredded pork!

See the difference? 1 is process oriented and 2 is object oriented.

What are the advantages of being object-oriented? First of all you don't need to know how the fish and meat threads are made, reducing coupling. If you suddenly don't want to eat shredded pork with fish and want to eat Luo Yang cabbage, for 1 you may not be too easy, and you need to re-buy the vegetables, seasonings and so on. For 2, it's too easy to yell, "Boss! Let's replace that fish-flavored shredded pork with luoyang cabbage, improves maintainability. Overall it's lower coupling and higher maintainability!

Process-oriented is concrete and flow-oriented, to solve a problem, you need to analyze it step by step and implement it step by step.

Object-oriented is modeled, you just need to abstract a class, which is a closed box, where you have the data and also the method to solve the problem. What functions you need to use directly on it, do not have to go step by step to achieve, as for how this function is realized, what do we care? We will use on it.

The bottom of the object-oriented is actually still process-oriented, process-oriented abstraction into a class, and then encapsulated, easy for us to use is object-oriented.

The Difference Between Process-Oriented and Object-Oriented

process-oriented

Pros: performance is better than object-oriented, because the class needs to be instantiated when it is called, the overhead is higher and more resource-consuming.
Cons: not easy to maintain, not easy to reuse, not easy to extend

object-oriented

Advantages: easy to maintain, easy to reuse, easy to extend, due to object-oriented encapsulation, inheritance, polymorphism, you can design a low-coupling system, so that the system is more flexible and easier to maintain.
Cons: worse performance than process-oriented

Three Object-Oriented Features and Five Principles

img

The three main characteristics of object orientation

1、Packaging
Hiding object properties and implementation details and providing only public access to the outside world isolates changes for ease of use and improves reusability and security.

2. Inheritance
Improve code reusability; inheritance is a prerequisite for polymorphism.

3. Polymorphism
Reference variables defined by a parent class or interface can point to instance objects of a subclass or concrete implementation class. The expandability of the program is improved.

The five basic principles

  • Single Responsibility Principle (SRP)
    Classes should be single-function, not all-encompassing, like a grocery store.

  • Open-Close Principle (OCP)
    A module is open for expansion and closed for modification, wanting to add functionality is warmly welcomed, wanting to modify, hmmm, 10,000 not happy.

  • The Liskov Substitution Principle LSP
    A subclass can replace the parent class to appear anywhere the parent class can appear. For example, you can represent your dad to work at your grandma's house. Haha~~

  • The principle of inversion of relianceDIP(the Dependency Inversion Principle DIP)
    Higher level modules should not depend on lower level modules, they should all depend on the abstraction. Abstractions should not depend on concrete implementations, concrete implementations should depend on abstractions. That is, you have to say you are Chinese when you go abroad, not which village you are from. Let's say Chinese is abstract, and there are concrete xx province, xx city, xx county underneath. The abstraction you have to rely on is Chinese people, not that you are from xx village.

  • The Interface Segregation Principle ISP (the Interface Segregation Principle ISP)
    It is better to design with multiple interfaces related to a specific client class rather than one generic interface. For example, if a cell phone has features such as making phone calls, watching videos, playing games, etc., it is better to split these features into different interfaces rather than in one interface.

summarize

  • Abstraction will make complex problems simpler.
  • From being a process-oriented executor, he has become a commanding officer with his mouth open.
  • Object-oriented is more in line with human thinking, process-oriented is machine thinking