Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of Java factory design pattern

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to write the code of Java factory design pattern", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to write the code of Java factory design pattern" article.

Realization method

We will create a Shape interface and a concrete class that implements the Shape interface. A factory class ShapeFactory is defined in the next step.

FactoryPatternDemo this is a demo class that uses ShapeFactory to get a Shape object. It passes the information (CIRCLE/RECTANGLE/SQUARE) to ShapeFactory to get the desired object type.

The structure of implementing the factory pattern is shown in the following figure-

Java-61.jpg

Step 1

Create an interface-

Shape.java

PublicinterfaceShape {

Voiddraw ()

}

Step 2

Create a concrete class that implements the same interface. Several classes are shown below-

Rectangle.java

PublicclassRectangleimplementsShape {

@ Override

Publicvoiddraw () {

System.out.println ("InsideRectangle::draw () method.")

}

}

Square.java

PublicclassSquareimplementsShape {

@ Override

Publicvoiddraw () {

System.out.println ("InsideSquare::draw () method.")

}

}

Circle.java

PublicclassCircleimplementsShape {

@ Override

Publicvoiddraw () {

System.out.println ("InsideCircle::draw () method.")

}

}

Step 3

Create a factory to generate objects of a specific class based on the given information.

ShapeFactory.java

PublicclassShapeFactory {

/ / usegetShapemethodtogetobjectoftypeshape

PublicShapegetShape (StringshapeType) {

If (shapeType==null) {

Returnnull

}

If (shapeType.equalsIgnoreCase ("CIRCLE")) {

ReturnnewCircle ()

} elseif (shapeType.equalsIgnoreCase ("RECTANGLE")) {

ReturnnewRectangle ()

} elseif (shapeType.equalsIgnoreCase ("SQUARE")) {

ReturnnewSquare ()

}

Returnnull

}

}

Step 4

Use a factory to get objects of a specific class by passing information such as types.

FactoryPatternDemo.java

PublicclassFactoryPatternDemo {

Publicstaticvoidmain (String [] args) {

ShapeFactoryshapeFactory=newShapeFactory ()

/ / getanobjectofCircleandcallitsdrawmethod.

Shapeshape1=shapeFactory.getShape ("CIRCLE")

/ / calldrawmethodofCircle

Shape1.draw ()

/ / getanobjectofRectangleandcallitsdrawmethod.

Shapeshape2=shapeFactory.getShape ("RECTANGLE")

/ / calldrawmethodofRectangle

Shape2.draw ()

/ / getanobjectofSquareandcallitsdrawmethod.

Shapeshape3=shapeFactory.getShape ("SQUARE")

/ / calldrawmethodofcircle

Shape3.draw ()

}

}

Step 5

Verify that the output is as follows-

InsideCircle::draw () method.

InsideRectangle::draw () method.

InsideSquare::draw () method.

The above is the content of this article on "how to write the code of Java factory design pattern". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report