21. Prototype

A Prototype is an object that is copied to implement application features. In particular, copying an existing object often provides more flexibility than allocating and initializing new instances from scratch. The Prototype pattern avoids the need to hardcode relationships between objects. For example, Cocoa’s NSMatrix class displays a grid of objects. As rows and columns are added to a matrix, more objects are created as needed to fill in every grid position. NSMatrix wouldn’t be very flexible if it only worked with one kind of object. To provide flexibility, NSMatrix allows you to specify a prototype object that NSMatrix copies as often as necessary to fill in the grid. If you provide a prototype button, you get a grid of buttons. ...

Get Cocoa Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.