欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Matlab工厂模式

程序员文章站 2022-06-27 22:13:46
工厂模式定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。工厂模式主要分为简单工厂模式、工厂方法模式以及抽象工厂模式。 Obj.m Object1.m Object2.m 简单工厂模式: SimpleFactory.m 测试代码: 工厂方法模式: Metho ......

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

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();