如果我通过代码使用它,我有一个“框架”。这很好,因为并非所有运行框架的系统都安装了图形资料。
现在我想为这个框架创建一个图形用户界面。因为框架的一个大的,自我管理的组合最终运行,我不想创建一个并行的GUI层次结构,并重新实现GUI的所有自我管理的东西以及组织。的确,我的想法是为每个班级创建一个扩展,例如: G。调用gui
,后面可以包含在主窗口中。因此,我会将其描绘如下:
在这个插图中,我不喜欢多重继承。正如m2()
示例所示,它引入了一些问题。在这里,问题是:“m(2)
执行哪个ConcreteGui
,来自AbstractGui
的更新的AbstractClass
或来自ConcreteClass
的{{1}}过时的class AbstractClass:
def __init__(self):
self.a = 23
self.b = 42
def m1(self):
return self.a + self.b
def m2(self):
self.b += 1
return self.a * 2
class ConcreteClass1(AbstractClass):
def __init__(self):
super().__init__() # constructor of AbstractClass
self.c = 13
self.d = 7
def m3(self):
return self.a - self.d
def m4(self):
self.a += 1
return self.b / self.c
? “
我可以使用哪种方法或设计模式?
修改
让我们假设以下给定的框架(它是一个与语言无关的问题,Python仅用作说明)。
select "year","Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (
select "year",'1er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 1 and 3 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'2º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 4 and 6 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'3er Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 7 and 9 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
select "year",'4º Trimestre' as "Trimestre",COALESCE(sum("base"),0) as "base",COALESCE(sum("iva"),0) as "iva",COALESCE(sum("total"),0) as "total" from (Select Year("p_fpagado") as "year",month("p_fpagado") as "month",COALESCE(sum("im_base"),0) as "base",COALESCE(sum("im_calculado"),0) as "iva",COALESCE(sum("im_total"),0) as "total" from "Facturas" where "p_pagado" = True and month("p_fpagado") between 10 and 12 group by Year("p_fpagado"),month("p_fpagado")) group by "year"
union all
(select distinct year("p_fpagado") as "year",'1er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'2º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'3er Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0
union all
select distinct year("p_fpagado") as "year",'4º Trimestre' as "Trimestre",0 as "base",0 as "iva",0 as "total" from "Facturas" having year("p_fpagado") != 0)
) group by "year","Trimestre" order by "year" desc,"Trimestre" asc
现在,每个属性都应该获得一个GUI小部件作为用户友好的表示(显示当前值)以及接口(设置一个值)。为此,我不想重写所有方法来使用特定小部件而不是属性。添加的小部件应该对所有已实现的方法都是透明的。
答案 0 :(得分:0)
您无法从ConcreteClass和AbstractGUIClass派生,您可能必须将GUI实现分开并将它们注入ConcreteGuiClass
public class ConcreteClassGui1 : ConcreteClass1 {
private IGuiSpecificOperations _guiSpecificOperations;
public ConcreteClassGui1(IGuiSpecificOperations guiSpecificOperations){
_guiSpecificOperations = guiSpecificOperations;
SetSomeGuiProperties();
}
~ConcreteClassGui1(){
}
public override Method2()
{
// specific implementation
}
private void SetGuiProperties()
{
_guiSpecificOperations.PropertyGuiA = "Test";
_guiSpecificOperations.PropertyGuiB = 10;
}
}
public class ConcreteClassGui2 : ConcreteClass2 {
private IGuiSpecificOperations _guiSpecificOperations;
public ConcreteClassGui2(IGuiSpecificOperations guiSpecificOperations){
_guiSpecificOperations = guiSpecificOperations;
}
~ConcreteClassGui2(){
}
public override Method2()
{
// specific implementation
_guiSpecificOperations.PropertyGuiA = "Text";
_guiSpecificOperations.PropertyGuiB = 10;
}
}
public class GuiSpecificOperationImplementation : IGuiSpecificOperations {
public GuiSpecificOperationImplementation(){
}
~GuiSpecificOperationImplementation(){
}
public string PropertyGuiA{ get; set;}
public int PropertyGuiB {get; set;}
}
public interface IGuiSpecificOperations
{
public string PropertyGuiA{ get; set;}
public int PropertyGuiB {get; set;}
}
还注意到IGuiSpecificOperations的实现已经注入ConcreteGuiClass而不是派生它们。