JAVA Design Patterns (1) - Factory Method Pattern

1. Ordinary Simple Factory

        The factory class is responsible for creating instances of classes that implement the same interface,

The relationship diagram is as follows

code show as below:

package com.designer.factory;

public class SendFactory {
	public Sender produce(String type) {
		if ("mail".equals(type)) {
			return new MailSender();
		} else if ("msg".equals(type)) {
			return new MsgSender();
		} else {
			System.out.println("请输入正确的类型");
			return null;
		}

	}
}
package com.designer.factory;

public interface Sender {
	public void send();
	
}
package com.designer.factory;

public class MailSender implements Sender{

	@Override
	public void send() {
		// TODO Auto-generated method stub
		System.out.println("this is a mailSender");
	}
	

}
package com.designer.factory;

public class MsgSender implements Sender{

	@Override
	public void send() {
		// TODO Auto-generated method stub
		System.out.println("this is a msgSender");
	}

}
package com.designer.factory;

public class Test {
	public static void main(String[] args) {
		Sender send=new SendFactory().produce("msg");
		send.send();
	}
}

operation result:

 

2. Multi-method simple factory

        In the ordinary factory method pattern, if the passed string is wrong, the object cannot be created correctly. The multi-method simple factory provides the factory with a method to create each instance to avoid this problem.

The relationship diagram is as follows

code show as below:

        In contrast , only SenderFactory has changed here, and only the code of SenderFactory and Test is posted here.

package com.designer.factory;

public class SendFactory {
	public Sender produceMail() {
		return new MailSender();
	}
	public Sender produceMsg(){
		return new MsgSender();
	}
}
package com.designer.factory;

public class Test {
	public static void main(String[] args) {
		Sender send=new SendFactory().produceMail();
		send.send();
	}
}

The result is as follows:

 

3. Static method simple factory

        Set the methods in the above multiple factory method patterns as static, without creating an instance, call them directly

        Change the produceMail() and produceMsg() in SenderFactory to static methods. Other files remain unchanged

code show as below:

package com.designer.factory;

public class SendFactory {
	public static Sender produceMail() {
		return new MailSender();
	}
	public static Sender produceMsg(){
		return new MsgSender();
	}
}
package com.designer.factory;

public class Test {
	public static void main(String[] args) {
		Sender send=SendFactory.produceMail();
		send.send();
	}
}

 

4. Summary

Factory pattern usage scenarios:

        When several implementation classes have the same interface, the factory pattern can be used, which is easy to extend and modify.

Factory mode selection:

        In the above three modes, the first type of string error will cause an exception, and the third type is a static method and does not need to repeatedly create objects, so the third type is generally selected - static factory method

 

5. Factory Method Pattern

        A problem with the simple factory pattern is that the creation of classes depends on the factory class, that is, if you want to extend the program, you must modify the factory class.

        Using the factory method pattern, create a factory interface and create multiple factory implementation classes, so that once you need to add new functions, you can directly add new factory classes without modifying the previous code

The relationship diagram is as follows:

The specific implementation will not be written out here, but only provides an interface to MailSenderFactory and MsgSenderFactory, making the factory class easy to extend.

 

The benefits of this model

If you want to add a function now: send timely information, you only need to make an implementation class, implement the Sender interface, and at the same time make a factory class, implement the Provider interface, it is OK, no need to change the ready-made code. Doing so, expands better!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325560820&siteId=291194637