Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
VentureQ committed Jul 9, 2021
1 parent 098cbbc commit 7b72e1a
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Design_Pattern.适配器模式.对象适配器模式;

public class Phone {
public void typecPhone() {
System.out.println("信息从Typec口的手机输出。");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Design_Pattern.适配器模式.对象适配器模式;

public class Test {
public static void main(String[] args) {
Typec2Vga2 typec2Vga1 = new Typec2Vga2(new Phone());
typec2Vga1.vgaInterface();//适配器将typec转换成vga
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Design_Pattern.适配器模式.对象适配器模式;

public class Typec2Vga2 implements Vga{

private Phone phone;

public Typec2Vga2(Phone phone) {
// TODO Auto-generated constructor stub
this.phone = phone;
}

@Override
public void vgaInterface() {
// TODO Auto-generated method stub
if(phone != null) {
phone.typecPhone();
System.out.println("接收到Type-c口信息,信息转换成VGA接口中...");
System.out.println("信息已转换成VGA接口,显示屏可以对接。");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Design_Pattern.适配器模式.对象适配器模式;

public interface Vga {
void vgaInterface();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Design_Pattern.适配器模式.接口适配器模式;

public abstract class Adapter implements Target{
public void typec() { }
public void typec2vga() { }
public void typec2hdmi() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Design_Pattern.适配器模式.接口适配器模式;

public interface Target {
void typec();
void typec2vga();
void typec2hdmi();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Design_Pattern.适配器模式.接口适配器模式;

public class Test {
public static void main(String[] args) {
VgaAdapter vgaAdapter = new VgaAdapter();
vgaAdapter.typec();
vgaAdapter.typec2vga();//适配器将typec转换成vga
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package Design_Pattern.适配器模式.接口适配器模式;

public class VgaAdapter extends Adapter{

public void typec() {
System.out.println("信息从Typec口的手机输出。");
}

public void typec2vga() {
System.out.println("接收到Type-c口信息,信息转换成VGA接口中...");
System.out.println("信息已转换成VGA接口,显示屏可以对接。");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package Design_Pattern.适配器模式.类适配器模式;

public class Phone {
public void typecPhone() {
System.out.println("信息从Typec口的手机输出。");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package Design_Pattern.适配器模式.类适配器模式;

public class Test {
public static void main(String[] args) {
Vga vga=new Typec2Vga1();
vga.vgaInterface();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package Design_Pattern.适配器模式.类适配器模式;

public class Typec2Vga1 extends Phone implements Vga{
@Override
public void vgaInterface() {
// TODO Auto-generated method stub
typecPhone();
System.out.println("接收到Type-c口信息,信息转换成VGA接口中...");
System.out.println("信息已转换成VGA接口,显示屏可以对接。");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package Design_Pattern.适配器模式.类适配器模式;

public interface Vga {
void vgaInterface();
}

0 comments on commit 7b72e1a

Please sign in to comment.