Code Monkey home page Code Monkey logo

patterns's People

Contributors

markmc27 avatar robbield avatar

Watchers

 avatar  avatar  avatar  avatar

patterns's Issues

State pattern needs state method

I like your Bike example but it doesn't follow my understanding of the State pattern.

I think the Add and Remove components is a good concept since adding some shocks to a road bike makes it a hybrid, so the state would update from RoadBikeState to HybridBikeState.

I think the example could go one step further and have a method called "NegotiatePothole()" or something that internally calls state.NegotiatePothole(). The outcome (behaviour) of this would then vary depending on the current state of the bike. E.g. if it's a mountain bike, it would ride over it, but if it was a road bike, it would crash or buckle. I think this would make the State example clearer - the Bike remains a Bike but it can be in different states and the behaviour of the bike changes depending on what state it's in.

Flyweight example improvements

My understanding of Flyweight is that it reuses heavy/expensive objects across multiple other objects. For example, in the bike race scenario, I think a better example of the flyweight pattern would be something like:

Competitor: A competitor has a race number, height, weight, name, and a Bike (abstract class). There will be one type of each Bike instantiated and used across the application via the Flyweight pattern.

Bike has methods like Accelerate, NegotiatePothole, Brake, etc, and properties like TopSpeed and Weight. So you could then interact with any competitor using competitor.Bike.Brake() and the braking speed would be defined by the Bike's weight and brake types.

The one part of this that I can't figure out is how the Bike is then added to the Competitor - maybe the competitor constructor has a Bike parameter?

You've already covered the immutability of the Flyweight object by using protected for the speed - would readonly be helpful here too?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.