Use prototype pattern or not

空扰寡人 提交于 2019-12-06 00:30:51
Jeff Willener

The Decorator Pattern is probably the most straight forward one to use and would be a good one to extend concrete objects functionality and/or characteristics.

Here is some light reading: Head First Design Patterns - CH3 pdf

FYI, couple must have's for learning and referencing design patterns regardless your language of choice:

1) Head First Design Patterns

2) Patterns for Enterprise Application Architecture

3) Design Patterns: Elements of Reusable Object-Oriented Software

And sites:

1) DoFactory

2) StackOverflow Design Patterns Newbie

There are a few others, I'll have to dig them up.

Does each type of car requires different behaviour? A petrol van acts different from a diesel van? A Saloon has to behave different than an Estate?

If I understand correctly, you need something like that

public enum FuelType
{
     Petrol,
     Diesel
}

public class Car
{
   public string Name {get;set;}
   public FuelType Fuel {get;set;}
}

public class Van:Car { } 
public class CityCar:Car { }

If the characterstics of a Van are just different values than a Car, you don't need subclassing, you need to change just a property value. A Van for example should have other properties or a differnt implementation of methods to be eligible for subclassing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!