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();
推荐阅读
-
Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
-
设计模式之单例模式(懒汉式单例、饿汉式单例、登记式单例)
-
Spring,SpringMvc,MyBatis用到的设计模式
-
python生产者和消费者模式实现(三)进程池方式
-
苹果努力扩产:纬创将在印度新工厂生产iPhone PCB
-
python生产者和消费者模式实现(一)普通方式
-
ASP.NET Core 2.2 WebApi 系列【七】泛型仓储模式和工作单元
-
android基础教程之夜间模式实现示例
-
代理模式(Proxy Pattern)
-
设计模式六大原则:里氏替换原则