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 }}
finalxcode
Design-Patterns
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Design-Patterns
/
ChartDisplay.php
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
40 lines (32 loc) · 1.6 KB
master
Breadcrumbs
Design-Patterns
/
ChartDisplay.php
Copy path
Top
File metadata and controls
Code
Blame
40 lines (32 loc) · 1.6 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
<?php
namespace DesignPatterns;
/**
* Class ChartDisplay
* @package DesignPatterns
*/
class ChartDisplay
{
const CHART_TYPE_PIE = 'pie';
const CHART_TYPE_LINE = 'line';
public function display($type) {
if ($type == self::CHART_TYPE_PIE) {
$pieChart = new PieChart();
$pieChart->display();
} else if ($type == self::CHART_TYPE_LINE) {
$lineChart = new LineChart();
$lineChart->display();
}
}
}
// 开闭
//上面一个图表显示类可以显示饼图和折线图,如果这个时候我们想要显示柱状图,怎么办?
//就需要增加一个柱状图的类,同时修改chartdispaly类中display的显示方法,这就破坏了开闭原则。
//开闭原则要求对扩展开放,对修改关闭。虽然我们实现了扩展,但是修改了chartdisplay类
//有没有办法来达到我们既支持扩展,又对修改关闭呢?
//我们定义abstractChartDisplay抽象类,让piechart和linechart类作为其子类
// 依赖倒置
//上层模块不应该依赖下层模块,两者都应该依赖其抽象
//在这里我们将chartDisplay看做上层模块,它依赖pieChart和lineChart模块,也就是下层模块
//这样是没有问题,但是对于扩展性不好,因此在开闭原则里面,我们抽象出了abstractChartDisplay
//让下层模块pieChart和lineChart模块,依赖这个抽象abstractChartDisplay,下层模块来实现这个抽象abstractChartDisplay
//从而将依赖实现了从上层模块依赖下层模块,转变成了下层模块要依赖上层抽象的转变, 这就是依赖倒置
You can’t perform that action at this time.