首先引入几个大神的博客
创建型模式
单例模式
单例模式要求在整个应用中保证只有一个类存在
饿汉模式
public class Singleton {
/* -----------------饿汉模式------------------- */
//1.私有的构造方法
private Singleton(){}
//2.创建唯一实例private static final修饰
private static final Singleton instance = new Singleton();
//3.提供获取实例的方法public static修饰
public static Singleton getInstance(){
return instance;
}
}
在类加载的时候就会创建对象
懒汉模式
public class Singleton {
//1.私有的构造方法
private Singleton(){}
/* -----------------懒汉模式-------------------- */
private volatile static Singleton instance = null;
public static Singleton getInstance(){
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
懒汉模式需要添加volatile
和synchronized
修饰,用来保证在大量并发情况下线程的安全
静态内部类的方式
public class Singleton {
/* -----------------饿汉模式------------------- */
//1.私有的构造方法
private Singleton(){}
///* -----------------静态内部类的方式------------------- */
private static class InnerSingleton{
private static final Singleton instance = new Singleton();
}
public static Singleton getInstance(){
return InnerSingleton.instance;
}
}
工厂模式
工厂模式主要是为创建对象提供了接口。工厂模式分为三类:
1. 简单工厂模式(Simple Factory)
2. 工厂方法模式(Factory Method)
3. 抽象工厂模式(Abstract Factory)
简单工厂模式
接口 | 实现类 | 工厂类 |
---|---|---|
Car | SmallCar ,BigCar | CarFactory |
接口–Car.java:
package design_pattern.factory;
public interface Car {
public void driveCar();
}
实现类–SmallCar.java和BigCar.java:
package design_pattern.factory;
public class SmallCar implements Car{
public void driveCar() {
System.out.println("SmallCar...");
}
}
package design_pattern.factory;
public class BigCar implements Car{
public void driveCar() {
System.out.println("BigCar...");
}
}
工厂类CarFactory:
package design_pattern.factory;
public class CarFactory {
//根据名字返回Car对象
public Car getCar(String carName){
if (carName.equalsIgnoreCase("SmallCar")) {
return new SmallCar();
}else if (carName.equalsIgnoreCase("BigCar")) {
return new BigCar();
}else
return null;
}
}
测试类:
package design_pattern.factory;
public class Test {
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
Car car = carFactory.getCar("bigcar");
car.driveCar();
}
}
输出:
BigCar...
在CarFactory.java
中要想获得car
类要在方法中写很多的if..
,在很多car
的情况下,非常不方便。于是想到java的反射来实现:
- 创建type.properties:
smallcar=design_pattern.factory.SmallCar
bigcar=design_pattern.factory.BigCar
- 创建读取type.properties的类:
package design_pattern.factory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class PropertiesReader {
public Map<String, String> getProperties(){
Properties props = new Properties();
Map<String, String>map = new HashMap<String, String>();
InputStream is = this.getClass().getResourceAsStream("type.properties");
try {
props.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Enumeration en = props.propertyNames();
while(en.hasMoreElements()){
String key = (String) en.nextElement();
String property = props.getProperty(key);
map.put(key, property);
}
return map;
}
}
CarFactory.java
添加方法:
//根据类名利用反射生成对象
public Car getCarByReflect(String carName){
//获得所有类名完整路径的map
Map<String, String>map = new PropertiesReader().getProperties();
try {
Car car = (Car) Class.forName(map.get(carName)).newInstance();
return car;
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
- 测试:
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
// Car car = carFactory.getCar("bigcar");
// car.driveCar();
Car car = carFactory.getCarByReflect("smallcar");
car.driveCar();
- 输出:
SmallCar...
抽象工厂模式
简单来讲就是把工厂抽象出来,实现不同的工厂,然后产生不同的类
类:
接口 | 实现类 |
---|---|
Person | FatPerson,ThinPerson |
Person.java:
package design_pattern.factory;
public interface Person {
public void say();
}
FactPerson.java:
package design_pattern.factory;
public class FatPerson implements Person{
public void say() {
System.out.println("FatPerson...");
}
}
ThinPerson.java:
package design_pattern.factory;
public class ThinPerson implements Person{
public void say() {
System.out.println("ThinPerson...");
}
}
工厂:
接口 | 实现类 |
---|---|
PersonFactory | FatPersonFactory,ThinPersonFactory |
PersonFactory.java:
package design_pattern.factory;
public interface PersonFactory {
public Person getPerson();
}
FatPersonFactory.java:
package design_pattern.factory;
public class FatPersonFactory implements PersonFactory{
public Person getPerson() {
return new FatPerson();
}
}
ThinPersonFactory.java:
package design_pattern.factory;
public class ThinPersonFactory implements PersonFactory{
public Person getPerson() {
return new ThinPerson();
}
}
测试类:
package design_pattern.factory;
public class Test {
public static void main(String[] args) {
PersonFactory personFactory = new FatPersonFactory();
Person person = personFactory.getPerson();
person.say();
}
}
输出:
FatPerson...