Chapter 2. Programming to Interfaces


In This Chapter

Defining Interfaces 34

Using Polymorphism 38

Summary 42

Learning and mastering the fundamentals of object-oriented design and application development is no small feat, and you should undoubtedly congratulate yourself for all your accomplishments so far. And as you know, with every step taken yet another step presents itself. Our next step involves understanding interfaces in the context of good application design and pattern-based development. Although mastering basic object-oriented concepts enables an evolution in how you build applications, understanding interfaces can rapidly push your coding to take another evolutionary leap. After you've learned about using interfaces, the code you write may hardly resemble the code you used to write.

It may surprise you to learn that lurking within every class are at least two distinct layers which we call the interface and the implementation. Early on when you're learning how to write classes, it can be difficult to distinguish between the two layers. However, as you become more adept at writing good classes, it becomes easy to differentiate between the interface and the implementation.

Classes consist of methods and properties. As explained in the previous chapter, properties should never be defined as public. Properties are used by a class to store its state, and good encapsulation dictates that an instance of a class should always be responsible for managing its own state. As such, the only public interface that most classes define is comprised of methods. Looking at a class from the outside, you could describe it by its public methods and their signatures. In fact, you could even say that two classes look identical from the outside if they have the same method names and method signatures. In contrast, those two classes with the same method names and signatures could have vastly different implementations for those methods. This is the basic idea that enables something called polymorphism. Polymorphism is the idea that if you program to an interface rather than an implementation, the resultant code is much more flexible because any instance of any class that uses that interface can stand in for any other object that uses the same interfaceeven if the implementations are totally different.

We'll talk much more about interfaces, implementation, polymorphism, and all sorts of related topics throughout this chapter.




Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net