DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Building Security into the Feature During the Design Phase
  • Build a Scalable E-commerce Platform: System Design Overview
  • A Step-by-Step Guide to Write a System Design Document

Trending

  • Building Enterprise-Ready Landing Zones: Beyond the Initial Setup
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • AI Meets Vector Databases: Redefining Data Retrieval in the Age of Intelligence
  • Docker Model Runner: Streamlining AI Deployment for Developers

The Wizard Design Pattern

By 
Nadav Azaria user avatar
Nadav Azaria
·
Mar. 01, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

We all love wizards.... (Software wizards I mean). We are always happy to jump on those ''Next" buttons like we were dancing the funky chicken on our… well you get the point. So today we bring you your beloved wizard into your coding experience. Let's jump right into an example.

Say you want to design a ConservativePerson class.

view source
print?
import java.util.List; 
class ConservativePerson{ 
  
    private boolean isVirgin; 
    private boolean isMarried; 
    private List<string> children; 
  
    ConservativePerson(boolean virgin, boolean married, List<string> children) { 
        this.isVirgin = virgin; 
        this.isMarried = married; 
        this.children = children; 
    } 
  
    public boolean isVirgin() { 
        return isVirgin; 
    } 
    public boolean isMarried() { 
        return isMarried; 
    } 
  
    public List<string> getChildren() { 
        return children; 
    } 
} 

As such it has some constrains.

  • He must be married before he can be... well, not a virgin.
  • He can't be a virgin before he can have children (as far as we know).

In the old days, which is basically all days until today..., you would probably define all kinds of modifiers methods for this class which will throw an exception in case of invariant invalidation such as NotMarriedException and VirginException. Not anymore.

Today we will do it by using the Wizard Design Pattern. We use a fluent interface style and utilize the power of a modern IDE to create a wizard-like feeling when building a ConservativePerson object. We know, we know, stop talking and show us the code... but before we will present the wizard code we will show you it usage so you will get a grasp of what we are talking about...

view source
print?
public class Main { 
public static void main(String[] args) { 
    ConservativePersonWizardBuilder wizard = new ConservativePersonWizardBuilder(); 
    ConservativePerson singlePerson = wizard. 
            createConservativePerson(). 
            whichIsSingle(). 
            getObject(); 
    ConservativePerson familyManPerson = wizard. 
            createConservativePerson(). 
            whichIsMarried(). 
            andNotVirgin(). 
            andHasChildNamed("Noa"). 
            anotherChildNamed("Guy"). 
            lastChildName("Alon"). 
            getObject(); 
  } 
  
} 

Now, it may look just like an ordinarily fluent interface, but the cool thing here is that a method is available for calling only if the current object state allows it. This means you will not be able to call the method andNotVirgin if you haven't called the method whichIsMarried.

See the following set of screen shots:


and after we state he is married we can:


Here is the wizard code. I urge you to copy/paste it to your IDE and give it a try by building an object with it.

view source
print?
import java.util.ArrayList; 
  import java.util.List; 
   
   public class ConservativePersonWizardBuilder { 
     private boolean isVirgin; 
     private boolean isMarried; 
     private List<String> children = new ArrayList<String>(); 
       
     public SetMarriedStep createConservativePerson(){ 
         return new SetMarriedStep(); 
     } 
   
     class SetMarriedStep { 
        public SetVirginStep whichIsMarried(){ 
             isMarried = true; 
             return new SetVirginStep(); 
         } 
    
         public FinalStep whichIsSingle(){ 
             isMarried = false; 
             return new FinalStep(); 
         } 
     } 
  
     class SetVirginStep { 
         public AddChildrenStep andNotVirgin(){ 
             isVirgin = false; 
             return new AddChildrenStep(); 
         } 
         public FinalStep butStillAVirgin(){ 
             isVirgin = true; 
             return new FinalStep(); 
         } 
     } 
  
     class FinalStep { 
         public ConservativePerson getObject(){ 
             return new ConservativePerson(isVirgin, isMarried, children); 
         } 
     } 
  
     class AddChildrenStep { 
         public AddChildrenStep andHasChildNamed(String childName) { 
             children.add(childName); 
             return new AddChildrenStep(); 
         } 
         public AddChildrenStep anotherChildNamed(String childName) { 
             children.add(childName); 
             return new AddChildrenStep(); 
         } 
         public FinalStep lastChildName(String childName){ 
             children.add(childName); 
             return new FinalStep(); 
         } 
     } 
 } 

As you can see the wizard consists of several steps. Each step is represented by a dedicated inner class. Each step reveals the legal available operations by its methods. Each method will then return a new step according to the change it has made. This way an attempt to create an illegal object will be detected at compile time instead of runtime.

This pattern is actually being used in our production code. One example that comes to mind is the MediaJob class. This class describes a manipulation on some media files. In order to submit a job to the system, one has to create a MediaJob object. The problem is that this object has many parameters that could be assigned with contradicting values that create an illegal object state. By using the Wizard pattern, one can easily build a legal job without the need to know the entire (and complicated…) set of constrains.

That is all for now. Hope you'll give it a try..... 

view source
print 

Design

Opinions expressed by DZone contributors are their own.

Related

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Building Security into the Feature During the Design Phase
  • Build a Scalable E-commerce Platform: System Design Overview
  • A Step-by-Step Guide to Write a System Design Document

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: