Java design pattern | simple factory pattern is really simple. pick it up quickly

Posted by venom999 on Fri, 03 Sep 2021 03:00:10 +0200

preface

The most direct way to get an object is to use new. If you need shampoo, use new shampoo (), and if you need shower gel, use new shower gel ().

Creating objects through the new method is equivalent to that if we want shampoo, we will automatically prepare the shampoo by hand. If we want shower gel, we will prepare the shower gel, which directly controls the production details of shampoo and shower gel.

In fact, I'm just a program ape. I don't want to know how shampoo is prepared. I just want to get a bottle of shampoo directly.

I only do what I should do, and the professional things are entrusted to the professional class. This is the "single responsibility principle" of software design.

Simple factory mode can realize my expectations, so I don't have to worry about how to prepare shampoo.

1. Mode definition

Simple factory mode, also known as static factory method. In the simple factory mode, different instances can be returned according to different parameters.

2. Mode composition role

  • Factory role
  • Abstract product role
  • Specific product roles

Factory: it is responsible for producing corresponding products according to the needs of the client. This factory can only produce a certain type of products.
Abstract product role: an abstract representation of a class of products produced by the factory.
Concrete product role: a group of concrete products. Each concrete product is a concrete implementation of an abstract product.

3. Code example

I am a procedural ape. I can't make shampoo. I want a bottle of shampoo. It may be like this:

I went to a wash and care product store
Me: "I want a bottle of shampoo"
Boss: "here, the shampoo you want"
Me: "I want a bottle of shower gel"
Boss: "here, the shower gel you want"

Here, the washing and care product store is the factory role, the washing and care product is the abstract product role, and the shampoo and shower gel are two specific products.

The factory role is responsible for the production of products. I told the factory that I want shampoo, which is a specific product, the factory will help me produce shampoo. I told the factory to produce shower gel, and the factory will help me produce shower gel.

------------------------------------------------------Code example----------------------------------------------------------

Abstract product

/**
 * Washing and care products
 * 
 * @author dashu
 * @since 2021/8/20
 */
public abstract class HygieneProduct {
	public abstract void whoAmI();
}

Specific products

/**
 * shampoo
 * 
 * @author dashu
 * @since 2021/8/20
 */
public class Shampoo extends HygieneProduct {
	public void whoAmI() {
		System.out.println("I'm shampoo");
	}
}
/**
 * Shower Gel
 * 
 * @author dashu
 * @since 2021/8/20
 */
public class BodyWash extends HygieneProduct {
	public void whoAmI() {
		System.out.println("I'm shower gel");
	}
}

Wash product enumeration

/**
 * @author dashu
 * @since 2021/8/20
 */
public enum HygieneProductEnum {
	SHAMPOO, BODY_WASH
}

Factory role

/**
 * Washing and care products store
 *
 * @author dashu
 * @since 2021/8/20
 */
public class HygieneProductStore {
    public static HygieneProduct offer(HygieneProductEnum hygieneProduct) {
        if (HygieneProductEnum.SHAMPOO.equals(hygieneProduct)) {
            return new Shampoo();
        } else if (HygieneProductEnum.BODY_WASH.equals(hygieneProduct)){
            return new BodyWash();
        } else {
            throw new UnsupportedOperationException("We don't sell this kind of products for the time being");
        }
    }
}

client

/**
 * client
 *
 * @author dashu
 * @since 2021/8/20
 */
public class Client {
    public static void main(String[] args) {
    	// I want shampoo. Tell the store that I need shampoo. The store gives me shampoo
        HygieneProduct shampoo = HygieneProductStore.offer(HygieneProductEnum.SHAMPOO);
        shampoo.whoAmI();
        
        // I want to take a shower. Tell the shop that I need shower gel. The shop gives me shower gel
		HygieneProduct bodyWash = HygieneProductStore.offer(HygieneProductEnum.BODY_WASH);
        bodyWash.whoAmI();
    }
}

4. Applicable environment of the model

  • The client only needs to know the parameters of the incoming factory class, and does not care about how to create the object: it does not care about the creation details or the class name.
  • Few products need to be created for the factory class.

END

If you often add or reduce specific products, the simple factory model is not enough. In the next article, I will introduce the factory method model to you. If you are interested, please pay attention to it

Topics: Java Algorithm