五种常用设计模式
五种常见的设计模式,通过这五种设计模式来实际了解设计模式的大体实现方式
- 单例模式
- 工厂模式
- 抽象工厂模式
- 装饰器模式
- 桥接模式
单例模式
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
该类负责自己的创建,同时确保自己只有单个类被创建,还要提供唯一一个访问自己方法,并且可以不需要创建可以直接访问
- 该类只能有一个实例对象
- 自己创建自己的唯一实例对象
- 必须给其他对象提供这一实例
创建单例对象
public class Alone {
//创建一个 Alone 的一个静态对象
private static Alone alone = new Alone();
//构造方法私有化
private Alone() {}
//获取唯一的一个可用对象
public static Alone getAlone() {
return alone;
}
//功能方法
public void message() {
System.out.println("Hello World!");
}
}
代用实例对象
public class Function {
public static void main(String[] args) {
//获取对象
Alone alone = Alone.getAlone();
//调用方法
alone.message();
}
}
工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们创建对象时不会体现创建的方式,只是通过一个共同的接口来指向新创建的对象
优点:
- 想调用一个对象,我们只需要知道对象名
- 扩展性高,想增加功能,只需要新增一个工厂类
- 隐藏具体实现,调用者只需要关注接口
实现代码:
先定义一个接口
public interface Factory {
public void draw();
}
实现接口方法
//工厂猫
public class Cat implements Animal {
@Override
public void draw() {
System.out.println("工厂制造玩具猫");
}
}
//工厂狗
public class Dog implements Animal {
@Override
public void draw() {
System.out.println("工厂制造玩具狗");
}
}
工厂类创建获取方法
public class Factory {
//实用get方法获取对应的实现类
public Animal getAnimal(String name) {
if (name == null) {
return null;
} else if (name.equals("Dog")) {
return new Dog();
} else if (name.equals("Cat")) {
return new Cat();
} else {
return null;
}
}
}
输出结果
public class Function {
public static void main(String[] args) {
//获取对象
Factory factory = new Factory();
//获取需要的实体类
Animal animalDog = factory.getAnimal("Dog");
Animal animalCat = factory.getAnimal("Cat");
//调用实现方法
animalDog.draw();
animalCat.draw();
}
}
抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在创建中,会出现不止一个工厂类型,于是需要一个超级工厂来管理这些工厂,大致分为三级
超级工厂管理工厂 -> 工厂管理类 -> 具体实现类
创建两个工厂,动物与食粮
//宠物抽象类
public interface Pet {
public void draw();
}
//宠物狗
public class Dog implements Pet {
@Override
public void draw() {
System.out.println("购买了宠物狗");
}
}
//宠物猫
public class Cat implements Pet {
@Override
public void draw() {
System.out.println("购买了宠物猫");
}
}
//食粮抽象类
public interface Food {
public void draw();
}
//狗粮
public class FoodDog implements Food{
@Override
public void draw() {
System.out.println("购买了狗粮");
}
}
//猫粮
public class FoodCat implements Food {
@Override
public void draw() {
System.out.println("购买了猫粮");
}
}
宠物与食粮工厂类
//抽象工厂类
public abstract class AbstractFactory {
public abstract Pet getColor(String color);
public abstract Food getShape(String shape) ;
}
//宠物
public class PetFactory extends AbstractFactory {
@Override
public Pet getPet(String name) {
if (name == null) {
return null;
} else if (name.equals("Dog")) {
return new Dog();
} else if (name.equals("Cat")) {
return new Cat();
} else {
return null;
}
}
@Override
public Food getFood(String food) {
return null;
}
}
//食粮
public class FoodFactory extends AbstractFactory {
@Override
public Pet getPet(String pet) {
return null;
}
@Override
public Food getFood(String food) {
if (food == null) {
return null;
} else if (food.equals("FoodDog")) {
return new FoodDog();
} else if (food.equals("FoodCat")) {
return new FoodCat();
} else {
return null;
}
}
}
超级工厂类,用来创建工厂
public class FactoryProducer {
public static AbstractFactory getFactory(String name) {
if (name.equals("Food")) {
return new FoodFactory();
}else if (name.equals("Pet")) {
return new PetFactory();
}
return null;
}
}
输出结果
public class Function {
public static void main(String[] args) {
//食粮
AbstractFactory food = FactoryProducer.getFactory("Food");
//宠物
AbstractFactory pet = FactoryProducer.getFactory("Pet");
//宠物狗
Pet dog = food.getPet("Dog");
//宠物猫
Pet cat = food.getPet("Cat");
//狗粮
Food foodDog = pet.getFood("FoodDog");
//猫粮
Food foodCat = pet.getFood("FoodCat");
//输出
dog.draw();
cat.draw();
foodDog.draw();
foodCat.draw();
}
}
装饰器模式
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
也就在现有对象保持不变的情况下,再添加一些内容,增加其功能
创建接口及实现类
//动物-叫声
public interface Animal {
public void call();
}
//狗会叫
public class Dog implements Animal {
@Override
public void call() {
System.out.println("汪-汪-汪");
}
}
装饰类
//抽象类
public abstract class DecorateAnimal implements Animal {
protected Animal animal;
public DecorateAnimal(Animal animal) {
this.animal = animal;
}
public void call() {
animal.call();
}
}
//实现类
public class EatDecorator extends DecorateAnimal {
public EatDecorator(Animal animal) {
super(animal);
}
@Override
public void call() {
animal.call();
eat(animal);
}
private void eat(Animal animal) {
System.out.println("狗吃东西");
}
}
实现方法
public class Function {
public static void main(String[] args) {
Animal dog = new Dog();
Animal eatDecorator = new EatDecorator(dog);
eatDecorator.call();
}
}
桥接模式
桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化。这种类型的设计模式属于结构型模式,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。
可以使实体类的功能独立yu于接口实现类,实体类与接口实现类的联系变弱,可以实现实体类的单方面扩展
具体代码:
接口及实现类:
//抽象类
public interface Bridge {
public void adopt(String name,int length,int weight);
}
//实现类
public class Vehicle implements Bridge {
@Override
public void adopt(String name,int length, int weight) {
System.out.println("通过车辆的车牌号为:"+name+",长度为"
+length+"米,重量为"+weight+"千克。"
);
}
}
桥接的抽象类
public abstract class Shape {
protected Bridge bridge;
protected Shape(Bridge bridge) {
this.bridge = bridge;
}
public abstract void draw();
}
桥接实现类
public class Place extends Shape {
private String name;
private int length;
private int weight;
public Place(String name,int length,int weight,Bridge bridge) {
super(bridge);
this.name = name;
this.length = length;
this.weight = weight;
}
@Override
public void draw() {
bridge.adopt(name, length, weight);
}
}
main方法测试
public class Function {
public static void main(String[] args) {
Shape shape = new Place("123456",2,1000,new Vehicle());
shape.draw();
}
}