Detailed description of design patterns: Abstract Factory patterns

Source: Internet
Author: User

Abstract Factory: one problem with the Factory method mode is that the creation of classes depends on the Factory class. That is to say, if you want to expand the program, you must modify the Factory class, this violates the closure principle. Therefore, from the design perspective, how can we solve certain problems? The abstract factory mode is used to create multiple factory classes. Once new functions need to be added, new factory classes can be directly added without modifying the previous code. Because the abstract factory is not very easy to understand, let's take a look at the figure first, and then it is easier to understand with the code. Interface class: [java] public interface Sender {public void Send ();} implementation class: [java] public class MailSender implements Sender {@ Override public void Send () {System. out. println ("this is mailsender! ") ;}} [Java] www.2cto. compublic class SmsSender implements Sender {@ Override public void Send () {System. out. println (" this is sms sender! ") ;}} Factory class: [java] public class SendMailFactory implements Provider {@ Override public Sender produce () {return new MailSender ();}} [java] public class SendSmsFactory implements Provider {@ Override public Sender produce () {return new SmsSender () ;}} abstract interface class: [java] public interface Provider {public Sender produce ();} Test class: [java] public class Test {public static void main (String [] args) {Provide R provider = new SendMailFactory (); Sender sender = provider. produce (); sender. the advantage of the Send () ;}} mode is that if you want to add a function: Send timely information, you only need to create an implementation class to implement the Sender interface, at the same time, make a factory class to implement the Provider interface, and it will be OK, without modifying the ready-made code. In this way, the scalability is better!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.