设计模式
设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。(就是前人总结的套路)
使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;设计模式是软件工程的基石脉络,如同大厦的结构一样。
单例模式
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。
单个实例,唯一对象。
- 静态属性
- 静态方法获取属性
- 构造方法私有
//饿汉式
class ClassA{
private static final ClassA instance = new ClassA();
public static ClassA newInstance(){
return instance;
}
private ClassA(){}
}
//懒汉式
class ClassB{
private static ClassB instance = null;
public static synchronized ClassB newInstance(){
if (instance == null) instance = new ClassB();
return instance;
}
private ClassB(){}
}
//结合
class ClassC{
private static class Holder{
public static ClassC instance = new ClassC();
}
public static ClassC newInstance(){
return Holder.instance;
}
private ClassC(){}
}
简单工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。即由一个工厂方法或工厂对象帮助完成对象创建的工作,会给你系统带来更大的可扩展性和尽量少的修改量。
import java.io.FileReader;
import java.util.Properties;
public class TestFactory {
public static void main(String[] args) {
Animal a = createAnimal();
a.eat();
}
static Animal createAnimal(){
try {
Properties ps = new Properties();
FileReader fr = new FileReader("config.txt");//config.txt文件中写classname = 类名;读取时会根据config.txt信息创建对象
ps.load(fr);
fr.close();
String className = ps.getProperty("classname").trim();
Class c = Class.forName(className);
Object o = c.newInstance();
return (Animal) o;
} catch (Exception e) {
return null;
}
}
}
abstract class Animal{
public abstract void eat();
}
class Dog extends Animal{
public void eat(){
System.out.println("Dog eat");
}
}
class Cat extends Animal{
public void eat(){
System.out.println("Cat eat");
}
}
class Tiger extends Animal{
public void eat(){
System.out.println("Tiger eat");
}
}
代理模式
主要解决:在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上。在面向对象系统中,有些对象由于某些原因(比如对象创建开销很大,或者某些操作需要安全控制,或者需要进程外的访问),直接访问会给使用者或者系统结构带来很多麻烦,我们可以在访问此对象时加上一个对此对象的访问层。
举个为方法添加计时器的例子
package com.Design_Pattern;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
public class TestProxy {
public static void main(String[] args) {
Service t1 = new Target();
Service p1 =(Service) createProxy(t1);
p1.m1();//有计时功能
p1.m2();
p1.m3();
Service2 t2 = new Target2();
Service2 p2 = (Service2)createProxy(t2);
p2.m4();
p2.m5();
p2.m6();
//List list = new ArrayList();
//List proxy =(List) createProxy(list);
//proxy.add("A");//都添加上了代理中的计时功能
//proxy.remove(0);
//proxy.size();
//System.out.println(list.getClass().getName());
//System.out.println(proxy.getClass().getName());//动态代理实际上是利用反射在运行时动态生成代理类。
}
//为target对象生成一个用于计时的代理对象
public static Object createProxy(Object target){
Object o = Proxy.newProxyInstance(
target.getClass().getClassLoader(),
target.getClass().getInterfaces(),
new InvocationHandler(){
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
long t1 = System.nanoTime();
//调用目标对象的相应方法
Object result = method.invoke(target,args);
long t2 = System.nanoTime();
System.out.println(t2-t1);
return result;
}
}
);
return o;
}
}
interface Service{
void m1();
void m2();
void m3();
}
class Target implements Service{
public void m1(){
System.out.println("Target m1");
}
public void m2(){
System.out.println("Target m2");
}
public void m3(){
System.out.println("Target m3");
}
}
interface Service2{
void m4();
void m5();
void m6();
}
class Target2 implements Service2{
public void m4(){
System.out.println("Target2 m4");
}
public void m5(){
System.out.println("Target2 m5");
}
public void m6(){
System.out.println("Target2 m6");
}
}