/From design to pattern
Introduction to Design Patterns
Design
Mode
Separate
From design to pattern
23 Different design modes
Create type
Factory mode (factory method mode, abstract Factory mode, builder mode)
Single-Case mode
Prototype mode
Combination type
Behavioral type
Policy mode
Template method Mode
Observer pattern
Iterator mode
Responsibility-linked Mode
Command mode
Memo Mode
State mode
Visitor mode
Broker mode
Interpreter mode
Structural type
Adapter mode
Adorner mode
Proxy mode
Appearance mode
Bridging mode
Combination mode
Enjoy meta mode
When you take a taxi, you can make a shuttle or express. Any car has a license plate number and a name.
Different prices of different cars, express 1 yuan per kilometer, car 2 yuan per kilometer
Show vehicle information when the trip starts
At the end of the tour, the taxi amount is displayed (assuming travel is 5 km)
Draw a UML type
Write the instance in ES6 syntax
Car-Parent class
Class car{
Constructor (Number,name) {
This.number = number
THIS.name = Name
}
}
Express
Class Kuaiche extends car{
Constructor (Number,name) {
Super (Number,name)
This.price = 1
}
}
Car
Class Zhuanche extends car{
Constructor (Number,name) {
Super (Number,name)
This.price = 2
}
}
Trip
Class trip{
Constructor (car) {
This.car = car
}
Start () {
Console.log (' Run Start, Name: ${this.car.name}, car number: ${this.car.number} ')
}
End () {
Console.log (' End of Run, Price: ' + (This.car.price)
}
}
Test
Let car = new Kuaiche (100, "Beijing Hyundai")
Let-trip = new-Trip (car)
Trip.start ()
Trip.end ()
A parking lot, 3 floors, 100 parking spaces per floor
Every parking space can be monitored for vehicles entering and leaving
Show the number of free spaces on each floor before entering the vehicle
The camera can recognize the license plate number and time when the vehicle enters
The vehicle comes out, the exit shows the license plate number and the length of the parking
//Vehicle
Class car{
Constructor (num) {
This.num = num
}
}
//car park
Class park{
Const Ructor (Floors) {
This.floors = Floors | | []
This.camera = new camera ()
This.screen = new screen ()
This.cartlist = {}//Store
}
in (CAR) {
//
Const INFO = this.camera.shot (car)
Const i = parseint (Math.random () *100%100)
Const Place = This.floors[0].places[i]
place.in ()
Info.place = Place
This.cartlist[car.num] = info
Con Sole.log (this.cartlist)
}
Out (CAR) {
Const info = this.cartlist[car.num]
//talk parking empty
Cons T place = Info.place
Place.out ()
//Display time
This.screen.show (car,info.intime)
//Empty
D Elete This.cartlist[car.num]
}
Emtpynum () {
return This.floors.map (floor =>{
Return ' ${flo Or.index} layer also has ${floor.emptyplacenum ()} spaces '
} '. Join (' \ n ')
}
}
Level
Class floor{
Constructor (index,places) {
This.index =index
This.places = Places | | []
}
Emptyplacenum () {
Let num = 0
This.places.forEach (p = = {
if (p.empty) {
num = num + 1
}
})
Return num
}
}
Parking spaces
Class place{
Constructor () {
This.empty = True
}
In () {
This.empty = False
}
Out () {
This.empty = True
}
}
Camera
Class camera{
Shot (CAR) {
return {
Num:car.num,
InTime:Date.now ()
}
}
}
Export display
Class screen{
Show (Car,intime) {
Console.log ("License plate number", car.num)
Console.log ("Parking Time", Date.now ()-InTime)
}
}
Initializing parking
Const FLOORS = []
for (Let i = 0;i<3;i++) {
Const PLACES = []
for (Let J = 0;j<100;j++) {
PLACES[J] = new Place ()
}
Floors[i] = new Floor (i+1,places)
}
Const PARK = new Park (floors)
Const CAR1 = new Car (100)
Const CAR2 = new Car (200)
Const CAR3 = new Car (300)
Park.in (CAR1)
Console.log (Park.emtpynum ())
Park.in (CAR2)
Console.log (Park.emtpynum ())
Park.out (CAR1)
Console.log (Park.emtpynum ())
Park.out (CAR2)
Console.log (Park.emtpynum ())
Park.in (CAR3)
Console.log (Park.emtpynum ())
Park.out (CAR3)
Console.log (Park.emtpynum ())
1 floors and 99 parking spaces.
2 floors and 100 parking spaces.
3 floors and 100 parking spaces.
1 floors and 98 parking spaces.
2 floors and 100 parking spaces.
3 floors and 100 parking spaces.
Car No. 100
Parking Time 17
1 floors and 99 parking spaces.
2 floors and 100 parking spaces.
3 floors and 100 parking spaces.
Car No. 200
Parking Time 3
1 floors and 100 parking spaces.
2 floors and 100 parking spaces.
3 floors and 100 parking spaces.
1 floors and 99 parking spaces.
2 floors and 100 parking spaces.
3 floors and 100 parking spaces.
Car No. 300
Parking Time 5
1 floors and 100 parking spaces.
2 floors and 100 parking spaces.
3 floors and 100 parking spaces.
JavaScript-oriented object-to-face test case