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

  

posted on 2019-05-14 21:18  sw-lab  阅读(428)  评论(0编辑  收藏  举报

导航