• Matlab工厂模式


    工厂模式定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。工厂模式主要分为简单工厂模式、工厂方法模式以及抽象工厂模式。

    Obj.m

    classdef Obj < handle
        methods
            function desc(~)
            end
        end
    end
    

    Object1.m

    classdef Object1 < Obj
        methods
            function desc(~)
                disp('This is Object1 class');
            end
        end   
    end
    

    Object2.m

    classdef Object2 < Obj
       methods
            function desc(~)
                disp('This is Object2 class');
            end
        end   
    end
    

    简单工厂模式:

    SimpleFactory.m

    classdef SimpleFactory   
        methods
            function res = createObject(~,obj_name)
                if  strcmp(obj_name,'Object1')
                    res = Object1();
                elseif strcmp(obj_name,'Object2')
                    res = Object2();
                else
                    res = Object.empty();
                end
            end
            function res = createObjectEval(~,obj_name)
                res = eval(obj_name);
            end
            function res = createObjectStr2Func(~,obj_name)
                consHandle = str2func(obj_name);
                res = consHandle();
            end
            function res = createObject1(~)
                res = Object1();
            end
            function res = createObject2(~)
                res = Object2();
            end
        end
        methods(Static)
            function res = createObjectStatic(obj_name)
                if  strcmp(obj_name,'Object1')
                    res = Object1();
                elseif strcmp(obj_name,'Object2')
                    res = Object2();
                else
                    res = Object.empty();
                end
            end
        end
    end

    测试代码:

    %简单工厂模式
    fac = SimpleFactory();
    obj1 = fac.createObject('Object1');
    obj1.desc();
    obj2 = fac.createObject('Object2');
    obj2.desc();
     
    %通过eval函数
    fac = SimpleFactory();
    obj1 = fac.createObjectEval('Object1');
    obj1.desc();
    obj2 = fac.createObjectEval('Object2');
    obj2.desc();
     
    %通过str2func函数
    fac = SimpleFactory();
    obj1 = fac.createObjectStr2Func('Object1');
    obj1.desc();
    obj2 = fac.createObjectStr2Func('Object2');
    obj2.desc();
     
    %静态工厂模式
    obj1 = SimpleFactory.createObjectStatic('Object1');
    obj1.desc();
    obj2 = SimpleFactory.createObjectStatic('Object2');
    obj2.desc();
    
    %多方法工厂模式
    fac = SimpleFactory();
    obj1 = fac.createObject1();
    obj1.desc();
    obj2 = fac.createObject2();
    obj2.desc();
    

    工厂方法模式:

    MethodFactory.m

    classdef MethodFactory   
        methods(Abstract)
            factoryMethod(~);
        end
        methods
            function dosomething(obj)
                disp('This is method factory!')
                o = obj.factoryMethod();
                o.desc();
            end
        end
    end
    

    MethodFactory1.m

    classdef MethodFactory1 < MethodFactory   
        methods
            function res = factoryMethod(~)
                res = Object1();
            end
        end
    end
    

    MethodFactory2.m

    classdef MethodFactory2 < MethodFactory   
        methods
            function res = factoryMethod(~)
                res = Object2();
            end
        end
    end
    

    测试代码:

    %工厂方法模式
    fac1 = MethodFactory1();
    fac1.dosomething();
    fac2 = MethodFactory2();
    fac2.dosomething();
    

    抽象工厂模式:

    IFactory.m

    classdef IFactory
        methods(Abstract)
            createObject(~);
        end
    end
    

    Obj1Factory.m

    classdef Obj1Factory < IFactory
        methods
            function res = createObject(~)
                res = Object1();
            end
        end
    end
    

    Obj2Factory.m

    classdef Obj2Factory < IFactory
        methods
            function res = createObject(~)
                res = Object2();
            end
        end
    end

    测试代码

    fac1 = Obj1Factory();
    obj1 = fac1.createObject();
    obj1.desc();
     
    fac2 = Obj2Factory();
    obj2 = fac2.createObject();
    obj2.desc();
    

      

  • 相关阅读:
    javascript js 内存泄露
    javascript js 内存泄露工具使用
    逻辑运算符及按位或与非比较难理解先放着87
    //测试全局变量及局部变量的输出结果的异同
    域名被墙原因
    编写转义字符:78页
    1.1-java创建包和类的方法
    浏览器方法及代码打包成APP的
    一个简单的jsp+servlet实例,实现简单的登录
    LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)
  • 原文地址:https://www.cnblogs.com/usaddew/p/10864666.html
Copyright © 2020-2023  润新知