简体   繁体   English

iOS中的设计模式作为装饰模式

[英]Design pattern in iOS as decorator pattern

I have a question about design patterns in iOS. 我对iOS中的设计模式有疑问。

I have a UIView with a xib file. 我有一个带有xib文件的UIView。 We can consider this view as a view-template. 我们可以将此视图视为视图模板。

Now we can consider several views (subclass, for example) that specify the behavior of the view template (different methods, animation, etc.). 现在我们可以考虑几个视图(例如,子类),它们指定视图模板的行为(不同的方法,动画等)。

Is there something that may be fine in my case? 在我的情况下,有什么可能是好的吗? Some design pattern? 一些设计模式? For example, the Decorator design pattern? 例如,Decorator设计模式?

The Decorator Design Pattern 装饰设计模式

The Decorator design pattern attaches additional responsibilities to an object dynamically. Decorator设计模式动态地将附加职责附加到对象。 Decorators provide a flexible alternative to subclassing for extending functionality. 装饰器为子类化提供了灵活的替代扩展功能。 As does subclassing, adaptation of the Decorator pattern allows you to incorporate new behavior without modifying existing code. 与子类化一样,修饰器模式的调整允许您在不修改现有代码的情况下合并新行为。 Decorators wrap an object of the class whose behavior they extend. 装饰器包装类的对象,它们的行为扩展。 They implement the same interface as the object they wrap and add their own behavior either before or after delegating a task to the wrapped object. 它们实现与它们包装的对象相同的接口,并在将任务委派给包装对象之前或之后添加它们自己的行为。 The Decorator pattern expresses the design principle that classes should be open to extension but closed to modification. Decorator模式表达了这样的设计原则:类应该对扩展开放但是对于修改是封闭的。

There are several patterns related to polymorphism that can be used within Swift, but two key ones you'll see often are the Decorator and Adapter patterns. 有几种与多态相关的模式可以在Swift中使用,但是你经常会看到的两个关键模式是Decorator和Adapter模式。 These are implemented using the language keywords extension and protocol respectively. 这些分别使用语言关键字扩展和协议来实现。

The primary example of the Decorator pattern in Swift is when you create an extension. Swift中Decorator模式的主要示例是创建扩展时。 In Objective-C, there is a similar mechanism with class categories. 在Objective-C中,类别的机制类似。

iOS Design Pattern iOS设计模式

http://en.wikipedia.org/wiki/Decorator_pattern http://en.wikipedia.org/wiki/Decorator_pattern

http://zubairraihan.blogspot.in/2013/02/decorator-design-pattern-in-objective-c.html http://zubairraihan.blogspot.in/2013/02/decorator-design-pattern-in-objective-c.html

http://techrantnz.blogspot.in/2011/08/decorator-builder-design-pattern-in.html http://techrantnz.blogspot.in/2011/08/decorator-builder-design-pattern-in.html

intro-object-oriented-design-swift-part-2 面向向内-对象设计-迅速部分-2-

Decorator Pattern is good for this. 装饰模式对此有好处。 You can Decorate a UIView to have a scroll indicator and decorate this to a text view and so on... Extend the view by subclassing. 您可以装饰UIView以具有滚动指示器并将其装饰到文本视图等等...通过子类扩展视图。

In your case, You can mark the Custom Class type of the View (attribute inspector) in you XIB to point to the subclass which has this behavior. 在您的情况下,您可以在XIB中标记View(属性检查器)的Custom Class类型,以指向具有此行为的子类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM