From the course: Java Design Patterns: Behavioral Part 1

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Implementing a complete Command pattern

Implementing a complete Command pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 1

Start my 1-month free trial

Implementing a complete Command pattern

- [Instructor] So now I have all the components I need to implement a complete example of the commands pattern. So I have my document class, which has methods for saving and printing documents, but I want to encapsulate those pieces of functionality inside another class. So I'll start with the save functionality. I'll go to my new save commands class, and I'm going to give it a field of type of document. So I'll do private document and I'll call it document. Then I'm going to create a constructor. So I'll do public save command, and I'm going to pass in a document objects called document, and inside this constructor, I'll do this.document equals document. Next in the execute method. I can just call the save method on the document object. So I'll do document.save. Now I'm going to repeat the process for printing. So I'll go to the print command class and creates a field of type document again and again, I'm going to create…

Contents