Design pattern abstract factory pattern

In the last article, we introduced the simple factory pattern and the factory method pattern. Today we will introduce th...

In the last article, we introduced the simple factory pattern and the factory method pattern. Today we will introduce the abstract factory pattern. We also use the example above.

As mentioned above, after the factory adopted the new design mode, the productivity exploded. However, the good times didn't last long. One day, the new demand came: the current films and music don't quite meet the refined preferences of the public. We want to produce pop music, niche music, pop film, niche film (key words: pop, niche, film, music).

After careful consideration, the factory is easy to operate!

interface ArtFactory { //Create music method public function createMusic(); //Create movie method public function createMovie(); }

We first create an interface for an art factory, which contains two production lines for producing movies and music. Next, we will create two sub factories (the way to realize the art factory interface) to produce pop music and movies, niche music and movies respectively.

//Popular factory class PopArtFactory implements ArtFactory { /** * Popular movies * * @return PopMoive */ public function createMovie() { // TODO: Implement createMovie() method. return new PopMoive(); } /** * pop music * * @return PopMusic */ public function createMusic() { // TODO: Implement createMusic() method. return new PopMusic(); } }
//Small factory class NotPopArtFactory implements ArtFactory { /** * Make music * * @return NotPopMusic */ public function createMusic() { // TODO: Implement createMusic() method. return new NotPopMusic(); } /** * Making movies`` * * @return NotPopMovie */ public function createMovie() { // TODO: Implement createMovie() method. return new NotPopMovie(); } }

At the same time, we have defined two products (interfaces): film and music

interface Movie { public function generate(); }
interface Music { public function generate(); }

According to the previous requirements, we have created four types of products: pop music, niche music, pop movies, niche movies.

//pop music class PopMusic implements Music { /** * black cat voice */ public function generate() { // TODO: Implement voice() method. p('pop music~'); } }
//Minority music class NotPopMusic implements Music { public function generate() { // TODO: Implement generate() method. p('Minority music~'); } }
//Popular movies class PopMovie implements Movie { public function generate() { // TODO: Implement generate() method. p('Popular movies~'); } }
//Minority film class NotPopMovice implements Movie { public function generate() { // TODO: Implement generate() method. p('Minority film~'); } }

In this way, the factory is established and the products are planned. Let's work at full power.

From this example, we can see that the abstract factory pattern is suitable for producing all kinds of products of different types. If we want to produce new products, such as comics, we need to modify and add product interfaces and product definitions. In this way, we need to modify many places, which does not conform to the opening and closing principles.

In two articles, the factory model involved is introduced. By giving these examples, we can summarize:

  • Factory model is just like his name. As a factory, we can get products through products without caring about the specific production process of products -- loose coupling. Through different types of factories and different product combinations, we can get the products we want from different factories -- scalable.
  • Simple factory mode is really only suitable for simple mode, that is, a single function factory produces a single product.
  • Factory method mode is suitable for the production of various products of the same type, but it can not add new attributes to the products.
  • Abstract factory mode is suitable for all products of different types, but no new products can be added.

6 June 2020, 00:34 | Views: 1862

Add new comment

For adding a comment, please log in
or create account

0 comments