Java Factory Methods Mode (Factory method)

Source: Internet
Author: User

1. General Factory mode

Normal Factory mode: It is to build a factory class that creates instances of some classes that implement the same interface.

1-1. Establishing the Sender Interface

Public interface Sender {
public void Send ();
}

1-2 Create implementation class implementation sender interface, such as text and image and video class

Text class

public class Textsender implements Sender {
public void Send () {
System.out.println ("This is textsender!");
}
}

Image class

public class Imagesender implements Sender {
public void Send () {
System.out.println ("This is imagessender!");
}
}

Video class

public class Videosender implements Sender {
public void Send () {
System.out.println ("This is imagessender!");
}
}

1-3 Building a factory class

public class Sendfactory {
Public Sender Produce (String type) {
if ("Mail". Equals (type)) {
return new Textsender ();
}
else if ("SMS". Equals (type)) {
return new Imagesender ();
}
else if ("video". Equals (type)) {
return new Videosender ();
}
else {
System.out.println ("Please enter the correct type!");
return null;
}
}
}

1-4 Normal Factory mode design has been completed. Below the test in main

public class Main {

public static void Main (string[] args) {
Sendfactory factory = new Sendfactory ();
Sender text=factory.produce ("text");
Text.send ();
Sender image=factory.produce ("image");
Image.send ();
Sender video=factory.produce ("video");
Video.send ();

}

}

Test results:

But this common factory method does not produce objects once the parameters are incorrect. In order to improve this way there is a multi-factory method, a method that produces a corresponding object, by invoking the method to produce the object

2-1 Multi-Factory mode

Just change the factory class in 1-3.

public class Sendfactory {
public static Sender Producetext () {
return new Textsender ();
}
public static Sender Produceimage () {
return new Imagesender ();
}
public static Sender Producevideo () {
return new Videosender ();
}
}

Test in Main

2-2

public class Main {

public static void Main (string[] args) {
Sendfactory factory = new Sendfactory ();
Sender text = Factory.producetext ();
Text.send ();
Sender image = Factory.produceimage ();
Image.send ();
Sender video = Factory.producevideo ();
Video.send ();

}

}

Test results such as

3. If the above example has a new class that is going to change the factory class and is not conducive to maintenance, we can use abstract Factory mode for this situation.

Or use the above as an example.

3-1. Establishing the Sender Interface

Public interface Sender {
public void Send ();
}

3-2 Create an implementation class to implement the sender interface, such as text and images and video classes

Text class

public class Textsender implements Sender {
public void Send () {
System.out.println ("This is textsender!");
}
}

Image class

public class Imagesender implements Sender {
public void Send () {
System.out.println ("This is imagessender!");
}
}

Video class

public class Videosender implements Sender {
public void Send () {
System.out.println ("This is imagessender!");
}
}

3-3 provide an interface for the factory class to implement this way I use the Offer interface

Public interface Offer {

Public Sender's offer ();

}

3-4 the corresponding class is produced by the corresponding factory.

public class Textsenderfactory implements offer {

Public Sender's offer () {

return new Textsender ();

}

}

public class Imagesenderfactory implements offer {

Public Sender's offer () {

return new Imagesender ();

}

}

public class Videosenderfactory implements offer {

Public Sender's offer () {

return new Videosender ();

}

}

3-5. Has finished to go to main test under

public class Main {

public static void Main (string[] args) {

Offer Textoffer=new textsenderfactory ();

Sender Text=textoffer.offer ();

Text.send ();

Offer Imageoffer=new imagesenderfactory ();

Sender Image=imageoffer.offer ();

Image.send ();

}

}

Test results:

Java Factory Methods Mode (Factory method)

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.