设计模式添加一个新类 [英] Design pattern to add a new class

查看:156
本文介绍了设计模式添加一个新类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班,学生和老师,学生有一个具体的方法:takeCourse;老师有一个具体的方法:教学课程。



现在我要添加一个新的Class,GradStudent,可以像学生一样学习,还可以教授课程,如老师。哪种模式是实现这个新类的最简单的方法?适配器,复合,委托....?



谢谢。

解决方案

问题应该是什么是最好的方法来建模,答案将是在现实世界中有道理的方式,即毕业生也可以教导和考虑; - )



不要尝试将所有内容与设计模式联系起来。坚持我们在学校学到的基本原则课堂模范现实世界的对象,让模特靠近现实世界

  interface CanTeach 
{
void teachCource();
}

class教师实现CanTeach {...}

class学生
{
void takeCourse(...);
}

class GradStudent扩展学生实体CanTeach
{
...
}


I have two classes, Student and Teacher, Student has one concrete method: takeCourse; Teacher has one concrete method: teachCourse.

Now I want to add a new Class, GradStudent, which can take course, like Student, and also can teach course, like Teacher. Which pattern is the easiest way to implement this new class ? Adapter, Composite, Delegate .... ?

Thanks.

解决方案

The question should be what is the best way to model this for which the answer would be "the way it makes sense in the real world" i.e. A Grad student can also teach and take course ;-)

Do not try to relate everything to the design patterns. Stick to the basic principle that we learnt in school "Classes model real world objects", "Keep the model close to the real world"

interface CanTeach
{
void teachCource();
}

class Teacher implements CanTeach {...}

class Student 
{
void takeCourse(...);
}

class GradStudent extends Student implements CanTeach
{
...
}

这篇关于设计模式添加一个新类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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