如何避免重复代码? [英] How to avoid repeating code?

查看:75
本文介绍了如何避免重复代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,


假设有三个类,其中CA具有CA1类成员和

CA2,如下所示。为了使CA1和CA2的公共功能可以在CA对象中的成员a1和a2上工作,我只需编写所有函数

,例如a1_func1(), CA

接口中的a1_fun2(),a2_func1(),as_func2()。我认为这是一种愚蠢的方式,因为如果CA1和CA2中有更多的

函数,我需要重复它们。有没有什么好的

方式来实现它?


class CA1

{

public:

void func1();

void func2();

...

};


class CA2

{

public:

void func1();

void func2();

...

};


class CA

{

public:

void a1_func1(a1.func1());

void a1_func2(a1.func2());

...

void a2_func1(a2.func1());

void a2_func2(a2.func2());

...

私人:

CA1 a1;

CA2 a2;

...

}


感谢您的帮助。


水生

解决方案

shuisheng写道:


亲爱的所有,


假设有三个班级其中CA具有CA1类成员和

CA2,如下所示。为了使CA1和CA2的公共功能可以在CA对象中的成员a1和a2上工作,我只需编写所有函数

,例如a1_func1(), CA

接口中的a1_fun2(),a2_func1(),as_func2()。我认为这是一种愚蠢的方式,因为如果CA1和CA2中有更多的

函数,我需要重复它们。有没有什么好的

方式来实现它?


class CA1

{

public:

void func1();

void func2();

...

};


class CA2

{

public:

void func1();

void func2();

...

};


class CA

{

public:

void a1_func1(a1.func1());

void a1_func2(a1.func2());

...

void a2_func1(a2.func1());

void a2_func2(a2.func2());

...

私人:

CA1 a1;

CA2 a2;

...

}



我认为最简单的解决方案是使CA类成为虚拟基础

类,CA1和CA2子类。


然后你可以创建一个指向CA的指针容器...


如果不解释整个概念,我的建议是阅读上
多态性的一些例子。


shuisheng写道:


假设有三个类,其中CA具有CA1类成员和

CA2,如下所示。为了使CA1和CA2的公共功能可以在CA对象中的成员a1和a2上工作,我只需编写所有函数

,例如a1_func1(), CA

接口中的a1_fun2(),a2_func1(),as_func2()。我认为这是一种愚蠢的方式,因为如果CA1和CA2中有更多的

函数,我需要重复它们。实现它有什么好的方法吗?



公开CA1和CA2。


您当前的计划只假装他们是私人的。 CA的用户知道

他们,所以他们/事实上/公开。


class CA

{

public:

void a1_func1(a1.func1());

void a1_func2(a1.func2());

...

void a2_func1(a2.func1());

void a2_func2(a2.func2());

...

私人:

CA1 a1;

CA2 a2;

...

}



我知道我们学习的第一个OO规则之一就是不要让数据成员

public"。规则是要制作,遵循和破坏的。在这种情况下,该规则背后的实际规则是不要在全球范围内制作原始的东西

可写。


在你的情况下,CA1和CA2是真实对象,而不是原始数据变量。

滥用它们的后果较少。


如果你只是必须封装,然后编写返回CA1的get()方法

const&另外,不允许CA的用户知道CA1和CA2的存在。 CA应该只接受高级别的请求来做事情,而且b $ b应该自己弄清楚如何做。接口应该告诉,而不是
要求,CA来完成它的任务。


-

Phlip
http://c2.com/cgi/wiki?ZeekLand < - NOT一个博客!!!




shuisheng写道:


亲爱的所有,


假设有三个类,其中CA具有CA1类成员和

CA2,如下所示。为了使CA1和CA2的公共功能可以在CA对象中的成员a1和a2上工作,我只需编写所有函数

,例如a1_func1(), CA

接口中的a1_fun2(),a2_func1(),as_func2()。我认为这是一种愚蠢的方式,因为如果CA1和CA2中有更多的

函数,我需要重复它们。有没有什么好的

方式来实现它?


class CA1

{

public:

void func1();

void func2();

...

};


class CA2

{

public:

void func1();

void func2();

...

};


class CA

{

public:

void a1_func1(a1.func1());

void a1_func2(a1.func2());

...

void a2_func1(a2.func1());

void a2_func2(a2.func2());

...

私人:

CA1 a1;

CA2 a2;

...

}


我感谢您的帮助。



好​​吧,看起来你的班级是移除的候选人,因为它没有b $ b似乎除了放两个对象之外什么都不做一起。另一方面它

可能会做更多...所以我们假设它有一个目的。


这是做你想要的最好的方法。您可以返回a1并让客户端调用该函数,但这会产生一种叫做中间人的气味或者级联调用。当你需要这个电话时,你不能改变你的课程来做其他的事情。它会产生刚性。


其他替代方案是继承,继承也会产生依赖关系

