有没有专门做衣服搭配的网站,php做的网站如何发布,找个可以直接看的网站,漯河住房和城乡建设局网站目录
适配器模式#xff08;Adapter Pattern#xff09;
优缺点
使用场景
注意事项
代码实现 适配器模式#xff08;Adapter Pattern#xff09; 适配器模式#xff08;Adapter Pattern#xff09;是作为两个不兼容的接口之间的桥梁。将一个类的接口转化为客户希望的…目录
适配器模式Adapter Pattern
优缺点
使用场景
注意事项
代码实现 适配器模式Adapter Pattern 适配器模式Adapter Pattern是作为两个不兼容的接口之间的桥梁。将一个类的接口转化为客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
优缺点
1优点
可以让任何两个没有关联的类一起运行。
提高了类的复用。
增加了类的透明度。
灵活性好。
2缺点过多地使用适配器会让系统非常零乱不易整体进行把握。比如明明看到调用的是 A 接口其实内部被适配成了 B 接口的实现一个系统如果太多出现这种情况无异于一场灾难。因此如果不是很有必要可以不使用适配器而是直接对系统进行重构。
使用场景
有动机地修改一个正常运行的系统的接口这时应该考虑使用适配器模式。
注意事项 适配器不是在详细设计时添加的而是解决正在服役的项目的问题。
代码实现
package mainimport fmt// 新接口
type MusicPlayer interface {play(fileType string, fileName string)
}// 旧接口
type ExistPlayer struct {
}func (e *ExistPlayer) PlayMp3(filName string) {fmt.Println(play mp3:, filName)
}
func (e *ExistPlayer) PlayWma(fileName string) {fmt.Println(play wma:, fileName)
}// 适配器
type PlayerAdapter struct {existPlayer ExistPlayer
}func (p *PlayerAdapter) play(fileType string, fileName string) {switch fileType {case mp3:p.existPlayer.PlayMp3(fileName)case wma:p.existPlayer.PlayWma(fileName)default:fmt.Println(暂不支持此类型文件播放)}
}
func main() {player : PlayerAdapter{}player.play(mp3, nsjd)player.play(wma, 孤勇者)
}