西维蜀黍

【iOS】iOS内存管理

之前接手公司的一个iOS SDK项目,是用MRC进行内存管理的。最终,我将这个项目迁移成了ARC。

  ...


【Design Pattern】Behavioural - Memento

1 动机

备忘录模式(Memento Pattern),又称为快照模式(Snapshot Pattern),为对象提供了恢复到先前状态的能力(通过回滚(rollback))。

  ...


【Design Pattern】Creational - Singleton

单例模式(Singleton Pattern)

动机

在涉及同步问题的日志模块、缓存模块、多线程或线程池设计过程中,对于系统中的某些类来说,只有一个实例很重要。

  ...


【Design Pattern】Creational - Builder

When to Use It

  • When an object has many optional fields or configurations.
  • When object construction involves complex steps or validation.
  • When you want to avoid constructor telescoping (i.e., multiple constructors with different parameters).

定义

建造者模式(Builder Pattern)将一个复杂对象的构建与它的表示分离,使得同样的构建过程(但传入的参数不同)可以创建不同的表示。

构成

The Builder pattern typically involves:

  1. Product – the complex object being built.
  2. Builder – an interface or abstract class defining building steps.
  3. ConcreteBuilder – implements the Builder, maintaining the product.
  4. Director (optional) – controls the building sequence.

Java Example

public class House {
    private int rooms;
    private int floors;
    private boolean hasGarage;
    private boolean hasSwimmingPool;

    private House(Builder builder) {
        this.rooms = builder.rooms;
        this.floors = builder.floors;
        this.hasGarage = builder.hasGarage;
        this.hasSwimmingPool = builder.hasSwimmingPool;
    }

    public static class Builder {
        private int rooms;
        private int floors;
        private boolean hasGarage;
        private boolean hasSwimmingPool;

        public Builder rooms(int rooms) {
            this.rooms = rooms;
            return this;
        }

        public Builder floors(int floors) {
            this.floors = floors;
            return this;
        }

        public Builder hasGarage(boolean hasGarage) {
            this.hasGarage = hasGarage;
            return this;
        }

        public Builder hasSwimmingPool(boolean hasSwimmingPool) {
            this.hasSwimmingPool = hasSwimmingPool;
            return this;
        }

        public House build() {
            return new House(this);
        }
    }
}

// usage
House myHouse = new House.Builder()
    .rooms(4)
    .floors(2)
    .hasGarage(true)
    .hasSwimmingPool(false)
    .build();

Pros and Cons

✅ Pros

  ...


【Design Pattern】Creational - Factory

工厂模式具体包括三种:

  • 简单工厂模式(Simple Factory Pattern)/静态工厂方法模式(Static Factory Method Pattern)
  • 工厂方法模式(Factory Method Pattern)
  • 抽象工厂模式(Abstruct Factory Pattern)
  ...


【Design Pattern】Behavioural - State

动机

  • 一个对象可能会拥有不同的状态,且可在运行时(runtime)在这几个状态之间切换;
  • 当这个对象与外界交互时,他的状态就可能发生变化,并在不同状态之间切换;
  • 当在状态变化时,对象可表现出不同的行为(以体现状态变化后的影响)。
  ...


【Design Pattern】设计模式

分类

  ...


【Markdown】Markdown中的数学符号与公式

Reference

http://web.ift.uib.no/Teori/KURS/WRK/TeX/symALL.html

https://oeis.org/wiki/List_of_LaTeX_mathematical_symbols

http://jzqt.github.io/2015/06/30/Markdown%E4%B8%AD%E5%86%99%E6%95%B0%E5%AD%A6%E5%85%AC%E5%BC%8F/

https://blog.csdn.net/Katherine_hsr/article/details/79179622

https://www.jianshu.com/p/7c34f5099b7e

  ...


【Linux】命令 - time

由于自己使用macOS,因此以下所有测试都macOS下进行。而macOS中的time与Linux中的time命令是几乎几乎完全相同的。

  ...


【Operating System】I/O - 同步、异步与阻塞、非阻塞I/O问题

软硬件层面的同步/异步

硬件

在现代操作系统中,I/O是一种与外围设备(peripherals)进行数据交换的方式,I/O包括读或写数据到磁盘/SSD中,或通过网络发送/接收数据,显示信息到显示器上,接收鼠标或键盘的输入。

因此,我们通常说的I/O,不仅仅只限于磁盘文件的读写。*nix将计算机抽象了一番,磁盘文件、硬件、套接字等几乎所有计算机资源都被抽象为了文件。

实现硬件层面异步的机制称为硬件中断(hardware interrupt)。

在典型的硬件**同步(synchronous)**场景中,当CPU请求外围设备读取数据时,CPU会进入一个无限循环(infinite loop),在循环中CPU需要不断去检查外围设备是否已经将数据读取到了,这个过程称为轮询(poll)。

而在现代硬件中,当CPU向外围设备发送I/O请求后,就会立刻去执行其他CPU指令(而不是不断的轮询)。当外围设备将数据准备完成后,它会通过电路中断(circuit interrupt)向CPU发送一个信号(signal)。这就是典型的**硬件异步(asynchronous)**场景。因此,CPU就不再需要静静的等待且不断轮询直到外围设备将数据准备完成,因而大大的提高了CPU的利用率。

软件

操作系统作为一层中间件,抽象了硬件设备,向应用程序以系统调用(System Call)的形式提供I/O操作服务。

类似地,当应用程序(通过调用系统调用)发起一个I/O操作后,操作系统会调用对应的设备驱动(device driver)以操作硬件进行这个I/O操作。

对于这个应用程序来说,如果此时需要静静的等待(或者不断的轮询),直到数据被准备完成后,这就是软件层面的同步,这样的编程方式称之为同步编程(synchronous programming)。

如果操作系统允许应用程序声明一个回调函数(callback),且当I/O操作完成后,操作系统会自动调用这个回调函数,这就是软件层面的异步,这样的编程方式称为异步编程(asynchronous programming)。

  ...