当前位置:网站首页>Factory mode
Factory mode
2022-06-24 16:33:00 【HLee】
Create an interface :Shape.java
public interface Shape {
void draw();
}Create entity classes that implement interfaces :Rectangle.java,Square.java,Circle.java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}Create a factory , Generate object of entity class based on given information :ShapeFactory.java
public class ShapeFactory {
// Use getShape Method to get the shape type object
public Shape getShape(String shapeType){
if(shapeType == null){
return null;
}
if(shapeType.equalsIgnoreCase("CIRCLE")){
return new Circle();
} else if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
} else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}Using the plant , Get the object of entity class by passing type information :FactoryPatternDemo.java
public class FactoryPatternDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
// obtain Circle The object of , And call its draw Method
Shape shape1 = shapeFactory.getShape("CIRCLE");
// call Circle Of draw Method
shape1.draw();
// obtain Rectangle The object of , And call its draw Method
Shape shape2 = shapeFactory.getShape("RECTANGLE");
// call Rectangle Of draw Method
shape2.draw();
// obtain Square The object of , And call its draw Method
Shape shape3 = shapeFactory.getShape("SQUARE");
// call Square Of draw Method
shape3.draw();
}
}
Output :
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.边栏推荐
- TRTC web end imitation Tencent conference microphone mute detection
- Global and Chinese market of inverted syrup 2022-2028: Research Report on technology, participants, trends, market size and share
- [play with Tencent cloud] my operation strategy from domain name application to website filing in Tencent cloud
- 【prometheus】1. Monitoring overview
- Batch BOM Bapi test
- MySQL Advanced Series: locks - locks in InnoDB
- Ui- first lesson
- Tencent releases the full platform version of reasoning framework TNN, and supports mobile terminal, desktop terminal and server terminal at the same time
- 2021-04-29: given an array arr, it represents a row of balloons with scores. One for each blow
- Snowflake algorithm implemented in go language
猜你喜欢

There are potential safety hazards Land Rover recalls some hybrid vehicles

A survey of training on graphs: taxonomy, methods, and Applications

ZOJ——4104 Sequence in the Pocket(思维问题)

Applet - use of template

C. Three displays(动态规划)Codeforces Round #485 (Div. 2)

Problems encountered in the work of product manager

Some adventurer hybrid versions with potential safety hazards will be recalled

C. Three displays codeforces round 485 (Div. 2)
MySQL Advanced Series: locks - locks in InnoDB

Ui- first lesson
随机推荐
Go deep into the implementation principle of go language defer
【prometheus】1. Monitoring overview
50 growers | closed door meeting of marketing circle of friends ス gathering Magic City thinking collision to help enterprise marketing growth
Cloud + community [play with Tencent cloud] video solicitation activity winners announced
山金期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
B. Ternary Sequence(思维+贪心)Codeforces Round #665 (Div. 2)
Embedded Software Engineer written interview guide arm system and architecture
One Minute! No code! Add [statistical analysis] to the website
Is Shanjin futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
What is the difference between a network card and a port
Introduction to new features of ECMAScript 2019 (ES10)
Some experiences of project K several operations in the global template
Dismantle the industrial chain of synthetic rubber industry, and the supply chain may become a sharp weapon for breakthrough
AI video structured intelligent security platform easycvr realizes intelligent security monitoring scheme for procuratorate building
A set of very good H3C and Tianrongxin Internet cutover scheme templates, with word document download
Where is the most formal and safe account opening for speculation futures? How to open a futures account?
Tencent blue whale Zhiyun community version v6.0.3 was officially released together with the container management platform!
How does the effective date of SAP PP ECM affect the work order?
MySQL InnoDB and MyISAM
Applet wxss