设计模式:
是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性以及代码的结构更加清晰.
简单工厂模式(静态工厂模式):
定义一个具体的工厂类负责创建一些类的创建,静态工厂模式在不改变客户端代码的情况下可以动态增加产品,明确了类的职责,但是静态工厂类如果需要增加新的对象,就需要不断地修改工厂类,不利于后期的维护
package org.westos.demo;//工厂类
public class AnimalFactory {
private AnimalFactory() {
super();
// TODO Auto-generated constructor stub
}
public static Animal createAnimal(String animalType) {
if (animalType.equals("dog")) {
return new Dog();
} else if (animalType.endsWith("cat")) {
return new Cat();
} else {
return null;
}
}
}
package org.westos.demo;//动物类
public class Animal {
public void eat() {
};
}
package org.westos.demo;//猫类
public class Cat extends Animal{
public void eat() {
System.out.println("猫吃鱼");
}
}
package org.westos.demo;//狗类
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
package org.westos.demo;//测试类
public class Test {
public static void main(String[] args) {
Animal animal = AnimalFactory.createAnimal("dog");
animal.eat();
Animal animal2 = AnimalFactory.createAnimal("cat");
animal2.eat();
}
}
工厂模式
抽象工厂类负责定义创建对象的接口,具体对象的创建工作由继承抽象工厂的具体类实现
客户端不需要再负责对象的创造,从而明确了个各类的职责,如果有新的对象,只需要增加一个具体的工厂类即可,不影响已有的代码,后期维护容易,增加了系统的扩展性,但是需要额外的编写代码,增加了工作量
package org.westos.demo2;//最大的工厂类
//相当于一个大的动物工厂
public interface AinmalFactory {
public abstract Animal creatAnimal();
}
package org.westos.demo2;//老虎工厂
public class TigerFactory implements AinmalFactory {
@Override
public Animal creatAnimal() {
// TODO Auto-generated method stub
return new Tiger();
}
}
package org.westos.demo2;//狗工厂
public class DogFactory implements AinmalFactory{
@Override
public Animal creatAnimal() {
return new Dog();
}
}
package org.westos.demo2;//猫工厂
public class CatFactory implements AinmalFactory {
@Override
public Animal creatAnimal() {
// TODO Auto-generated method stub
return new Cat();
}
}
package org.westos.demo2;//动物类
public abstract class Animal {
public abstract void eat();
}
package org.westos.demo2;//猫类
public class Cat extends Animal {
public void eat() {
System.out.println("猫吃鱼");
}
}
package org.westos.demo2;//狗类
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("狗吃肉");
}
}
package org.westos.demo2;//老虎类
public class Tiger extends Animal {
@Override
public void eat() {
System.out.println("老虎吃鸡肉");
}
}
package org.westos.demo2;//测试类
public class Test {
public static void main(String[] args) {
CatFactory catFactory = new CatFactory();
Animal cat = catFactory.creatAnimal();
cat.eat();
new DogFactory().creatAnimal().eat();
new TigerFactory().creatAnimal().eat();
}
}
单例模式
单例模式的设计思想是保证内存中只有一个对象
方法:私有化构造,本身提供一个对象,通过公共的方法让外界访问
饿汉式:
package org.westos.demo3;
public class Student {
// 第二步:我自己在内部来创建一个对象
//单例设计模式之饿汉式
private static Student student = new Student();
// 第一步:私有化构造
private Student() {
super();
// TODO Auto-generated constructor stub
}
// 第三步:提供一个静态的公共的方法,返回这个学生对象
public static Student getStundent() {
return student;
}
}
package org.westos.demo3;
public class Test {
public static void main(String[] args) {
//单例设计模式之饿汉式
Student stundent = Student.getStundent();
Student stundent2 = Student.getStundent();
System.out.println(stundent == stundent2);
}
}
懒汉式:
package org.westos.demo4;
public class Teacher {
private static Teacher teacher = null;
private Teacher() {// 1.私有化构造
super();
}
// 3.提供公共的静态方法
// 懒汉式
public synchronized static Teacher geTeacher() {
if (teacher == null) {
teacher = new Teacher();
}
return teacher;
}
}
package org.westos.demo4;
public class Test {
public static void main(String[] args) {
// 单例模式之懒汉式 体现是一种延迟加载的思想
Teacher teacher = Teacher.geTeacher();
Teacher teacher2 = Teacher.geTeacher();
System.out.println(teacher == teacher2);
}
}
Runtime类:可以执行某些DOS命令,是单例模式的应用
package org.westos.demo4;
import java.io.IOException;
public class Test2 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Runtime runtime = Runtime.getRuntime();
// exec(command); 可以执行某些DOS命令
runtime.exec("mspaint");
runtime.exec("calc");
}
}
计算程序的耗时
package org.westos.demo5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public abstract class CalcTime {
public void calcTime() throws IOException {
// 计算for循环的耗时
long startTime = System.currentTimeMillis();
// testFor();
// testCopyFile();
testTime();
long endTime = System.currentTimeMillis();
System.out.println("耗时" + (endTime - startTime) + "毫秒");
}
private void testCopyFile() throws FileNotFoundException, IOException {
// 复制文件
File file = new File("E:\\录像1.exe");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File("D:\\录像1.exe"));
byte[] by = new byte[1024];
int len = 0;
while ((len = fis.read(by)) != -1) {
fos.write(by, 0, len);
}
fis.close();
fos.close();
}
private void testFor() {
for (int i = 0; i < 100000; i++) {
System.out.println("");
}
}
public abstract void testTime();
}
package org.westos.demo5;//for循环
public class TestFor extends CalcTime{
@Override
public void testTime() {
for (int i = 0; i < 100000; i++) {
System.out.println("");
}
}
}
package org.westos.demo5;//复制文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestCopyFile extends CalcTime {
@Override
public void testTime() {
try {
// 复制文件
File file = new File("E:\\录像1.exe");
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(new File("D:\\录像1.exe"));
byte[] by = new byte[1024];
int len = 0;
while ((len = fis.read(by)) != -1) {
fos.write(by, 0, len);
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package org.westos.demo5;//测试类
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
CalcTime c= new TestFor();
c.calcTime();
c=new TestCopyFile();
c.calcTime();
}
}