<< Chapter < Page Chapter >> Page >

The methods in the interface should control access to, or provide a pathway to the private instance variables.

Not bound to the implementation

The interface should be generic in that it is not bound to any particular implementation. Hence, the class author should be able to change theimplementation without affecting the using programs so long as the interface doesn't change.

In practice, this means that the signatures of the interface methods should not change, and that the interface methods and their arguments should continueto have the same meaning.

Preview

In this lesson, I will present and briefly explain a program with an output that consists of a Flex user interface containing two custom component objectsas shown in Figure 1. Each of the objects is intended to represent a single multiple-choice question in an online test. (See Creating Online Tests using Custom ActionScript Components here for a detailed explanation of the code.)

Screen output for encapsulation01.

Missing image.
Screen output for Encapsulation01.

The two question objects in Figure 1 have the same behavior. And as you will see later, the classes from which they were instantiated have the same userinterfaces. However, the classes are implemented in significantly different ways.

Discussion and sample code

Will discuss in fragments

I will discuss the code in this lesson in fragments. Listing 1 shows the mxml code that instantiates the two component objects shown in Figure 1.A complete listing of the file named Encapsulation01.mxml is provided in Listing 12 near the end of the lesson.

Mxml code to instantiate two custom component objects.

<!--The following code instantiates an object of the class named QuizA for a multiple-choice quiz question with threechoices.--><cc:QuizA question="Which of the following is not the name of one of the seven dwarfs?"choice0="Dopey" choice1="Sneezy"choice2="Harold" answer="2"/><!--The following code instantiates an object of the class named QuizB for a multiple-choice quiz question with threechoices. Note that the interface is exactly the same as for the class named QuizA. However, the implementation ofQuizB is radically different from QuizA.--><cc:QuizB question="Which of the following is not the name of one of the seven dwarfs?"choice0="Dopey" choice1="Sneezy"choice2="Harold" answer="2"/>

The important thing...

The important thing to note in Listing 1 is that, with the exception of the name of the class being instantiated in each case (QuizA and QuizB) , the mxml code is identical for the two cases.

The code that begins with cc:Quiza produces the top question object in Figure 1 and the code that begins with cc:Quizb produces the bottom question object in Figure 1.

Since the mxml code for the two objects is identical, the user interface for the two classes must also be identical.

The project file structure

The Flex project file structure that I used for this program is fairly typical. However, before getting into a discussion of the two class files,I will show you how the files are organized in the Flex project as shown in Figure 2.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask