AS3 - when to implement or expand?

Take, for example, a multi-choice game.

Do you have the MathQuestion and WordQuestion classes, should they implement the IQuestion interface that defines the question, answer, and difficulty functions OR is it more common to extend the base question class and redefine these functions?

What is the best way to do such things?

+3
source share
2 answers

Here's another way of thinking about the problem - are there any differences at all between MathQuestion and WordQuestion? For me, it sounds like they are both objects of a Question, and you can distinguish between different types with composition .

Enum, , ( , ActionScript 3 Enums, .)

public class QuestionType {
    public static const MATH : QuestionType = new QuestionType("math");
    public static const WORLD : QuestionType = new QuestionType("world");

    private var _name : String;

    // Private constructor, do no instantiate new instances.
    public function QuestionType(name : String) {
        _name = name;
    }

    public function toString() : String {
        return _name;
    }
} 

QuestionType :

public class Question {
    private var _message : String /* Which country is Paris the capital of? */
    private var _answers : Vector.<Answer>; /* List of possible Answer objects */
    private var _correctAnswer : Answer; /* The expected Answer */
    private var _type : QuestionType; /* What type of question is this? */

    public function Question(message : String, 
                              answers : Vector.<Answer>, 
                              correctAnswer : Answer, 
                              type : QuestionType) {
        _message = message;
        _answers = answers.concat(); // defensive copy to avoid modification.
        _correctAnswer = correctAnswer;
        _type = type;
    }

    public function getType() : QuestionType {
        return _type;
    }
}

, (, Question) :

public class QuizView extends Sprite {
    public function displayQuestion(question : Question) : void {
        // Change the background to reflect the QuestionType.
        switch(question.type) {
            case QuestionType.WORLD:
                _backgroundClip.gotoAndStop("world_background");
                break;

            case QuestionType.MATH:
                _backgroundClip.gotoAndStop("math_background");
                break;
        }
    }
}
+2

. , /, . , .

, , " /" , , .

, , , , /. .

+2

All Articles