1、单例模式
当需要使用某个全局共享访问点或共享数据时,或某个工具类时使用。
(1)静态内部类形式
public class ProductRobot {
private RobotFactory robotFactory;
public static ProductRobot getInstance(){
return ProductHolder.productRobot;
}
private static class ProductHolder{
private static ProductRobot productRobot=new ProductRobot();
}
}
(2)DCL:双重检查模式
private static volatile ProductRobot productRobot;
public static ProductRobot getProductRobot(){
if (productRobot==null){
synchronized (ProductRobot.class){
if (productRobot==null) productRobot=new ProductRobot();
}
}
return productRobot;
}
特点:返回某个类的引用
二、工厂模式
定义:定义一个用于创建对象的接口,让子类决定实例化哪个类。工厂方法使一个类的实例延迟到其子类。
(1)产品抽象类
public interface Robot extends ProxyMode{
String TAG="RobotPrint";
void run();
void walk();
void talk();
void createHead(String head);
void createBody(String body);
void createCPU(String cpu);
void printMessage();
void buy();
}
(2)具体产品类
public class CatRobot implements Robot {
@Override
public void run() {
LogUit.printLog("i",TAG,"机器猫跑步了");
}
@Override
public void walk() {
LogUit.printLog("i",TAG,"机器猫走路");
}
@Override
public void talk() {
LogUit.printLog("i",TAG,"我是机器猫");
}
private String head,body,cpu;
@Override
public void createHead(String head) {
this.head=head;
}
@Override
public void createBody(String body) {
this.body=body;
}
@Override
public void createCPU(String cpu) {
this.cpu=cpu;
}
@Override
public void printMessage() {
LogUit.printLog("i",TAG,"head="+head+",body="+body+",cpu="+cpu);
}
@Override
public void buy() {
LogUit.printLog("i",TAG,"机器猫购物");
}
}
(3)工厂抽象类
public interface RobotFactoryMode {
<T extends Robot> T createRobot(Class<T> tClass);
}
(4)具体工厂类
public class RobotFactory implements RobotFactoryMode {
private Robot robot;
@Override
public <T extends Robot> T createRobot(Class<T> tClass) {
try {
robot= (Robot) Class.forName(tClass.getName()).newInstance();//发射
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
return (T) robot;
}
}
这样,当调用工厂的生产方法createRobot时将产品类传递进去即可产生具体产品。
特点:适用于生产不同的子类对象,这些子类具有相同的基类
三、建造者模式
定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
包含:Director、Builder、Product
(1)产品类(如上)
(2)Builder:相同的执行步骤
public interface BuilderMode {
void createHead(String head);
void createBody(String body);
void createCPU(String cpu);
Robot create();
}
public class RobotBuilder implements BuilderMode{
private Robot robot;
public RobotBuilder(Robot robot){
this.robot=robot;
}
@Override
public void createHead(String head) {
robot.createHead(head);
}
@Override
public void createBody(String body) {
robot.createBody(body);
}
@Override
public void createCPU(String cpu) {
robot.createCPU(cpu);
}
@Override
public Robot create() {
return robot;
}
}
(3)Director:导演类,负责安排已有模块顺序,通过Builder开始建造
public class Director {
private RobotBuilder robotBuilder;
public Director(RobotBuilder robotBuilder){
this.robotBuilder=robotBuilder;
}
public Robot createRobot(String head,String body,String cpu){
robotBuilder.createHead(head);
robotBuilder.createBody(body);
robotBuilder.createCPU(cpu);
return robotBuilder.create();
}
}
特点:适用于不同的子类有相同的执行步骤,建造产品嘛
四、装饰器模式
定义:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器比生成子类更为灵活。
(1)对象(如上)
(2)装饰器类:
public interface DecoratorMode {
String TAG="RobotPrint";
void behavior();
}
/**
* 具体装饰器
*/
public class RobotDecorator implements DecoratorMode{
private Robot robot;
public RobotDecorator(Robot robot){
this.robot=robot;
}
private void eat(String fish) {
LogUit.printLog("i",TAG,"机器人吃"+fish+"了");
}
private void drink(String water) {
LogUit.printLog("i",TAG,"机器人喝"+water+"了");
}
public Robot getRobot() {
return robot;
}
@Override
public void behavior() {
eat("鱼");drink("牛奶");
eat("猫粮");drink("水");
}
}
这样,只要继承DecoratorMode 实现装饰器类都可以对具体对象Robot的功能进行扩充,调用装饰器时既可以使用具体对象的方法又可以调用装饰器内新实现的方法。
优点:通过组合而非继承的方式,动态地扩展一个对象的功能,运行时选择不同的装饰器从而实现不同的功能
缺点:如果抽象对象发生改变,所有装饰器和具体对象(被装饰者)都受影响;由于比继承更灵活,装饰模式更加易于出错,也更加难以排错。
网友评论