(实际上更多)因此在可能的情况下,组合是更好的选择。 />
除非你需要多态地行事,否则你不想继承...... b $ b继承....不要为了重用而继承。


Dear All,

Assume there are three classes where CA has members of class CA1 and
CA2 as follows. To make the public functions of CA1 and CA2 can work on
the members a1 and a2 in a CA object, I just write all the functions
such as a1_func1(), a1_fun2(), a2_func1(), as_func2() in the CA
interface. I think this is a stupid way since if there are more
functions in CA1 and CA2, I need to repeat them all. Is there any good
way to implement it?

class CA1
{
public:
void func1( );
void func2( );
...
};

class CA2
{
public:
void func1( );
void func2( );
...
};

class CA
{
public:
void a1_func1( a1.func1( ) );
void a1_func2( a1.func2( ) );
...
void a2_func1( a2.func1( ) );
void a2_func2( a2.func2( ) );
...
private:
CA1 a1;
CA2 a2;
...
}

I appreciate your help.

Shuisheng

解决方案

shuisheng wrote:

Dear All,

Assume there are three classes where CA has members of class CA1 and
CA2 as follows. To make the public functions of CA1 and CA2 can work on
the members a1 and a2 in a CA object, I just write all the functions
such as a1_func1(), a1_fun2(), a2_func1(), as_func2() in the CA
interface. I think this is a stupid way since if there are more
functions in CA1 and CA2, I need to repeat them all. Is there any good
way to implement it?

class CA1
{
public:
void func1( );
void func2( );
...
};

class CA2
{
public:
void func1( );
void func2( );
...
};

class CA
{
public:
void a1_func1( a1.func1( ) );
void a1_func2( a1.func2( ) );
...
void a2_func1( a2.func1( ) );
void a2_func2( a2.func2( ) );
...
private:
CA1 a1;
CA2 a2;
...
}

I suppose the simplest solution is to make class CA a virtual base
class, with CA1 and CA2 subclasses.

Then you could create a container of pointers to CA...

Without explaining the entire concept, my advice is to read up on
polymorphism for some examples of this.


shuisheng wrote:

Assume there are three classes where CA has members of class CA1 and
CA2 as follows. To make the public functions of CA1 and CA2 can work on
the members a1 and a2 in a CA object, I just write all the functions
such as a1_func1(), a1_fun2(), a2_func1(), as_func2() in the CA
interface. I think this is a stupid way since if there are more
functions in CA1 and CA2, I need to repeat them all. Is there any good
way to implement it?

Make CA1 and CA2 public.

Your current plan only pretends they are private. Users of CA are aware of
them, so they are /de-facto/ public.

class CA
{
public:
void a1_func1( a1.func1( ) );
void a1_func2( a1.func2( ) );
...
void a2_func1( a2.func1( ) );
void a2_func2( a2.func2( ) );
...
private:
CA1 a1;
CA2 a2;
...
}

I am aware one of the first OO rules we learn is "don''t make data members
public". Rules are meant to be made, followed, and broken. In this case, the
real rule behind that rule is "don''t make primitive things globally
writable".

In your case, CA1 and CA2 are real objects, not primitive data variables.
Abusing them has less consequences.

If you simply must encapsulate, then write get() methods that return CA1
const & and CA2 const &.

Alternately, don''t allow users of CA to even know of CA1 and CA2''s
existence. CA should only accept high-level requests to do things, and it
should figure out for itself how to do them. Interfaces should tell, not
ask, CA to do its tasks.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!



shuisheng wrote:

Dear All,

Assume there are three classes where CA has members of class CA1 and
CA2 as follows. To make the public functions of CA1 and CA2 can work on
the members a1 and a2 in a CA object, I just write all the functions
such as a1_func1(), a1_fun2(), a2_func1(), as_func2() in the CA
interface. I think this is a stupid way since if there are more
functions in CA1 and CA2, I need to repeat them all. Is there any good
way to implement it?

class CA1
{
public:
void func1( );
void func2( );
...
};

class CA2
{
public:
void func1( );
void func2( );
...
};

class CA
{
public:
void a1_func1( a1.func1( ) );
void a1_func2( a1.func2( ) );
...
void a2_func1( a2.func1( ) );
void a2_func2( a2.func2( ) );
...
private:
CA1 a1;
CA2 a2;
...
}

I appreciate your help.

Well, it looks like your class is a candidate for removal as it doesn''t
seem to do anything but put two object together. On the other hand it
probably does more in ... so lets assume it has a purpose.

This is the best way to do what you want. You could return a1 and let
the client call the function but this creates a smell known as a middle
man or cascade calls. You can''t then alter your class to do other
things when that call is needed. It creates rigidity.

Other alternative is inheritance, inheritance also creates dependencies
(more so in fact) so composition is a better choice when possible.
Unless you need to behave polymorphically you don''t want to
inherit....don''t inherit for the purposes of reuse.


这篇关于如何避免重复代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