您的位置:首页 > 其它

设计模式

2017-04-22 20:49 99 查看
设计模式:

简单工厂模式:

1.创建最基本的属性接口;

2.基本属性,使用工厂方法;

3.分别使用具有该属性的类实行该接口;

4.分别在各自的类,实现最基本属性的工厂接口;

5.测试;

基本接口:

public interface Animal {

 

}

创建Cat和Dog类实现Animal接口;

创建AnimalFactory接口,里面有抽象方法

public interface AnimalFactory {

public abstract Animal createAnimal();

}

分别创建CatFactory和DogFactory实现AnimalFactory接口;

测试类:

public class FactoryDemo {

public static void main(String[] args) {

AnimalFactory factory=new DogFactory();

Animal dog=factory.createAnimal();

AnimalFactory factory2=new CatFactory();

Animal cat=factory2.createAnimal();

}

}

单例模式:

饿汉式:

1.声明静态成员对象;

2.私有化构造函数;

3.获得对象;

代码:

public class Student {

private static Student s1=new Student();

private Student(){

}

public static Student getStudent(){

return s1;

}

}

懒汉式:

public class Teacher {

private static Teacher t=null;

private Teacher(){

}

public static Teacher getTeacher(){

if(t==null){

return new Teacher();

}else{

return t;

}

}

}

区别:饿汉式:类一加载就创建对象;

      懒汉式:用的时候才加载对象,会出现线程安全问题;

相同点:都是私有化构造函数;

 

模板设计模式:

特点:执行代码用时计算时间;

1.在程序的开始获取时间;

2.程序执行结束后获取时间;

3.计算两个之间的时间差;

代码:

public class GetTime {

public long getTime(){

long start=System.currentTimeMillis();

// int sum=0;

// for(int i=0;i<100000;i++){

// sum+=i;

// }

try {

BufferedInputStream bis=new BufferedInputStream(new FileInputStream("1.wmv"));

BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("2.wmv"));

byte[] bys=new byte[1024];

int len=0;

while((len=bis.read(bys))!=-1){

bos.write(bys, 0, len);

}

bos.close();

bis.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

long end=System.currentTimeMillis();

return end-start;

}

}

装饰者模式:

思路:比如一款手机(抽象),最初它只有打电话的功能,后来加上播放音乐和彩铃功能,不断完善就是装饰者模式;

1.创建一个基本Phone接口,里面有拨打电话的方法;

public interface Phone {

public void call();

}

2.创建Iphone类,实现Phone接口,并实现call方法;

3.创建PhoneDecorate类实现Phone接口;将Phone对象传入到PhoneDecorate构造方法中;使用phone对象调用call()方法;

public class PhoneDecorate implements Phone{

private Phone p=null;

public PhoneDecorate(Phone p){

this.p=p;

}

@Override

public void call() {

// TODO Auto-generated method stub

p.call();

}

}

4.为了增加播放彩铃和音乐的功能,分别创建MusicPhoneDecorate和SingPhoneDecorate类继承PhoneDecorate
类,在call方法中增加播放音乐和彩铃功能;

public class MusicPhoneDecorate extends PhoneDecorate {

 

public MusicPhoneDecorate(Phone p) {

super(p);

// TODO Auto-generated constructor stub

}

public void call(){

super.call();

System.out.println("可以播放音乐了");

}

}

public class SingPhoneDecorate extends PhoneDecorate {

 

public SingPhoneDecorate(Phone p) {

super(p);

// TODO Auto-generated constructor stub

}

public void call(){

System.out.println("手机可以听彩铃了");

super.call();

}

}

测试方法:

public static void main(String[] args) {

Phone p=new Iphone();

PhoneDecorate pd2=new SingPhoneDecorate(new MusicPhoneDecorate(p));

pd2.call();

}

这和输入输出流很相似,例如:BufferedInputStream bis=new BufferedInputStream(new FileInputStream("1.wmv"));

实际上IO流是最典型的装饰者模式;

 

适配器模式:

1.创建ActionListener接口;

2.创建ActionAdapter实现ActionListener接口;

3.使用ButtonAction继承ActionAdapter,如想实现其中的方法,直接调用即可;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息