add factories and update code samples for chapter 4