In Java 23, the design mode is Factory Method. The design mode is factory.
1. General factory Model
Factory
/*** @ Title Factory. java * @ Package factory. factory1 * @ date 10:16:02 * @ version V1.0 */package factory. factory1;/*** @ ClassName Factory * @ date 2015-1-22 10:16:02 */public class Factory {public Sender procedure (String type) {if ("mail ". endsWith (type) {return new MailSender ();} if ("sms ". endsWith (type) {return new SMSSender ();
an example of sending emails and texting)
First, create a common interface between the two:
Public interface Sender {public
void Send ();
}
Second, create the implementation class:
public class MailSender implements Sender {
@Override public
void Send () {
System.out.println () Mailsender! ");
}
}
public class Smssender implements Sender {
@Override public
void Send () {
System.out.println ("This is SMS
, create a common interface for both:Public interface Sender {public void Send ();}Second, create the implementation class:public class MailSender implements Sender {@Overridepublic void Send () {System.out.println ("This is mailsender!");}}public class Smssender implements Sender {@Overridepublic void Send () {System.out.println ("This is SMS Sender!");}}Finally, build the factory class:public class Sendfactory {public Sender produce (String type) {i
.
Three, Java 23 design pattern
Starting from this piece, we introduce the concept of 23 design Patterns in Java, the application scenarios and so on, and combine their characteristics and design patterns of the principles of analysis.
1. Factory mode (Factory method)
The factory method model is divided into three kinds:
11, the common factory model is to establish a factory class, the implementation of the same interface for some classes to create an instance. First look at the diagram:
Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39760895Factory method Mode (Factory )Factory method: As the name implies, is called the factory method to produce objects (products).There are 3 ways to implement factory methods:first, the Common factory model。 is to create a factory class that creates instances of classes that implement the same interface.1. UML Modeling Diagram:2. Code implementation/** * Demo sample (i): General Factory Method * Disadvantage: Assum
class, the implementation of the same interface for some classes to create instances. First look at the diagram:The ratio is as follows: (we give a sample of sending email and SMS)First, create a common interface for both:Public interface Sender {public void Send ();}Second, create the implementation class:public class MailSender implements Sender {@Overridepublic void Send () {System.out.println ("This is mailsender!");}}public class Smssender imple
Introduction to the factory method mode of the JavaScript design mode, and the javascript Design Mode
1. Simple factory Model
Note: A factory class is created to create the implementation class for the same interface.
But JavaScript seems to have no interface, so we should remove the interface layer. Of course, the member variables in the implementation class here should be the same;
For example, here is an example of text message sending and email sending;
1>. Mail sending [Implementation] cl
factory class, to implement the same interface of the Product class for instance creation
Example://Send SMS and Mail interface public interface Sender {public void send (); The implementation class public class MailSender implements Sender {public void Send () {System.out
. println ("Send mail!"); }///Send SMS Implementation class public class Smssender implements Sender {public void Send () {Sys
TEM.OUT.PRINTLN ("Send SMS!");
This article mainly introduces the factory method mode of the JavaScript design mode. This article describes the simple factory mode, multiple factory method modes, and so on. For more information, see
1. Simple factory Model
Note: A factory class is created to create the implementation class for the same interface.
But JavaScript seems to have no interface, so we should remove the interface layer. Of course, the member variables in the implementation class here should be the same;
For example
This article mainly introduces the Factory method mode of the JavaScript design mode. This article describes the simple factory mode, multiple Factory method modes, and so on. For more information, see
1. simple factory model
Note: A factory class is created to create the implementation class for the same interface.
But JavaScript seems to have no interface, so we should remove the interface layer. of course, the member variables in the implementation class here should be the same;
For example
Java Design Pattern cainiao series (4) Factory method pattern modeling and implementation
Factory Method)
Factory method: As the name implies, it is to call methods in the factory to produce objects (products.There are three ways to implement the factory method:
I. general factory Model. Is to create a factory class and create instances for some classes that implement the same interface.
1. uml modeling diagram:
2. Code Implementation
/*** Example (1): Common factory method ** disadvantage: I
Reprint Please specify source: http://blog.csdn.net/lhy_ycu/article/details/39760895Factory method Mode (Factory )Factory method: As the name implies, is called the factory method to produce objects (products).There are 3 ways to implement factory methods:first, the Common factory model。 is to create a factory class that creates instances of classes that implement the same interface.1. UML Modeling Diagram:2. Code implementation/** * Example (i): General Factory Method * * Disadvantage: If a st
First, the factory method modeThere are three types of factory method models 1, Ordinary Factory mode : is to establish a factory class, the implementation of the same interface for some classes to create instances. First look at the diagram:Here's an example: (Let's give an example of sending an email and a text message)First, create a common interface for both:
Public interface Sender {
public void Send ();
}
Second, create the implementation class:
public class
Factory mode (Factory method)The factory model is suitable : When a large number of products need to be created, and have a common interface, can be created through the factory method mode. In the following three modes, the first if the passed string is incorrect, the object cannot be created correctly, the third is not required for the second type, so, in most cases, we will choose the third-static factory method mode.1: normal Factory modeis to create a factory class that creates instances of
The factory method pattern is to put the factory class into a method to create, so that the factory class can be created, but also do not modify the factory class code, so as to extend the functionality, the other advantage of the design is that the code is well encapsulated.Cases:Create a factory interfacepublic interface sender{public void Send ();}Two factory implementation classespublic class MailSender implements sender{@Overridepublic void Send () {System.out.println ("This is mainsender!"
1. Simple Factory model
Description: Create a factory class that implements the creation of the implementation class for the same interface.
But it seems like JavaScript doesn't have an interface, so we remove the interface layer; Of course, the member variables under the implementation class here should all be the same;
For example: At this time, to send text messages and send examples;
1>. mail delivery [Implementation] class
Copy Code code as follows:
function MailS
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.