Code Monkey home page Code Monkey logo

state-design-pattern's Introduction

State-Design-Pattern

State is a behavioral design pattern that lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.

Problem Statement :

Filters used in vehicle renting web(or Android) applications - If the user chooses CAR section, the internal state changes and only the properties/actions related to cars are displayed/performed or if the user chooses BIKE section, only the properties/actions related to bikes are displayed/performed.

Description :

The State pattern is closely related to the concept of a Finite-State Machine.

At any given moment, there are two states(CAR and BIKE) which a program can be in. Within any unique state, the program behaves differently, and the program can be switched from one state to another instantaneously.

Initially an object of the 'Transport' class is created. This object by default will behave as if it is in 'BIKE' state. The user can list only the bikes using the 'listVehicles' method; select which bike he/she wants using the 'selectNextVehicle' method. When the 'toggle_wheels' method is called on this object, the internal state changes to 'CAR' state. Now the user can list only the cars using the same method 'listVehicles'; select which car he/she wants using the same method 'selectNextVehicle'.

Usage (Python3):

$ python3 client.py

[*] Choose your option :

1) Change state
2) List vehicles in current state
3) Select next vehicle in current state
4) Quit (or Ctrl+C)

>>> 2
1) TVS
2) Yamaha
3) Duke
4) Activa
5) Bullet
>>> 3
[+] Selecting vehicle... Vehicle is TVS  -  BIKE
>>> 3
[+] Selecting vehicle... Vehicle is Yamaha  -  BIKE
>>> 1
[*] Switching to CARS
>>> 2
1) Swift Dzire
2) Verna
3) Renault Kwid
4) Nano
5) Innova
>>> 3
[+] Selecting vehicle... Vehicle is Swift Dzire  -  CAR
>>> 3
[+] Selecting vehicle... Vehicle is Verna  -  CAR
>>> 4
[-] Exiting ...

Author

state-design-pattern's People

Contributors

g0uth4m avatar

Watchers

 avatar  avatar

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.