概述
1、适配器模式
1.1类适配器模式
package com.yqj.pattern.adapter.classadapter;
//被适配的类
class Voltage220V{
public int output220V(){
int src = 220;
System.out.println("输出电压="+src+"V");
return src;
}
}
//适配器接口
interface Voltage5V{
int output5V();
}
//类适配器
class VoltageAdapter extends Voltage220V implements Voltage5V{
//实现适配,将220V电压转换为5V电压
@Override
public int output5V() {
//调用父类的方法
int src = output220V();
//转换电压
int dst = src / 44;
return dst;
}
}
//手机
class Phone{
public void charging(Voltage5V voltage5V){
if(voltage5V.output5V() == 5){
System.out.println("可以充电 电压5V");
}else {
System.out.println("不可充电");
}
}
}
//用户
public class Client {
public static void main(String[] args) {
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}
1.2.对象适配器模式
package com.yqj.pattern.adapter.objectadapter;
//被适配的类
class Voltage220V{
public int output220V(){
int src = 220;
System.out.println("输出电压="+src+"V");
return src;
}
}
//适配器接口
interface Voltage5V{
int output5V();
}
//对象适配器
class VoltageAdapter implements Voltage5V{
Voltage220V voltage220V;
//聚合被适配对象
public VoltageAdapter(Voltage220V voltage220V) {
this.voltage220V = voltage220V;
}
//实现适配,将220V电压转换为5V电压
@Override
public int output5V() {
int src = voltage220V.output220V();
//转换电压
int dst = src / 44;
return dst;
}
}
//手机
class Phone{
public void charging(Voltage5V voltage5V){
if(voltage5V.output5V() == 5){
System.out.println("可以充电 电压5V");
}else {
System.out.println("不可充电");
}
}
}
//用户
public class Client {
public static void main(String[] args) {
Phone phone = new Phone();
phone.charging(new VoltageAdapter(new Voltage220V()));
}
}
1.3接口适配器
package com.yqj.pattern.adapter.abstractadapter;
interface Life{
void eat();
void sing();
void movie();
}
abstract class AbsAdapter implements Life{
@Override
public void eat() {
}
@Override
public void sing() {
}
@Override
public void movie() {
}
}
public class Client {
public static void main(String[] args) {
AbsAdapter adapter = new AbsAdapter(){
@Override
public void eat() {
System.out.println("吃顿好的");
}
};
adapter.eat();
}
}
2、桥接模式
package com.yqj.pattern.bridge;
interface Brand{
void open();
void call();
void close();
}
class Vivo implements Brand{
@Override
public void open() {
System.out.println("Vivo开机");
}
@Override
public void call() {
System.out.println("Vivo打电话");
}
@Override
public void close() {
System.out.println("Vivo关机");
}
}
class XiaoMi implements Brand{
@Override
public void open() {
System.out.println("小米开机");
}
@Override
public void call() {
System.out.println("小米打电话");
}
@Override
public void close() {
System.out.println("小米关机");
}
}
abstract class Phone{
Brand brand;
public Phone(Brand brand) {
this.brand = brand;
}
protected void open(){
brand.open();
}
protected void call(){
brand.call();
}
protected void close(){
brand.close();
}
}
class FoldedPhone extends Phone{
public FoldedPhone(Brand brand) {
super(brand);
}
public void open(){
super.open();
System.out.println("折叠样式手机");
}
public void call(){
super.call();
System.out.println("折叠样式手机");
}
public void close(){
super.close();
System.out.println("折叠样式手机");
}
}
class UpRightPhone extends Phone{
public UpRightPhone(Brand brand) {
super(brand);
}
public void open(){
super.open();
System.out.println("直立样式手机");
}
public void call(){
super.call();
System.out.println("直立样式手机");
}
public void close(){
super.close();
System.out.println("直立样式手机");
}
}
public class Client{
public static void main(String[] args) {
Phone phone = new FoldedPhone(new XiaoMi());
phone.open();
phone.call();
phone.close();
System.out.println("+++++++++++++");
UpRightPhone phone2 = new UpRightPhone(new Vivo());
phone2.open();
phone2.call();
phone2.close();
}
}
最后
以上就是专注保温杯为你收集整理的设计模式--适配器模式和桥接模式的全部内容,希望文章能够帮你解决设计模式--适配器模式和桥接模式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复