头图
The real master will always have the heart of an apprentice. The article will be updated continuously. You can search for [Xiaoqi JAVA Interview] on WeChat and read it as soon as possible, and reply to [Information] and I will prepare it for you. the benefits! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.

 title=


foreword

Today's electronic sewing machine factory is still in full swing. The young men are very motivated to marry their wives. They step on the pedal of the sewing machine under their feet and smoke. Pushed away from the outside, a new colleague came. Based on the principle that you must watch it if you are not busy, I slowly moved my eyes to the door like everyone else.

Hey~, why does this person look so familiar? Isn't this the Internet celebrity battery brother, how could he come to work?

 title=

While eating at noon, I found Brother Battery.

Me: "Brother Battery, don't you claim to be impossible to work, why did you come here to work?"

Brother Battery: "I didn't come to work, I came to work."

Me: "You're so stupid that I'm speechless... I'm very curious about who told you to come to work."

Brother Battery: "No one persuaded me. I saw the advertisement at the entrance of the village. I am going to earn money to find a mother-in-law."

 title=

Me: "This ad works really well, it's easy to understand and attractive."

Just then my phone rang suddenly.

Me: "Hey, who is this!"

The other party: "I, I called you for an interview yesterday. Is it convenient for you now? Let's talk again."

Me: "Convenient, as long as I answer the phone during work hours, it is convenient for me."

1. Interview

Interviewer: Let's talk about the factory method pattern today. Could you tell us your understanding of the factory method pattern?

Me: The factory method pattern is an extension of the simple factory pattern, it inherits the advantages of the simple factory pattern, and also makes up for the shortcomings of the simple factory pattern.

Interviewer: Can you tell me how the factory method pattern is better than the simple factory pattern?

Me: In the factory method pattern, the logic in our factory class is based on the parameters passed in. That is, if you want to buy an apple, then you must have the judgment logic in the factory class to generate an apple instance based on the apple parameters. , when you suddenly want to eat pineapple, if there is no such logic in the factory class before, you have to go to the factory class to add this logic, so that you will frequently modify the code in the factory class, so as not to It conforms to the open-closed principle.

The factory method pattern adds a specific factory between the factory class and the product. Our specific products are produced through the specific factory, and there is no need to modify the logic in the factory class.

For example, we used to be "Sweet Orchard". If you want to eat apples, just call our factory and we will mail you a box of apples. But now that our business has expanded, we have started raising poultry, and you can call us if you want to eat chicken, but it is a bit inappropriate to call "Sweet Orchard" at this time.

So I have now established a Chico Group. You can just call the Chico Group if you want to eat it. If you want to eat apples, you can call the Chico Group, and then the Chico Group will give the subordinate "It's so sweet, Orchard". "Call you and mail a box of apples. If you want to eat chicken, you also call the Chico Group, and then Chico calls the subordinate "Really Fragrant Chicken Ring" to mail you a chicken.

Interviewer: Then tell us about the roles of the factory method pattern!

1. Product (abstract product): It is the interface that defines the product. Here, it is equivalent to a box for fruit, or a cage for chickens.

2. ConcreteProduct: He implements the abstract product interface, which is equivalent to an apple or a chicken here.

3. Factory (abstract factory): He is equivalent to the Chico Group here. He is used to receive calls from customers, and then give customers a box of fruit or a cage of chickens.

4. ConcreteFactory (specific factory): he is equivalent to "really sweet orchard" or "really fragrant chicken ring", he is responsible for receiving the group's calls and then giving the group the corresponding products.

Interviewer: Then can you write specific code to describe the factory method pattern?

1. First define a box interface, which represents the abstract product role

 public interface Box {
    public void show();
}

2. Then define an apple class, which represents a specific product

 public class Apple implements Box {
    @Override
    public void show() {
        System.out.println("买了一箱苹果");
    }
}

3. Then define a Chigo group class, which represents the abstract factory

 public interface FactoryQG {
    public void show(String name); //抽象工厂方法
}

4. Then define a "Really Fragrant" class, which represents a specific factory

 public class Zxy implements FactoryQG {
    Box box = null;
    @Override
    public void show(String name) {
        if(name.equals("苹果")){
            box = new Apple();
            box.show();
            System.out.println("真香呀果园苹果,50元一箱");
        }
    }
}

5. Final test

 public class Test {
    public static void main(String[] args) {
        FactoryQG factoryQG = new Zxy();
        factoryQG.show("苹果");
    }
}

在这里插入图片描述

It can be seen from the above process that if you need to add a "chicken circle" at this time, you only need to add a concrete factory class, and you do not need to modify the code of the abstract factory class, which is open to extension and closed to modification.

Interviewer: Okay, boy, when will you come to the company for a job?

Me: Let’s talk about it, I’ll finish sewing the long johns in my hand first. . .

2. Summary

The relevant content here has not been sorted out, and the article will continue to be updated later, and it is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search for [Xiaoqi JAVA Interview] on WeChat to read it for the first time, and reply to [Information] and there are more benefits I have prepared for you! Reply to [Project] There are some project source codes that I have prepared for you. Reply [Resume Template] There is a resume template that I have prepared for you.


小奇Java面试
12 声望7 粉丝