Factory pattern of java design pattern

  • A common factory
    is to create a factory class to create instances of some classes that implement the same interface.
    Create a common interface for both:
public interface Sender {  
    public void Send();  
}  

Create the implementation class:

public class MailSender implements Sender {  
    @Override  
    public void Send() {  
        System.out.println("this is mailsender!");  
    }  
}  

Build factory class:

public class SendFactory {  

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

Test class:

public class FactoryTest {  

    public static void main(String[] args) {  
        SendFactory factory = new SendFactory();  
        Sender sender = factory.produce("sms");  
        sender.Send();  
    }  
}  

Output: this is sms sender!

  • The multiple factory method pattern
    is an improvement to the common factory method pattern. In the common factory method pattern, if the passed string is wrong, the object cannot be created correctly, while the multiple factory method pattern is to provide multiple factory methods to create objects separately. .
    Modify the above code and change the SendFactory class, as follows:
public class SendFactory {  
   public Sender produceMail(){  
        return new MailSender();  
    }  

    public Sender produceSms(){  
        return new SmsSender();  
    }  
}  

The test class is as follows:

public class FactoryTest {  

    public static void main(String[] args) {  
        SendFactory factory = new SendFactory();  
        Sender sender = factory.produceMail();  
        sender.Send();  
    }  
} 

Output: this is mailsender!

  • Static factory method pattern :
    Set the methods in the above multiple factory method patterns as static, without creating an instance, just call them directly.
public class SendFactory {  

    public static Sender produceMail(){  
        return new MailSender();  
    }  

    public static Sender produceSms(){  
        return new SmsSender();  
    }  
}  

Test class:

public class FactoryTest {  

    public static void main(String[] args) {      
        Sender sender = SendFactory.produceMail();  
        sender.Send();  
    }  
}  

Output: this is mailsender!
Factory pattern is suitable: when a large number of products need to be created and have a common interface, they can be created through the factory method pattern. In the above three modes, the first one cannot create the object correctly if the incoming string is wrong, the third one does not need to instantiate the factory class relative to the second one, so in most cases, we will Choose the third - static factory method pattern.

  • Abstract Factory :
    A problem with the factory method pattern is that the creation of a class depends on the factory class, that is to say, if you want to extend the program, you must modify the factory class, which violates the closure principle. Therefore, from From the design point of view, there are certain problems, how to solve them? Just use the abstract factory pattern to create multiple factory classes, so that once you need to add new functions, you can directly add new factory classes without modifying the previous code.

example:

public interface Sender {  
    public void Send();  
}  

Two implementation classes:

public class MailSender implements Sender {  
    @Override  
    public void Send() {  
        System.out.println("this is mailsender!");  
    }  
}  

public class SmsSender implements Sender {  

    @Override  
    public void Send() {  
        System.out.println("this is sms sender!");  
    }  
}  

Unified interface for two factory classes:

public interface Provider {  
    public Sender produce();  
}  

Two factory classes:

public class SendMailFactory implements Provider {  

    @Override  
    public Sender produce(){  
        return new MailSender();  
    }  
}


public class SendSmsFactory implements Provider{  

    @Override  
    public Sender produce() {  
        return new SmsSender();  
    }  
}  

Test class:

public class Test {  
    public static void main(String[] args) {  
        Provider provider = new SendMailFactory();  
        Sender sender = provider.produce();  
        sender.Send();  
    }  
}  

The advantage of this mode is that 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=325966545&siteId=291194637