cxlove / DesignPattern

The example code from head first design patterns for practicing
0 stars 2 forks source link

The Factory Pattern using Java #10

Open cxlove opened 8 years ago

cxlove commented 8 years ago

RT

cxlove commented 8 years ago

Type#1: The simple factory.

Which have a store need to order different types of product. And we can build a factory to help us create the products which base on a common interface. So the store don't care how factory do it, and it also don't care it will be a new product occurred. So the code in store is written to interface, not concrete class. It also fit the first principle which find the aspects that vary and separate them from what stay the same.

cxlove commented 8 years ago

Type#2: Factory Method

which defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. We can create a abstract method for concrete factory to implement to create product.