Chapter 11. Encapsulating Objects

In This Chapter

  • Understanding the Model-View-Controller pattern

  • The role of interfaces

  • How composite objects work

  • Factoring your code to implement Model-View-Controller

Using encapsulation enables you to safely tuck data behind an object's walls. You can keep the data safe and reduce the dependencies of other parts of your program on what the data is and how it is structured.

Encapsulation is also useful when you apply it to application functionality. When you limit what your objects know about other objects in your application, changing objects or their functionality becomes much easier because it reduces the impact of those changes on the rest of your application.

In this chapter, I'll show you a way to design, or architect, your application that limits the knowledge that objects have of other objects.

Getting to Know the Model-View-Controller (MVC) Pattern

The Cocoa framework you'll use on the Mac is designed around certain programming paradigms, known as design patterns — a commonly used template that gives you a consistent way to get a particular task done.

While you'll need to be comfortable with several design patterns in Cocoa, there is one that implements the kind of object encapsulation that reduces the impact of changes to an application — the Model-View-Controller (MVC) design pattern. This design pattern is not unique to Cocoa — a version of it has been in use since the early days of Smalltalk (which the Objective-C extensions to the C language ...

Get Objective-C® For Dummies® 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.