NivaarExam PrepOfficial exam papers ↗

25-Comp-B11 Advanced Software Design · May 2016

Question 17 of 28: Credit-Card Validation — Pattern Selection

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

98-Comp-B11 Advanced Software Design — National Exams, May 2016. 3 hours, closed book exam with one aid sheet allowed (written on both sides), no calculator permitted. The paper is organized into five parts, and candidates were instructed to answer any five (5) questions in Part I, any three (3) in Part II, any four (4) in Part III, any two (2) in Part IV, and any five (5) in Part V — only the first questions answered, in each part, as they appear in the answer book are marked. All questions carry equal weight, so the 19 questions actually marked (5+3+4+2+5 of 28) each count for 100/19 ≈ 5.26% of the paper. All 28 questions are answered below for completeness.

Reference texts: Sommerville, Software Engineering (10th ed., Pearson) — software processes, requirements engineering, agile methods, design principles, dependability; Pressman, Software Engineering: A Practitioner's Approach (9th ed.) — supplementary process and quality coverage; Gamma, Helm, Johnson & Vlissides (GoF), Design Patterns: Elements of Reusable Object-Oriented Software — creational/structural/behavioural pattern catalogue (Singleton, Proxy, Template Method, Observer, etc.); Sebesta, Concepts of Programming Languages (12th ed.) — polymorphism, dynamic binding, inheritance and language-level object semantics; Bertrand Meyer, Object-Oriented Software Construction — design by contract, preconditions/postconditions/invariants, the open–closed principle; Barbara Liskov's 1987 substitutability paper for Question 11; Rogers, Sharp & Preece, Interaction Design, and Nielsen, Usability Engineering, for Question 21's HMI-specific non-functional requirements; Myers, The Art of Software Testing, for Question 28's boundary value analysis.

PART I — General Principles (answer any 5 of 7)

Question 17: Credit-Card Validation — Pattern Selection (Part III)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

A FIXED SEQUENCE of steps (expiration → length → characters → standing), always in the same order, but performed DIFFERENTLY per card type, is the textbook signature of the Template Method pattern: define the fixed algorithm skeleton once in an abstract base class, with each step exposed as an abstract "primitive operation" that each concrete card-type subclass implements according to its own format rules.

Constituents. An abstract class CardValidator declares a single, non-overridable (e.g., Java final) template method validate() that calls the four primitive operations in the fixed order, plus four protected abstract primitive operations: checkExpiration(), checkLength(), checkCharacters(), checkStanding(). Three concrete subclasses — VisaValidator, MasterCardValidator, DinersClubValidator — each implement the four primitives according to their own card-number length and character rules; the ORDER itself lives only in the base class and is never duplicated.

Ease of adding new card types. Adding a new card type (e.g., a fourth network) requires only a NEW subclass implementing the four primitive operations — no existing code (the base class, any other subclass, or client code that calls validate()) needs to change, directly satisfying the open–closed principle from Question 22.

Complementary pattern. If the client must select which concrete validator to use for a given card number at runtime (e.g., from a card-number prefix), a Factory Method pairs naturally with Template Method here: a CardValidatorFactory maps the prefix to the correct concrete validator instance, so client code depends only on the abstract CardValidator type and never names a concrete subclass directly.

CardValidator + validate(): final # checkExpiration() # checkLength(), checkCharacters() # checkStanding() VisaValidator MasterCardValidator DinersClubValidator
Template Method: fixed 4-step validate() lives once in the abstract base; each subclass supplies only the primitive operations.