This is the first article I participated in the novice entry, welcome everyone to exchange more corrections

concept

The factory pattern is used to encapsulate the creation of an object and typically has multiple creator (factory) classes and multiple product classes. The parent class factory includes a method for creating products that encapsulates the object creation process by letting the inherited subclass factory decide to create specific product objects.

  1. When to use the factory approach: Patterns care about the product rather than how it was generated, hiding the generation of complex objects.
  2. The advantage of using the factory approach is that there are multiple complex object products, and when adding products, you only need to add the corresponding products and the corresponding factories.

For example,

Let’s say you go back to your hometown and open a bakery and hire two pastry chefs. And then one of them pulled out of the opening.

Because of the first business, lack of experience, coupled with the idea of a little different from ordinary people, you feel that things must pay attention to the two words “atmosphere”, so one breath rented half a floor of the central shop, very row noodles. Plus machinery, raw materials, labor… At the beginning of the start-up capital is almost squandered, can be said to be completely without considering the things after.

But it doesn’t matter.

Because at this time, you ask another master to come and say to you, he can only do pure cake, after all, now can only do one kind of cake pastry chef in countless pastry chefs have no bright spot. So he’s exaggerating a little bit on his resume. But he promised that his pure cake was delicious and unique.

Hearing this, although you want to praise him on the spot is a cute, but considering the opening time is very close, leaflets and advertising have been publicized, and you are also a man of no two words, so now start to make cakes, let the shop go on is the most important, otherwise years of savings will be wasted.

Ok, now let’s make the cake:

Cake shop

public abstract class CakeFactory {

	public abstract Cake make(a);
}
Copy the code

Half – floor shop pure cake small workshop

public class PlainCakeFactory extends CakeFactory {

	@Override
    	public Cake make(a) {
            System.out.println("Pure cake in the making."); }}Copy the code

Cake interface

public interface Cake {}
Copy the code

Pure cake

public class Plain implements Cake{
    void bake(a) {
        System.out.println("Pure Cake Baking Secrets."); }}Copy the code

The first pure cakes are made, you taste them, they’re not bad, and the pastry chef doesn’t lie about it.

.

Soon it was opening day. Surprisingly, there were a lot of customers that day. Maybe out of curiosity? Maybe want to see who’s smart enough to run a store this big and sell only pure cakes? Anyway, the first step is out of the way.

But only pure cakes, with this gimmick, the shop could not be opened for long, so we had to start market research to produce other types of cakes.

In the past, for emergencies, all the facilities were built around making pure cakes. You can’t just tear it all down to make a new cake.

You already know the importance of being flexible.

Let’s take a look at the class diagram to get an idea of what the factory pattern looks like:

This way you don’t have to make a fuss every time you add a new type of cake.

But soon, new problems followed, cake shop and what new challenges?

Let’s talk about this in the next abstract Factory pattern.

To be continued…