enochmcbride

July 13, 2010

Factory mode advantages

Filed under: Uncategorized

The words too literally

The constructor is concerned, its name must belong to the same class name. This is often lost the use of function names convey more information. For example, we have a class to create XML Parser:

1 public class XMLConverter () inmplements Converter (

2

3 / / create a default converter, Idoc Converter

4

5 public XMLConverter () (

6

7 … … … … … … … … … … ….

8

9)

10

11 / / create a specific xml parser, idoc / bapi

12

13 public XMLConverter (String xmlConverterType) (

14

15 … … … … … … ….

16

17)

18

19)

20

21

This XMLConverter class has two constructors, the first constructor creates a default XML converter. The second constructor can be specified according to the programmer to create an XML conveter. To use this class, we must first read API documentation and we may be difficult to remember these two constructors of their respective purposes. We see how to use the Factory to solve the following problem:

1 public class XMLConverter () (

2

3 public Converter getIdocConverter () (

4

5 return new XML ();

6

7)

8

9 public static Converter getBapiConverter () (

10

11 return new XMLConverter (”bapi”);

12

13)

14

15 / / create a default converter, Idoc Converter

16

17 private static Converter XMLConverter () (

18

19 … … … … … … … … … … ….

20

21)

22

23 / / create a specific xml parser, idoc / bapi

24

25 private XMLConverter (String xmlParserType) (

26

27 … … … … … … ….

28

29)

30

31)

32

33

This new XMLConverter class has two get functions, they return two XMLConverter type Object. This is a typical Factory mode. These two get the function name that clearly tell us their own purposes, we do not check the API documentation can understand their use. Please pay special attention to the two constructors are marked as pivate.

Can be controlled within a JVM can generate a number of types of Object

We all know that to generate a database Connection object is a process affecting the speed of operation, so “connection pool '(connection pooling) is a commonly used techniques: procedures to maintain a certain number of Connection object, in order to continue to reuse. Such a to the greatly reduced production and destruction of Connection object time. This requires us to control up to create a Connection object. Now we look like and use Factory mode to solve this problem:

1 import java.sql .*;

2

3 import java.util .*;

4

5 pulbic class ConnectionPool (

6

7 private static final int NUM_OF_CONNECTION = 50;

8

9 private static int counter = 0;

10

11 private Vector connectionPool = new Vector ();

12

13 private ConnectionPool () (

14

15)

16

17 public static Connection getConnection () throws Exception (

18

19 Connection connection = null;

20

21 if (connectionPool.size ()

22

23 connection = Class.forName (”my.sql.Driver”).

24

25 getConnection (”dbc: oracle: thin: @ 192.168.100.36:1521: ora9i”);

26

27 connectionPool.add (connection);

28

29) else (

30

31 if (conuter

32

33 counter + +;

34

35 else counter = 0;

36

37 connection = (Connection) connectionPool.elementAT (counter%

38

39 NUM_OF_CONNECTION);

40

41)

42

43 return connection;

44

45)

46

47)

48

Above this short program shows how to use Factory mode to maintain 50 Connection Object: When the number is less than the specified number of Connection, we have continued to create the Connection, up to 50 Connection Object is created. After that, we will continue to reuse the Object.

Examples described by the above author possess the Factory pattern constructors do not possess two advantages, we can be put to use in practice.

Recommand Link:

Simple FTP Clients

Apple AVI to FLV

mp4 to 3gp converter FREE

Free mp4 to mpeg converter

Hope DVD YouTube Video to Pocket PC

Articles about Office Suites And Tools

Youtube Movie To MPEG Professional

Stamp ID3 Tag Editor

How-to Youtube Converter

Convert mp4 to mp3

Lohan DVD TO Xbox 360

swf FORMAT

Audio CD/DVD Dumper

Comments »

The URI to TrackBack this entry is: http://enochmcbridefm.blogsome.com/2010/07/13/factory-mode-advantages/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hermanitos Verdes