iOS开发中的设计模式/Design patterns in iOS programming

MVC模式 MVC pattern

从通信的角度看,iOS的MVC更接近MVP模式:因为View和Model之间不通信。

关于MVC与MVP的文章

iOS开发中的设计模式/Design patterns in iOS programming_第1张图片
苹果官网

iOS开发中的设计模式/Design patterns in iOS programming_第2张图片
斯坦福大学公开课ppt

原型模式 Prototype pattern

  • objc/runtime.h:
  struct objc_class {
             Class isa  OBJC_ISA_AVAILABILITY;

      #if !__OBJC2__
          Class super_class                                        OBJC2_UNAVAILABLE;
          const char *name                                         OBJC2_UNAVAILABLE;
          long version                                             OBJC2_UNAVAILABLE;
          long info                                                OBJC2_UNAVAILABLE;
          long instance_size                                       OBJC2_UNAVAILABLE;
          struct objc_ivar_list *ivars                             OBJC2_UNAVAILABLE;
          struct objc_method_list **methodLists                    OBJC2_UNAVAILABLE;
          struct objc_cache *cache                                 OBJC2_UNAVAILABLE;
          struct objc_protocol_list *protocols                     OBJC2_UNAVAILABLE;
      #endif

      } OBJC2_UNAVAILABLE;

适配器模式 Adapter pattern

  • AFNetWorking

工厂模式 Factory pattern

  • UIButton:
  + (instancetype)buttonWithType:(UIButtonType)buttonType;
  • NSArray:
  + (instancetype)array;

单例模式 Singleton pattern

  • NSUserDefaults:
  @property (class, readonly, strong) NSUserDefaults *standardUserDefaults;
  • NSNotificationCenter:
  @property (class, readonly, strong) NSNotificationCenter *defaultCenter;

观察者模式 Observer pattern

  • KVO:
  - (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;
  • NSNotificationCenter:
  - (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;

代理模式 Proxy pattern

  • UITableView:
  @property (nonatomic, weak, nullable) id  delegate;

装饰模式 Decorate pattern

  • objc/runtime.h:

    void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
    
  • Category

职责链模式 Chain-of-responsibility pattern

  • UIResponder:

    - (nullable UIResponder*)nextResponder;
    - (BOOL)canBecomeFirstResponder; 
    

迭代器模式 Iterater pattern

  • NSEnumerator:

    - (nullable ObjectType)nextObject;
    
  • NSArray:

    - (void)enumerateObjectsUsingBlock:(void (^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;
    

你可能感兴趣的:(iOS开发中的设计模式/Design patterns in iOS programming)