From the course: Advanced TypeScript Concepts
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Design patterns with generics - TypeScript Tutorial
From the course: Advanced TypeScript Concepts
Design patterns with generics
- [Instructor] You probably have heard of the factory pattern. This is a creational design pattern that you can use when you need to create an object, but you don't know the exact class. That might sound a little weird if this is the first time you've heard about it, but imagine that you'll have to create a document, but you're not sure which type of document yet. You can use generics for creating the factory methods pattern. In order to do this, we'll define an interface for the document. It has a key name, postfix, content, and a print content method. Next, we're going to implement the concrete document classes. We have a Excel document that's implementing documents. You can see the details are filled out here. We also have a Word document where, again, we have a postfix that is set, and we also have a body for the print content method now. Then we need the document factory class that looks somewhat like this. We have a method, create document, that's accepting a string, and when…