您的位置:首页 > 编程语言 > Java开发

设计模式之结构模式

2016-01-05 00:00 288 查看
摘要: 告别过程走向工程

设计模式之结构模式

一、概述

1.1 简述

告别面向过程: 从汇编到C,由于机器的执行都是通过有顺序的,我们的编程都是面向过程,可以极大的提高系统资源的利用率。当越来越多的项目需要快速实现,硬件设备越来越多的不是我们考虑的重点。项目越来越大,开发人员越来越多的时候,我们需要一种更加工程化的思想来分析项目。

走向工程化:从项目的可扩展,低耦合、开发周期等方面来思考问题,面向对象编程就是基于这一点提出的一种高度抽象的思维方式。将项目的各个模块从分解、求同、抽象,形成以功能模块为基点来完成项目工程。封装、基础、多态的出现可以让我们大大降低项目的耦合性、 从而提高稳定行和可扩展性。

1.2 结构模式

结构模式是功能的封装、基础、多态的主要使用实现。我们平时用的最多,也是最容易忽略的一种实现。常用的有以下几种实现。

二、实例

2.2 适配器模式

//旧功能已经存在,需要在原来的功能上添加新功能
//基类
public class Source {

public void method1() {
System.out.println("this is basic Source");
}
}

//想添加功能
public interface TargetTable {
// 旧方法
// public void method1();
/**
* @see 添加新的方法
*/
public void method2();
}

//适配器

public class Adapter extends Source implements TargetTable {

// Source实现了方法1,所有只需要实现方法2
@Override
public void method2() {
System.out.println("this is other method");
}

public static void main(String[] args) {
// 这中方法在集合中大量使用,例如ArrayList和LinkList,如果使用List做引用,则有一些方法没法实现
// TargetTable tt = new Adapter(); 如果是接口引用,则需要在接口里面实现一下旧接口
Adapter tt = new Adapter(); //如果直接用adapter类,直接调用就行
tt.method1();
tt.method2();
}

}


2.3 装饰模式

//共能还没实现,需要将功能用在不同的地方
//功能
public interface SourceInterf {

public void method();
}

//实现类
public class Source implements SourceInterf {

@Override
public void method() {
System.out.println("the original method!");
}

}

// 装饰器

public class Decorator implements SourceInterf {

/**
* @see 接口:需要实现的功能
* @see 基类:实现功能
* @see 装饰类:接受接口实现类,强化
*/
private SourceInterf sourceInterf;

public Decorator(SourceInterf sourceInterf) {
this.sourceInterf = sourceInterf;
}

@Override
public void method() {
System.out.println("before decorator!");
sourceInterf.method();
System.out.println("before decorator!");
}

public static void main(String[] args) {
SourceInterf source = new Source();
SourceInterf obj = new Decorator(source);
obj.method();

}
}


2.4 代理模式

//需要在旧的功能上实现新功能
//功能
public interface SourceInterf {

public void method();
}
//实现类
public class Source implements SourceInterf {

@Override
public void method() {
System.out.println("the original method!");
}

}
//代理类
public class Proxy implements SourceInterf {
/**
* @see 接口:需要实现的功能
* @see 基类:实现功能
* @see 装饰类:实例化接口实现类,强化
*/
private SourceInterf sourceInterf;

// 直接实例化
public Proxy() {
this.sourceInterf = new Source();
}

@Override
public void method() {
System.out.println("before decorator!");
sourceInterf.method();
System.out.println("before decorator!");
}

public static void main(String[] args) {
SourceInterf obj = new Proxy();
obj.method();
//1、修改原有的方法来适应。这样违反了“对扩展开放,对修改关闭”的原则。
//2、就是采用一个代理类调用原有的方法,且对产生的结果进行控制。这种方法就是代理模式。

}
}


2.5 享元模式

public class ConnectionP2 {
/**
* @see 享元模式:类似于池技术
*/
private String url = "jdbc:mysql//localhost:33016/test";
private String username = "root";
private String password = "root";
private String driverClassName = "com.mysql.jdbc.Driver";

// 空闲池:存放空闲连接
private Vector<Connection> freePool;
// 空闲个数
private int freeNumber = 100;
private int poolSize = 100;
private static ConnectionP2 instance = null;
Connection conn = null;

private ConnectionP2() {
freePool = new Vector<Connection>(poolSize);
for (int i = 0; i < poolSize; i++) {
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, username, password);
freePool.add(conn);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

// 空闲数+1
public void release() {
synchronized (ConnectionP2.class) {
freeNumber++;
}
}

// 返回一个、空闲数-1
public Connection getConnection() {
synchronized (ConnectionP2.class) {
if (!freePool.isEmpty()) {
conn = freePool.get(freeNumber);
freeNumber--;
}
return conn;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息