当前位置:网站首页>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.边栏推荐
- Where is the most formal and safe account opening for speculation futures? How to open a futures account?
- What is a framework?
- Annual contribution! Tencent cloud middleware product upgrade conference is in hot registration!
- Virtual machine virtual disk recovery case tutorial
- Goby+awvs realize attack surface detection
- Comparison of jmeter/k6/locust pressure measuring tools (not completed yet)
- MySQL Innodb和Myisam
- Week7 weekly report
- CDs view permission check
- The mystery of redis data migration capacity
猜你喜欢

Ps\ai and other design software pondering notes
MySQL Advanced Series: locks - locks in InnoDB

Applet - use of template

B. Ternary Sequence(思维+贪心)Codeforces Round #665 (Div. 2)

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

Some adventurer hybrid versions with potential safety hazards will be recalled

Applet wxss
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

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

C. K-th Not Divisible by n(数学+思维) Codeforces Round #640 (Div. 4)
随机推荐
[golang] Introduction to golang (I) establishment of running environment
Teach you to write a classic dodge game
Funny! Pictures and texts give you a comprehensive understanding of the effects of dynamics and mass
Memo list: useful commands for ffmpeg command line tools
Global and Chinese markets of natural insect repellents 2022-2028: Research Report on technology, participants, trends, market size and share
Transpose convolution learning notes
What is cloud development? Why cloud development? Talk about our story
Experience and suggestions on cloud development database
AI structured intelligent security video monitoring technology, supporting the protective umbrella of the reserve / wild animals
B. Terry sequence (thinking + greed) codeforces round 665 (Div. 2)
Goby+AWVS 实现攻击面检测
[tke] troubleshooting tips for container problems
During JMeter pressure measurement, time_ The number of requests does not go up due to many waits. The problem is solved
Global and Chinese market of training dance clothes 2022-2028: Research Report on technology, participants, trends, market size and share
Cognition and difference of service number, subscription number, applet and enterprise number (enterprise wechat)
[go] runtime package for concurrent programming and its common methods
What does the router pin mean?
ZOJ——4104 Sequence in the Pocket(思维问题)
What is Ethernet
Video structured intelligent analysis platform easycvr video recording plan function optimization / regularly delete expired videos