Interviewer: Simple Factory Pattern of Design Patterns

A true master always divides his life into two with the heart of an apprentice
, without hesitation in the first half of his life, and no regrets in the second half of his life.
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] for more There are 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.

insert image description here

Article directory


foreword

After half a year of job hunting, your Chico finally couldn't stand it anymore. He had no source of income for half a year and was forced to go to work in an electronics factory first. Someone asked me why I had to rely on talent, although I could rely on my face to eat. Think this may be Chico's natural arrogance.

insert image description here

Pushing open the door of the factory, there are rows of worn-out sewing machines. I told the supervisor that we are not an electronics factory, where are the electronics? The supervisor pointed to the electronic watch on the wall, and said that the factory with the electronic watch is the electronic factory (I Gan). . . .

I opened the four-seven city on my phone again, and looked at the picture of the recruitment profile. It was a world of difference. Now, do you have to use photos to deceive people when you are working? The picture of the recruitment profile is as follows (suckling~~)

insert image description here

Forget it, do it as soon as you come, go to the dormitory to pick a good place first, don't talk about the bed if you go late, the floor will not be able to hit the place.

insert image description here
Fortunately, I came early, and no one came to the dormitory. I first occupied a seat by the window and next to the heater. Next to the heater, I could put the washed socks on it as a humidifier (poof...)

On the first day of the class, the little friends were in full swing. I stepped on the pedal of the sewing machine as smoothly as I stepped on a hot wheel. Just as I was winking with the next door Xiaomei, my phone rang.

Me: "Hey, who is it!"

Interviewer: "Hello, is this Mr. Qi? We are from XXX Company. We have seen your resume. Is it convenient for the interview now?"

Me: "Convenient, you can interview!"

Interviewer: "Okay, let's talk about your understanding of the simple factory pattern first."

Me: "Well... Simple Factory, his simple factory is actually... um." (At this time, the boss heard me say Simple Factory, and immediately came over to quibble)

Boss: "We are not a simple factory. Our factory is very spectacular and complicated. Don't listen to Xiao Qi's nonsense."

Me: (I'm so...what's the matter with you)

1. Interview

Interviewer: Can you tell me the definition of the simple factory pattern?

Me: The simple factory pattern is to define a factory class, which can return instances of different classes according to different parameters, and the created instances usually have a common parent class.

Interviewer: Can you think of a real-life scenario to describe it?

Me: Suppose I open an orchard called "Sweet Orchard", my orchard can be regarded as a factory, and the fruits produced from my orchard are called products. Suppose you come To buy fruit in my orchard, you only need to tell me the fruit you want to buy, such as apples, where apples represent a parameter, you tell me you need apples, and I will produce a box of apples for you. You don't need to know how apples are produced, you don't need to know whether our apples are picked from trees or from the ground, you only need to know 50 yuan per box.

Interviewer: What are the core roles of the simple factory pattern

me: Simple factory pattern has 3 roles.

1. Factory (factory role): The role of the factory is "Sweet Orchard", which is responsible for implementing the internal logic of creating all product instances; the factory class can be directly called by the outside world, that is, the user can directly call the factory and say what fruit you need , and then we will produce it for you, providing a static factory method factoryMethod() in the factory class, and its return type is the abstract product type Product, for example, the product type here is a box, and the highest fruit is packed into a box (product) .

2. Product (abstract product role): It is the parent class of all objects created by the factory class. To put it bluntly, it is a fruit box, and all fruits need to be put into the box.

3. ConcreteProduct (specific product role): It is the creation target of the simple factory pattern, that is, the final fruit, such as apples, bananas, etc. Each concrete product role inherits the abstract product role, that is, each fruit is packed into a box.

Interviewer: Can you write me a simple factory pattern?

1. First, you need a custom batch of fruit boxes

public abstract class Product {
    
    
    //所有水果类的公共特点
    public void common(){
    
    
        System.out.println("我们都是水果,我们都能榨汁");
    }

    //声明抽象业务方法
    public abstract void methodDiff();
}

2. Then you need some real fruits to put in the fruit box

public class Apple extends Product{
    
    
    @Override
    public void methodDiff() {
    
    
        System.out.println("我是苹果,我5元一斤");
    }
}

3. Then you need to create a factory to sell these fruits

public class Factory {
    
    
    //静态工厂方法
    public static Product getProduct(String name){
    
    
        Product product = null;
        if(name.equals("苹果")){
    
    
            product = new Apple();
        }
        return product;
    }
}

4. Finally the customer calls the factory and says I need Apple

public class Client {
    
    
    public static void main(String[] args) {
    
    
        Product product;
        product = Factory.getProduct("苹果"); //给工厂打电话要一箱苹果
        product.common();   //苹果到家了,我输出一下苹果的好坏
        product.methodDiff(); //苹果到家了,我输出一下苹果的好坏
    }
}

insert image description here

2. Summary

The relevant content here has not been sorted out, and the article will continue to be updated later. 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.

Guess you like

Origin blog.csdn.net/weixin_44096133/article/details/125730876