Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
lihengming
java-codes
Repository navigation
Code
Issues
1
(1)
Pull requests
Actions
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
java-codes
/
design-pattern
/
src
/
main
/
java
/
creational
/
FactoryMethodPattern.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
66 lines (61 loc) · 2 KB
master
Breadcrumbs
java-codes
/
design-pattern
/
src
/
main
/
java
/
creational
/
FactoryMethodPattern.java
Copy path
Top
File metadata and controls
Code
Blame
66 lines (61 loc) · 2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package creational;
/**
* Created by 李恒名 on 2017/6/14.
* <p>
* 工厂方法模式
*/
public class FactoryMethodPattern {
//汽车抽象
static abstract class Car {
abstract void run();
}
//奔驰汽车
static class Benz extends Car {
@Override
void run() {
System.out.println("奔驰车在跑");
}
}
//宝马汽车
static class BMW extends Car {
@Override
void run() {
System.out.println("宝马车在跑");
}
}
//抽象的汽车工厂
static abstract class CarFactory {
public abstract Car getCar();
}
//奔驰汽车工厂
static class BenzFactory extends CarFactory {
@Override
public Car getCar() {
return new Benz();
}
}
//宝马汽车工厂
static class BMWFactory extends CarFactory {
@Override
public Car getCar() {
return new BMW();
}
}
public static void main(String[] args) {
CarFactory factory = new BenzFactory();
//CarFactory factory = new BMWFactory();
Car car = factory.getCar();
car.run();
}
/**
* 总结:
* 作为抽象工厂模式的孪生兄弟,工厂方法模式定义了一个创建对象的接口,但由子类决定要
* 实例化的类是哪一个,也就是说工厂方法模式让实例化推迟到子类。
*
* 工厂方法模式非常符合“开闭原则”,当需要增加一个新的产品时,我们只需要增加一个具
* 体的产品类和与之对应的具体工厂即可,无须修改原有系统。同时在工厂方法模式中用户只
* 需要知道生产产品的具体工厂即可,无须关系产品的创建过程,甚至连具体的产品类名称都
* 不需要知道。虽然他很好的符合了“开闭原则”,但是由于每新增一个新产品时就需要增加
* 两个类,这样势必会导致系统的复杂度增加。
*/
}
You can’t perform that action at this time.