25-Comp-B11 Advanced Software Design · December 2018
Question 17 of 28: Credit-Card Validation — Template Method Pattern
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Notes on this paper
17-Comp-B11 Advanced Software Design — National Exams, December 2018. 3 hours, closed book exam with up to 2 aid sheets 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, 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 and the "program to an interface, not an implementation" / "favor object composition over class inheritance" principles; 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; Barbara Liskov's 1987 substitutability paper for Question 11; Karl Wiegers, Software Requirements (3rd ed.) — the functional/quality/process/implementation/business requirements taxonomy of Question 2; Kruchten, The Rational Unified Process: An Introduction, for Question 1; Myers, The Art of Software Testing, for Questions 6 and 28.
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(). Two concrete subclasses — VisaValidator, MasterCardValidator — 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 requires only a NEW subclass implementing the four primitive operations — no existing code needs to change, directly satisfying the "program to an interface, not an implementation" principle of Question 22.
Complementary pattern. If the client must select which concrete validator to use for a given card number at runtime, 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.
Template Method: fixed 4-step validate() lives once in the abstract base; each of the two subclasses supplies only the primitive operations.