java结构型设计模式
主要分为:适配器模式、装饰模式、代理模式、外观模式、桥接模式、组合模式、享员模式
一、其中适配器模式是其它6种设计模式的起源,主要分为类的适配器、对象适配器、接口适配器
类的适配器代码示例:
public Interface Adapter{
public void method1();
public void method2();
}
public Class AdapterTestO{
public void method1(){
system.out.println("实现方法一");
}
}
public Class AdapterTestT extends AdapterTestT implements Adapter{
public void method2(){
system.out.println("实现方法二");
}
}
public Class Test{
public static void main(String[] args){
Adapter adapter = new AdapterTestT();
adapter.method1();
adapter.method2();
}
}
对象的适配器代码示例:
public Interface Adapter{
public void method1();
public void method2();
}
public Class AdapterTestO{
public void method1(){
system.out.println("实现方法一");
}
}
public Class AdapterTestT extends AdapterTestT implements Adapter{
private Adapter TestO testo;
public AdapterTestT(Adapter TestO testo){
this.test0 = testo;
}
public void method1(){
testo.method1();
}
public void method2(){
system.out.println("实现方法二");
}
}
public Class Test{
public static void main(String[] args){
AdapterTestO Testo = new AdapterTestO();
Adapter adapter = new AdapterTestT(Testo);
adapter.method1();
adapter.method2();
}
}
接口的适配器示例:
接口适配器是某一个接口的方法,其实现类并不想全部实现,只是根据自己的需要实现某些方法,这个时候就需要一个抽象类
实现该接口,并由其实现类继承该抽象类
代码示例:
public Interface Adapter{
public void method1();
public void method2();
}
public Abstract Class AbstractAdapter{
public void method1(){};
public void method2(){};
}
public Class Adapter1 extends Adapter{
public void method1(){
system.out.println("实现方法一");
}
public Classs Adapter2 extends Adapter{
public void method2(){
system.out.println("实现方法二")
}
}
}
public Class PatterTest{
public static void main(String[] args){
Adapter adapter1= new Adapter1();
Adapter adapter2= new Adapter2();
adapter1.method1();
adapter2.method2();
}
}
装饰模式
顾名思义该模式主要是,通过某些类对其它类进行修改或添加功能达到装饰的目的,但要求装饰对象和被装饰对象实现同一接口
代码示例:
public Interface Source{
public void method();
}
public Class Sourceable implements Source{
public void method(){
system.out.println("被修饰的方法");
}
}
public Class Decoration implements Source{
pirvate Source source;
public Decoration(Source source){
this.source= source;
}
public void method(){
system.out.println("装饰前.....");
source.method();
system.out.println("装饰后.....");
}
}
public Class PatternTest{
public static void main(String[] args){
Source source = new Sourceable();
Source descro = new Decoration(source);
descro.method();
}
}
代理模式
多出一个代理类,替原对象做某些事情,比如去外地租房子,由于对周围的环境不够熟悉,这个时候就需要找一个中介帮忙找房子,中介就相当于代理
public Interface Source{
public void method();
}
public Class Sourceable implements Source{
public void method(){
system.out.println("被代理的对象");
}
}
public Class Proxy implements Source{
private Sourceable sourceable;
public Proxy(){
this.sourceable = new Sourceable();
}
public void method(){
system.out.println("代理前....");
sourceable.method();
system.out.println("代理后....");
}
}
public Class PatternTest{
public static void main(String[] args){
Source source = new Proxy();
source.method();
}
}
外观模式
主要降低类与类之间的耦合度,减少实例与实例之间的相互关系
代码示例:
public Class Cpu{
public void startUp(){
system.out.println("Cpu启动");
}
public void shutDown(){
system.out.println("Cpu关闭");
}
}
public Class Memery{
public void startUp(){
system.out.println("内存启动");
}
public void shutDown(){
system.out.println("内存关闭");
}
}
public Class Disk{
public void startUp(){
system.out.println("磁盘启动");
}
public void shutDown(){
system.out.println("磁盘关闭");
}
}
public Class Computer{
private Cpu cpu;
private Memery memery;
private Disk disk;
public computer(){
this.cpu = new Cpu();
this.memery = new Memery();
this.disk = new Disk();
}
public void startUp(){
system.out.println("电脑启动开始");
cpu.startUp();
memery.startUp();
disk.startUp();
system.out.println("电脑启动结束");
}
public void shutDown(){
system.out.println("电脑关闭开始");
cpu.shutDown();
memery.shutDown();
disk.shutDown();
system.out.println("电脑关闭结束");
}
}
public Class PatternTest{
public static void main(String[] args){
Computer computer = new Computer();
computer.startUp();
computer.shutDown();
}
}
桥接模式
该模式是把事物和具体实现分开,实现抽象和具体实现类解耦,通常是用一个做桥梁的类来实现,比如jdbc连接数据库时,在数据库切换时根本不需要改动什么代码,主要是由于DriverManager做桥梁的作用
代码示例:
public Interface Source{
public void method();
}
public Class Sourceable1 implements Source{
public void method(){
system.out.println("实现方法一");
}
}
public Class Sourceable2 implements Source{
public void method(){
system.out.println("实现方法二");
}
}
public Abstract Class Bridge{
private Source source;
public void method(){
source.method();
}
public Source getSource(){
return source;
}
public void setSource(Source source){
this.source = source;
}
}
public Class MyBridge extends Bridge{
public void method(){
getSource().method();
}
}
public Class PatternTest{
public static void main(String[] args){
Bridge bridge = new MyBridge();
Source source1 = new Sourceable1();
bridge.setSource(source1);
bridge.method();
Source source2 = new Sourceable2();
bridge.setSource(source2);
bridge.method();
}
}