githubEdit

23. Design Unlock Pattern

Difficulty: Medium Topics: Graph Theory, DFS/Backtracking, Validation Logic Key Concepts: Midpoint Formula for Skip Logic, Adjacency Matrix.

Phase 1: Requirements Gathering

Goals

  • Design the logic to validate and set the 3x3 Android Grid unlock pattern.

1. Who are the actors?

  • User: Draws a pattern on the screen.

  • System: Validates the pattern constraints.

2. What are the must-have features? (Core)

  • Grid: 3x3 dots (Indices 0-8).

  • Constraints:

    1. Minimum 4 dots.

    2. Each dot visited at most once.

    3. Skip Rule: Connecting non-adjacent dots (e.g., 0 -> 2) is valid ONLY if the middle dot (1) was already visited.

3. What are the constraints?

  • Complexity: $O(N!)$ validation is fine since N=9.

  • Storage: Store hashed pattern, not raw sequence.


Phase 2: Use Cases

UC1: Set Pattern

Actor: User Flow:

  1. User connects sequence: 0 -> 3 -> 6 -> 7.

  2. System validates:

    • Length >= 4? Yes.

    • No repeats? Yes.

    • Valid moves? (0->3 adjacent, 3->6 adjacent). Yes.

  3. System saves pattern hash.

UC2: Invalid Skip

Actor: User Flow:

  1. User connects: 0 -> 2.

  2. System calculates Midpoint between 0 and 2 is 1.

  3. System checks: Is 1 in visited set? No.

  4. System rejects pattern: "Cannot skip over unvisited node".


Phase 3: Class Diagram

Step 1: Core Entities

  • PatternLockSystem: Facade.

  • PatternValidator: Encapsulates the complex rules.

  • Point: Represents (r, c) or index.

UML Diagram

spinner

Phase 4: Design Patterns

1. Strategy Pattern

  • Description: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.

  • Why used: Validation rules might evolve (e.g., 3x3 grid vs 4x4 grid, allow repeats vs no repeats). Strategy allows injecting different PatternValidator implementations without changing the LockSystem code.

2. Graph Theory (DFS)

  • Description: Modeling the grid as a Graph where nodes are dots and edges are valid moves.

  • Why used: Validating a pattern is essentially traversing a path in a graph. We use DFS to ensure the path doesn't visit a node twice (unless allowed) and respects the "Skip" constraints (edges exist only if intermediate node visited).


Phase 5: Code Key Methods

Java Implementation


Phase 6: Discussion

Combinatorics

Q: "How many valid patterns exist?"

  • A: "Using DFS to count, there are exactly 389,112 valid patterns for a 3x3 grid with min length 4."

Security

Q: "Is it secure?"

  • A: "Entropy is lower than a 6-digit PIN. Smudge attacks (grease on screen) can reveal the pattern. Should throttle attempts (exponential backoff after 5 failures)."

Knight's Move

Q: "Is 0 -> 5 valid?"

  • A: "Yes. Use the midpoint formula: $(0+1)/2 = 0.5$, $(0+2)/2 = 1.0$. Row is fractional, so no integer node exists between them. Direct connection allowed."


SOLID Principles Checklist

  • S (Single Responsibility): PatternLockSystem manages auth state, Validator manages graph rules.

  • O (Open/Closed): Logic is extensible.

  • L (Liskov Substitution): N/A.

  • I (Interface Segregation): N/A.

  • D (Dependency Inversion): N/A.

Last updated