1. 适配器模式【读卡器连接内存卡和笔记本】
- 适配器模式作为两个不兼容的接口之间的桥梁
【读卡器连接内存卡和笔记本】
- 适配器模式实现流程
public interface MediaPlayer{}
public interface AdvancedMediaPlayer{
}
public class VlcPlayer implements AdvancedMediaPlayer{
}
public class Mp4Player implements AdvancedMediaPlayer{
}
public class MediaAdapter implements MediaPlayer{
AdvancedMediaPlayer advancedMediaPlayer;
}
public class AudioPlayer implements MediaPlayer{
MediaAdapter mediaAdapter;
}
2. 桥接模式【一个接口当作另一个接口中的一个对象】
- 用于把抽象化与实现化解耦,使得二者可以独立变化。通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦。
【一个接口当作另一个接口中的一个对象】
- 桥接模式实现流程
public interface DrawAPI{public void drawCircle(int radius, int x, int y);}
public class RedCircle implements DrawAPI{
public class GreenCircle implements DrawAPI{
public abstract class Shape {
protected DrawAPI drawAPI;
protected Shape(DrawAPI drawAPI){
this.drawAPI = drawAPI;
}
public abstract void draw();
}
public class Circle extends Shape {
private int x, y, radius;
public Circle(int x, int y, int radius, DrawAPI drawAPI) {
super(drawAPI);
this.x = x;
this.y = y;
this.radius = radius;
}
public void draw() {
drawAPI.drawCircle(radius,x,y);
}
}
public class BridgePatternDemo {
public static void main(String[] args) {
Shape redCircle = new Circle(100,100, 10, new RedCircle());
Shape greenCircle = new Circle(100,100, 10, new GreenCircle());
redCircle.draw();
greenCircle.draw();
}
}
3. 过滤器模式【筛选出满足要求的值】
- 使用不同的标准来过滤掉一组对象,通过逻辑运算以解耦的方式把它们连接起来。
【筛选出满足要求的值】
- 过滤器模式的实现流程
public class Person{name, gender, maritalStatus, 还有有参构造和getxxx方法}
public interface Criteria{
public List<Person> meetCriteria(List<Person> persons)
}
public class CriteriaMale implements Criteria {
}
public class CriteriaFemale implements Criteria {
}
public class CriteriaSingle implements Criteria {
}
public class AndCriteria implements Criteria{
private Criteria criteria;
private Criteria otherCriteria;
public AndCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria = criteria;
this.otherCriteria = otherCriteria;
}
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> firstCriteriaPersons = criteria.meetCriteria(persons);
return otherCriteria.meetCriteria(firstCriteriaPersons);
}
}
public class OrCriteria implements Criteria {
private Criteria criteria;
private Criteria otherCriteria;
public OrCriteria(Criteria criteria, Criteria otherCriteria) {
this.criteria = criteria;
this.otherCriteria = otherCriteria;
}
@Override
public List<Person> meetCriteria(List<Person> persons) {
List<Person> firstCriteriaItems = criteria.meetCriteria(persons);
List<Person> otherCriteriaItems = otherCriteria.meetCriteria(persons);
for (Person person : otherCriteriaItems) {
if(!firstCriteriaItems.contains(person)){
firstCriteriaItems.add(person);
}
}
return firstCriteriaItems;
}
}
4. 组合模式【套娃模式】
- 把一组相似的对象当作一个单一的对象。
【套娃模式】
- 组合模式实现流程
public class Employee{
private List<Employee> subordinates;
public void add(Employee e) {subordinates.add(e);}
}
public class Demo{
Employee CED = xxx;
Employee headSales = xxx;
Employee headMarketing = xxx;
Employee cleark1 = xxx;
Employee cleark2 = xxx;
Employee salesExecutive1 = xxx;
Employee salesExecutive2 = xxx;
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExecutive1);
headSales.add(salesExecutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
}
5. 装饰器模式【类似于子类继承】
- 允许向一个现有的对象添加新的功能,同时又不改变其结构。
【类似于子类继承】
- 装饰器模式的实现流程
public interface Shape{}
public class Rectangle implements Shape{}
public class Circle implements Shape{}
public abstract class ShapeDecorator implements Shape{
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){
decoratedShape.draw();
}
}
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
}
Shape circle = new Circle();
ShapeDecorator redCircle = new RedShapeDecorator(new Circle());
ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangle());
circle.draw();
redCircle.draw();
redRectangle.draw();
6. 外观模式【外观展现,实现隐藏】
- 隐藏系统的复杂性,并向客户端提供一个客户端可以访问系统的接口。
【外观展现,实现隐藏】
- 外观模式实现流程
public interface Shape{}
public class Rectangle implements Shape{}
public class Square implements Shape{}
public class Circle implements Shape{}
public class ShapeMaker{}
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.xxxCircle();
shapeMaker.xxxRectangle();
shapeMaker.xxxSquare();
7. 享元模式【重复利用模式】
- 主要用于减少创建对象的数量
【重复利用模式】
- 享元模式的实现流程
public inteface Shape{}
public class Circle implements Shape{}
public class ShapeFactory{
private static final HashMap<String, Shape> circleMap = new HashMap<>();
}
8. 代理模式【生产商和经销商】
- 一个类代表另一个类的功能。
【生产商和经销商】
- 代理模式实现流程
public interface Image{}
public class RealImage implements Image{}
public class ProxyImage implements Image{
@Override
public void display(){
if(realImage == null){
realImage = new RealImage(fileImage);
}
realImage.display();
}
}