Factory method pattern
Java
Medium
4 views
Problem Description
Task: create static factory for Rectangle to validate inputs (w,h > 0).
Output Format
Return value
Constraints
Throw exception on invalid.
Official Solution
static class Rect{final int w,h;private Rect(int w,int h){this.w=w;this.h=h;}static Rect of(int w,int h){if(w<=0||h<=0) throw new IllegalArgumentException();return new Rect(w,h);}int area(){return w*h;}}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!