Java factory mode example

Source: Internet
Author: User

1. Simple Factory:

(1). Create an output interface

Package stone;
Public interface output {
Public int max_count = 10; // maximum number of records that can be printed
Public void show (); // print
Public void adddata (string MSG); // Add the records to be printed to the print order.
}

 

(2) create a printer class, which inherits the output interface

Package stone;
Public class printer implements output {
Private string [] data_array = new string [max_count]; // Save the array for printing information
Private int datanum = 0; // number of information to print
Public void adddata (string MSG ){
If (datanum> max_count) // The information to be printed cannot exceed the maximum value
System. Out. println ("print queue is Max ");
Else
Data_array [datanum ++] = MSG;
}
Public void show (){
While (datanum> 0 ){
System. Out. println ("Print:" + data_array [0]);
System. arraycopy (data_array, 1, data_array, 0, datanum --);
}
}
}

 

(3). Create an output in the factory, which defines which printer is used for printing.

Package stone;
Public class outputfactory {
Public static output (){
Return new printer ();
}
}

 

(4). Print the factory model:

Package stone;
Public class testsimplefactory {
Private output;
Public testsimplefactory (output ){
This. Output = output;
}
Private void dataIn (string MSG ){
Output. adddata (MSG );
}
Private void dataout (){
Output. Show ();
}
Public static void main (string [] ARGs ){
Testsimplefactory Sf = new testsimplefactory (outputfactory. Output ());
SF. dataIn ("Stone test simple factory 1 ");
SF. dataIn ("Stone test simple factory 2 ");
SF. dataout ();
}
}

 

The output result is as follows:

Print: Stone test simple factory 1
Print: Stone test simple factory 2

 

(5) add another printer class:

Package stone;
Public class printer2 implements output {
Private string [] data_array = new string [max_count];
Private int datanum = 0;
Public void adddata (string MSG ){
If (datanum> max_count)
System. Out. println ("print queue is Max ");
Else
Data_array [datanum ++] = MSG;
}
Public void show (){
While (datanum> 0 ){
System. Out. println ("OtherPrint: "+ data_array [0]);
System. arraycopy (data_array, 1, data_array, 0, datanum --);
}
}
}

 

(6) to use another printer, you only need to change the factory class.

Package stone;
Public class outputfactory {
Public static output (){
// Return new printer ();
Return new printer2 ();
}
}

 

(7) Run testsimplefactory and the result is as follows:

Other print: Stone test simple factory 1
Other print: Stone test simple factory 2

 

Bytes ------------------------------------------------------------------------------------------------

2. Factory method

(8) Add a printer in the factory:

Package stone;
Public class printerfactory {
Public static output (){
Return new printer ();
}
}

 

Package stone;
Public class printer2factory {
Public static output (){
Return new printer2 ();
}
}

 

(9). Add a factory for the printer Factory:

Package stone;
Public class outputfactoryfactory {
Public static output outputfactory (string MSG ){
If (msg. inclusignorecase ("printer2 "))
Return printer2factory. Output ();
Else
Return printerfactory. Output ();
}
}

 

(10) Run testfactorymethod to test the factory method:

Package stone;
Public class testfactorymethod {
Private output;
Public testfactorymethod (output ){
This. Output = output;
}
Private void dataIn (string MSG ){
Output. adddata (MSG );
}
Private void dataout (){
Output. Show ();
}
Public static void main (string [] ARGs ){
Testfactorymethod Sf = new testfactorymethod (outputfactoryfactory. outputfactory ("printer2 "));
SF. dataIn ("Stone test simple factory 1 ");
SF. dataIn ("Stone test simple factory 2 ");
SF. dataout ();
}
}

 

The test results are as follows:

Other print: Stone test simple factory 1
Other print: Stone test simple factory 2

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.