创建型模式:这些模式处理对象的创建过程,隐藏创建逻辑不是直接用new实例化对象。
- 单例模式
public class DatabaseConnection{
private static DatabaseConnection instance;
//私有的构造方法,防止外部直接实例化
public DatabaseConnection(){
// todo
}
// 公有静态方法,提供全局访问点
public static DatabaseConnection getInstance(){
//双重检测
if(instance == null){
synchronized (DatabaseConnection.class){
if(instance == null){
instance = new DatabaseConnection();
}
}
}
return instance;
}
}
- 工厂模式
public interface Animal{
void speak();
}
public class Dog implements Animal{
@Override
public void speak(){
// todo
}
}
public class Cat implements Animal{
@Override
public void speak(){
//todo
}
}
public class AnimalFactory{
public static Animal getAnimal(String type){
if(StringUtils.equals(type,"dog")){
return new Dog();
}else if(StringUtils.equals(type,"cat")){
return new Cat();
}
return null;
}
}
// 客户端代码
public class AnimalShelter {
public void adopt(String animalType) {
Animal animal = AnimalFactory.getAnimal(animalType);
if(animal != null){
animal.speak();
}
}
}
public class Main{
public static void main(String[] args){
AnimalShelter shelter = new AnimalShelter();
shelter.adopt("dog");
AnimalShelter shelter = new AnimalShelter();
shelter.adopt("cat");
}
}
这里每次新增新的类型还是得改工厂类,仍然要加if else,然后就有更好的抽象工厂模式
- 抽象工厂模式(新的类型只需要添加新的实现类和工厂就行了)
public interface Animal{
void speak();
}
public class Dog implements Animal{
@Override
public void speak(){
// todo
}
}
public class Cat implements Animal{
@Override
public void speak(){
//todo
}
}
public interface AnimalFactory{
Animal createAnimal();
}
public class DogFactory implements AnimalFactory{
@Override
public Animal createAnimal(){
return new Dog();
}
}
public class CatFactory implements AnimalFactory{
@Override
public Animal createAnimal(){
return new Cat();
}
}
public class AnimalShelter {
public void adopt(AnimalFactory factory) {
Animal animal = factory.createAnimal();
animal.speak();
}
}
public class Main{
public static void main(String[] args){
AnimalShelter shelter = new AnimalShelter();
shelter.adopt(new DogFactory());
AnimalShelter shelter = new AnimalShelter();
shelter.adopt(new CatFactory());
}
}
- 建造者模式
- 原型模式 就是复制对象,原型模式复制对象的开销小于直接创建,并且可以保留目前对象的状态
public Prototype{
Prototype clone();
}
class Circle implements Prototype{
private int radius;
private String color;
@Override
public Prototype clone(){
return new Circle(radius,color);
}
}
class Square implements Prototype{
private int radius;
private String color;
@Override
public Prototype clone(){
return new Square(radius,color);
}
}
class ShapeCache{
private static Map<String, Prototype> shapeMap = new HashMap<>();
public static void loadCache() {
Circle circle = new Circle(5, "Red");
Square square = new Square(4, "Blue");
shapeMap.put("1", circle);
shapeMap.put("2", square);
}
public static Prototype getShape(String shapeId) {
Prototype cachedShape = shapeMap.get(shapeId);
return (cachedShape != null) ? cachedShape.clone() : null;
}
}
public class PrototypePatternDemo {
public static void main(String[] args) {
ShapeCache.loadCache();
Prototype clonedShape = ShapeCache.getShape("1");
System.out.println("Shape : " + clonedShape.toString());
Prototype anotherClonedShape = ShapeCache.getShape("2");
System.out.println("Shape : " + anotherClonedShape.toString());
}
}
这里的拷贝可以延伸深拷贝和浅拷贝。