Java Design Patterns

Source: Internet
Author: User

Java23 design mode.

In general, design patterns fall into three broad categories :

Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.

There are 11 types of behavioral Patterns: Strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

Stepwise Analysis:

1. General Factory mode:

Send interface:

 Public Interface Sender {    publicvoid  send ();}

Two classes that implement an interface:

 Public class Implements sender{    @Override    publicvoid  Send () {        System.out.println ("Send mail!") );    }}
 Public class Implements Sender {    @Override    publicvoid  Send () {        System.out.println ( "Send SMS!" );    }}

Factory Method:

 Public classSendfactory { PublicSender Produce (String type) {if("Mail". Equals (Type) {            return NewMailSend (); } Else if("SMS". Equals (Type) {            return NewSmssend (); } Else{System.out.println ("Please check the type"); return NULL; }    }}

Test method:

 Public class Test001 {    @Test    publicvoid  test001 () {        new  Sendfactory ();         = Factory.produce ("Mail");         = Factory.produce ("SMS");        SYSTEM.OUT.PRINTLN (produce);        System.out.println (PRODUCE2);    }}

Operation Result:

[Email protected] [Email protected]

Analysis: You can create different objects, but you cannot create objects if the strings are wrong.

2: multiple factory method modes

The difference from the first method is the factory method:

 Public class sendfactory {    public  Sender producemailsend () {        returnnew  MailSend ();    }      Public Sender producesmssend () {        returnnew  smssend ();    }}

Test:

     Public void test001 () {        new  sendfactory ();         = factory.producemailsend ();         = factory.producesmssend ();        SYSTEM.OUT.PRINTLN (produce);        System.out.println (PRODUCE2);    }

Operation Result:

[Email protected] [Email protected]

Analysis: After this modification, there is no problem of passing the value.

3: Static Factory method mode

To modify the factory method:

 Public class sendfactory {    publicstatic  Sender producemailsend () {        return New MailSend ();    }      Public Static Sender producesmssend () {        returnnew  smssend ();    }}

Test method:

 Public class Test001 {    @Test    publicvoid  test001 () {        =  Sendfactory.producemailsend ();         = sendfactory.producesmssend ();        SYSTEM.OUT.PRINTLN (produce);        System.out.println (PRODUCE2);    }}

Operation Result:

[Email protected] [email protected]

Analysis: Do not need to create new objects, directly using the class loading method, 123 In contrast, 3 is more commonly used. But when it comes to extending the functionality, it's not good to change the previous code. So we have to think, how to do to expand our program without changing the source code. So there's the abstract factory method pattern .

4: Abstract Factory Method mode

Solution, we create multiple factory classes, and once you need to add new functionality, add the factory class directly.

Code:

Interface

 Public Interface Sender {    publicvoid  send ();}

Even an implementation class:

 Public class Implements sender{    @Override    publicvoid  Send () {        System.out.println ( "Send mail!" );    }}
 Public class Implements Sender {    @Override    publicvoid  Send () {        System.out.println ("Send SMS!") );    }}

Interface of the factory method:

 Public Interface Provider {    public  Sender getsend ();}

The implementation class for the factory method:

 Public class Implements Provider {    @Override    public  Sender getsend () {        returnNew  MailSend ();    }}
 Public class Implements provider{    @Override    public  Sender getsend () {        return New smssend ();    }}

Test class:

 Public class Test001 {    @Test    publicvoid  test001 () {        new  Mailsendfactory ();         = provider.getsend ();         New smssendfactory ();         = provider2.getsend ();        SYSTEM.OUT.PRINTLN (send);        System.out.println (Send2);    }}

Operation Result:

[Email protected]
[Email protected]

Analysis: Adding new functionality requires only the interface of the functional interface and the factory method, and the new functionality can be added without the need to change the previous code.

Example: Add send

To add an implementation class:

 Public class Implements sender{    @Override    publicvoid  Send () {        System.out.println ("Send" );    }}

To add an implementation class for a factory method:

 Public class Implements Provider {    @Override    public  Sender getsend () {        returnNew  weixinsend ();    }}

Test method:

 Public classTest001 {@Test Public voidtest001 () {Provider Provider=Newmailsendfactory (); Sender Send=Provider.getsend (); Provider Provider2=Newsmssendfactory (); Sender Send2=Provider2.getsend ();        SYSTEM.OUT.PRINTLN (send);        System.out.println (SEND2); Provider Provider3=Newweixinsendfactory (); Sender Send3=Provider3.getsend ();    System.out.println (SEND3); }}

Operation Result:

[Email protected]
[Email protected]
[Email protected]

Analysis: Add new functionality as long as the appropriate method and factory methods are added, without modifying the original code. Facilitates expansion.

Java Design Patterns

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.