给定以下一组类:
Camera, Photo, Photoshoot, Subject.他们将如何相互作用,以模拟以下句子?
下一个主题叫做并拍摄他们的照片。
我想看看是否有更好的解决方案。
对不起,我应该在C#上说的。
发布于 2011-04-27 01:12:00
有很多不同的方法来看待这个问题。如果我是在面试的话,我会接受这个的。
Photoshoot shoot = new Photoshoot(
new Camera("Fuji"),
new Subject("Bill"),
new Subject("Dave"),
new Subject("Claire")
);
IEnumerable<Photo> photos = shoot.TakeAllPhotos();哪里
class Photoshoot
{
private readonly IEnumerable<Subject> _subjects;
private readonly Camera _camera;
public Photoshoot(Camera camera, params Subject[] subjects)
{
_subjects = subjects;
_camera = camera;
}
public IEnumerable<Photo> TakeAllPhotos()
{
List<Photo> photos = new List<Photo>();
foreach(Subject subject in _subjects)
{
// Here is the interaction in question
Call(subject);
_camera.TakePhotoOf(subject);
}
return photos;
}
}所以互动是这样的:
摄影类的TakeAllPhotos方法通过拍摄的主体循环,然后调用一个被摄者,然后用它的相机对该对象进行TakePhotoOf。
在不同的一天,我可能会把相机传递给TakeAllPhotos方法,但今天我认为相机属于摄影。
还请注意,我将subject传递给照片拍摄的调用方法(到目前为止还没有定义),而不是在主题上调用调用方法。在我看来,这门学科不应该知道如何称呼自己。
这些都是面试官希望你进行的谈话。具体的互动并不是那么重要。
发布于 2011-04-27 00:37:36
Photoshoot p = new Photoshoot(new Camera("Name of really expensive camera"));
Subject bob = new Subject("Bob");
Photo photo = p.TakePhotoOf(bob);这样好吗?
发布于 2011-04-27 04:44:16
没有首选的解决办法。如果您有Camera.TakePhoto(subject)或Photoshoot.TakePhoto(subject,camera),这并不重要。即使有一组完整的需求,它也可能没有多大的区别。有些设计可能比其他的好,但从来没有一个“最好”的设计。
https://softwareengineering.stackexchange.com/questions/71573
复制相似问题