Design Pattern s - Creative Patterns- Factory Method
It is a mode of operation that replaces new. It can bring more scalability and fewer modifications to the system.
There are three kinds of factory modes: ordinary factory, multiple factory and static factory.
1. Ordinary factories
1.1. Description:
(1) Sender interface;
(2) MailSender implementation class;
(3) SmsSender implementation class;
(4) SimpleFactory simple factory.
1.2. Java implementation, code as follows:
(1)Sender
package com.java.designPattern.factoryMethod.simpleFactory;
/**
* Interface
*
*/
public interface Sender {
public void send();
}
(2)MailSender
package com.java.designPattern.factoryMethod.simpleFactory;
/**
* Implementation class
*
*/
public class MailSender implements Sender {
@Override
public void send() {
System.out.println("mail send !");
}
}
(3)SmsSender
package com.java.designPattern.factoryMethod.simpleFactory;
/**
* Implementation class
*
*/
public class SmsSender implements Sender {
@Override
public void send() {
System.out.println("sms send !");
}
}
(4)SimpleFactory
package com.java.designPattern.factoryMethod.simpleFactory;
/**
* Simple factory
*
*/
public class SimpleFactory {
public Sender produce(String senderName) {
if ("mail".equals(senderName)) {
return new MailSender();
} else if ("sms".equals(senderName)) {
return new SmsSender();
} else {
System.out.println("wrong parameter !");
return null;
}
}
}
(5)Test
package com.java.designPattern.factoryMethod.simpleFactory;
/**
* Test class
*
*/
public class Test {
public static void main(String[] args) {
SimpleFactory factory = new SimpleFactory();
Sender mailSender = factory.produce("mail");
mailSender.send();
Sender smsSender = factory.produce("sms");
smsSender.send();
}
}
Output:
mail send !
sms send !
2. Multiple factories
2.1. Description:
(1) Sender interface (same as 1);
(2) MailSender implementation class (same as 1);
(3) SmsSender implementation class (same as 1);
(4) Multiple factories of ManyFactory.
2.2. Java implementation, code as follows:
(1) Sender (same as 1)
(2) MailSender (same as 1)
(3) Sms Sender (same as 1)
(4)ManyFactory
package com.java.designPattern.factoryMethod.manyFactory;
/**
* Multiple factories
*
*/
public class ManyFactory {
public Sender produceMail() {
return new MailSender();
}
public Sender produceSms() {
return new SmsSender();
}
}
(5)Test
package com.java.designPattern.factoryMethod.manyFactory;
/**
* Test class
*
*/
public class Test {
public static void main(String[] args) {
ManyFactory factory = new ManyFactory();
Sender mailSender = factory.produceMail();
mailSender.send();
Sender smsSender = factory.produceSms();
smsSender.send();
}
}
Output:
mail send !
sms send !
3. Static Factory (Recommendation)
3.1. Description:
(1) Sender interface (same as 1);
(2) MailSender implementation class (same as 1);
(3) SmsSender implementation class (same as 1);
(4) Factory static factory (two methods become static).
3.2. Java implementation, code as follows:
(1) Sender (same as 1)
(2) MailSender (same as 1)
(3) Sms Sender (same as 1)
(4)Factory
package com.java.designPattern.factoryMethod.factory;
/**
* Static factory
*
*/
public class Factory {
public static Sender produceMail() {
return new MailSender();
}
public static Sender produceSms() {
return new SmsSender();
}
}
(5)Test
package com.java.designPattern.factoryMethod.factory;
/**
* Test class
*
*/
public class Test {
public static void main(String[] args) {
Sender mailSender = Factory.produceMail();
mailSender.send();
Sender smsSender = Factory.produceSms();
smsSender.send();
}
}
Output:
mail send !
sms send !