In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to realize the factory method design pattern by Java". In the daily operation, I believe many people have doubts about how to realize the factory method design pattern by Java. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to realize the factory method design pattern by Java". Next, please follow the editor to study!
The factory method pattern enables us to create objects without exposing the creation logic to the client and to reference newly created objects using a common interface. It is one of the most widely used creative modes.
This mode is also known as a virtual constructor.
According to the Design Patterns of Gamma et al., the purpose of this model is:
Define the interface used to create the object, but let the subclass decide which class to instantiate. The Factory method allows classes to defer instantiation to subclasses.
The Factory method is used to create objects. The superclass specifies all standards and common behaviors, and then delegates the creation details to the subclasses provided by the client.
The Factory approach makes the design more customizable and more complex. Other design patterns require new classes, while Factory requires only new operations.
Structural body
The structure of the factory method pattern is shown in the following figure:
Figure: structure of the factory method pattern
Today, the methods of the static class are often used as factory methods to return objects of the desired class type. Unlike the constructor, the actual object it returns may be an instance of a subclass, or it may reuse an existing object instead of creating a new one.
One advantage of using a factory instead of a constructor is that factory methods can have different and more descriptive names.
Example
Let's consider an application that draws different geometric shapes according to customer requirements. The class diagram of the application is as follows:
Figure: example of factory method pattern
The ShapeFactory class has a static method getShape (), which returns the object based on the input of the GeometricShape string name.
Java implementation
We have introduced the Java implementation of the application discussed above.
Let's first make an interface factory for the product.
/ * Product interface * / Public interface GeometricShape {void draw ();}
The following is the implementation of the above interface:
/ * * concrete products * / public class Line implements GeometricShape {@ to overwrite public void draw () {system. Out. Println ("Line Drawn." );}} / * concrete products * / public class Rectangle implements GeometricShape {@ to overwrite public void draw () {system. Out. Println ("draw a rectangle." );}} / * concrete products * / public class Circle implements GeometricShape {@ to overwrite public void draw () {system. Out. Println ("draw circles". );}}
I added the following enumeration to name the shape:
Public enum ShapeType {LINE, CIRCLE, RECTANGLE, triangle}
Now, let's create a product that factory provides (in this case, GeometricShape):
/ * concrete products * / Public abstract class ShapeFactory {public static GeometricShape getShape (ShapeType name) {GeometricShape shape = null; switch (name) {case LINE: shape = new Line (); break; case CIRCLE: shape = new Circle (); break Case RECTANGLE: shape = new Rectangle (); break;} return shape;}}
The client of the application provides the name of the desired shape, as shown below.
/ * customer * / Public class application {public static void main (String [] args) {/ / request circle GeometricShape circle = ShapeFactory. GetShape (ShapeType. CIRCLE); if (circle! = null) {Circle. Draw ();} else {system. Out. Println ("this shape cannot be drawn." );} / / request a shape GeometricShape triangle = ShapeFactory that does not exist. GetShape (ShapeType. Triangle); if (triangle! = null) {triangle. Draw ();} else {system. Out. Println ("this shape cannot be drawn");}}
The output of the scheme is:
The circle is drawn. This shape can be drawn in tons.
Because there is a circle, Circle returns a valid object. However, there is no class with triangles, so the shape cannot be drawn.
At this point, the study on "how to implement the factory method design pattern in Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.