"Beauty of Design Patterns" 2 Object-oriented (2)

Posted Jun 5, 20205 min read

6. Comparison between object-oriented and process-oriented

6.1 What is process-oriented programming
  • Process-oriented programming is also a programming paradigm or programming style. It takes the process(can be understood as methods, functions, operations) as the basic unit of organization code, and the separation of data(can be understood as member variables, attributes) and methods as the main feature. The process-oriented style is a process-oriented programming style, which can complete a function by manipulating a set of sequentially executed methods to manipulate data
6.2 Advantages of object-oriented than process-oriented

*oop is more able to deal with large-scale complex program development

  • oop style code is easier to reuse, extend and maintain
  • oop language is more humanized, more advanced and more intelligent(assembly language, high-level language)
  • Extended
  • Problem:Object-oriented is easier to deal with the development of large-scale complex programs than process-oriented. But complex systems such as Unix and Linux are also developed based on the C-oriented process-oriented programming language. What do you think of this phenomenon?
  • Answer:The program written in any programming language must be implemented by the CPU one by one instruction(whether it is interpreted by the virtual machine or compiled directly into machine code), and the CPU cannot see what is used. Program written in language. For all programming languages, the ultimate goal is two:Improve the operating efficiency of hardware and improve the development efficiency of programmers. However, it is difficult to have both.
    The C language is almost perfect in terms of efficiency. It is more suitable for mining the value of hardware. For example, the C language uses array char a[8], which becomes(base address + offset) after compilation. For the CPU, no operation is faster than addition, and the algorithm complexity of its execution efficiency is O(1). From the aspect of execution efficiency, C language is an excellent choice for developing operating systems and low-level programs close to the hardware.
    The problems caused by the C language are memory out of bounds, wild pointers, memory leaks, etc. `It only cares whether the program is flying high or not, it doesn t care whether the program is flying or not. In order to understand off-programmers and improve development efficiency, more smart programming languages such as OOP were designed, but the ease of development stems from the layer-by-layer packaging of the bottom layer after all. There are more intermediate links to complete a specific operation, occupying more memory space, and occupying more CPU operations. From this perspective, OOP, a high-level language, is popular because hardware is getting cheaper.
6.4 What seem to be object-oriented actual process-oriented code
  • Misunderstanding object-oriented programming, always thinking that all the code is stuffed into the class, naturally it is object-oriented programming
  • Three examples to illustrate
    1. Abuse of getter and setter methods, which violates the characteristics of object-oriented encapsulation
  • Correct idea:When designing an implementation class, unless really needed, try not to define setter methods for properties. In addition, although the getter method is safer than the setter method, if the collection container is returned, the risk of the data within the collection being modified must also be prevented.
  • Demo shopping cart
public class ShoppingCart {
  private int itemsCount;//Number of products
  private double totalPrice;//Commodity total price
  private List<ShoppingCartItem> items = new ArrayList<>();//Product list

  public int getItemsCount() {
    return this.itemsCount;
  //Unreasonable 1:itemsCount is a private property, but the public setter method is set, and the value can be freely modified externally through the setter method
  public void setItemsCount(int itemsCount) {
    this.itemsCount = itemsCount;

  public double getTotalPrice() {
    return this.totalPrice;

  public void setTotalPrice(double totalPrice) {
    this.totalPrice = totalPrice;
  //Unreasonable 2:Although the property can set the getter method, but here returns the list collection container, external calls can still modify the data inside
  //For example:ShoppingCart cart = new ShoppCart();cart.getItems.clear();//Empty the shopping cart
  //Although the need to clear the shopping cart needs to be said, the business logic of clearing the shopping cart should not be exposed to the upper layer code. The correct approach should be to define the clear() method in the class and encapsulate the clearing logic in the method
  //If you want to view the contents of the shopping cart, the Collections.unmodifiableList() method provided by Java allows the getter method to return an unmodifiableList collection container that cannot be modified
  public List<ShoppingCartItem> getItems() {
    return this.items;

  public void addItem(ShoppingCartItem item) {
    totalPrice += item.getPrice();
  //... omit other methods...
    1. Abuse of global variables and global methods
  • Global variables:
  • Static member variable(it belongs to the data on the class and is shared by all instantiated objects, so it is also a global variable)
  • constant(such as MySQL configuration parameters, etc.)
  • Singleton class object(since there is only one copy of the global code of the singleton class object, it is equivalent to a global variable)
  • Global method:Static method
  • Correct idea:For global variables and global methods, we try our best to be able to single responsibilities and define some detailed small classes, such as redis and mysql, which are defined separately, rather than defining a large and complete one. In addition, if the attributes and methods in these classes can be divided and merged into other business classes(that is, they must be split out), then it is best. It can greatly improve the cohesion and code of the class. Reusability.
    1. Define data and methods Separate classes
  • The data is defined in one class, the method is defined in another class, the typical is the MVC pattern currently used in web development, and it is also a typical process-oriented development
  • Correct idea:add in later
6.5 In object-oriented programming, why is it easy to write procedure-oriented code
  • In life, when you complete a task, you generally think about what you should do first, then what you should do, how to perform a series of operations step by step, and finally complete the entire task. "Procedure-oriented programming style is exactly in line with this flow of thinking of people."
  • The object-oriented programming style is just the opposite. It is a "bottom-up way of thinking". It does not first decompose tasks according to the execution process, but translates the tasks into small modules(that is, classes) one by one, designs the interaction between the classes, and finally assembles the classes according to the process to complete the entire task.