diff --git a/channel/basic/debug b/channel/basic/debug new file mode 100755 index 0000000..faac351 Binary files /dev/null and b/channel/basic/debug differ diff --git a/creational/abstract_factory/car.go b/creational/abstract_factory/car.go new file mode 100644 index 0000000..30e6bcd --- /dev/null +++ b/creational/abstract_factory/car.go @@ -0,0 +1,5 @@ +package main + +type Car interface { + NumDoors() int +} diff --git a/creational/abstract_factory/car_factory.go b/creational/abstract_factory/car_factory.go new file mode 100644 index 0000000..47cf168 --- /dev/null +++ b/creational/abstract_factory/car_factory.go @@ -0,0 +1,24 @@ +package main + +import ( + "errors" + "fmt" +) + +const ( + LuxuryCarType = 1 + FamilyCarType = 2 +) + +type CarFactory struct{} + +func (c *CarFactory) NewVehicle(v int) (Vehicle, error) { + switch v { + case LuxuryCarType: + return new(LuxuryCar), nil + case FamilyCarType: + return new(FamilyCar), nil + default: + return nil, errors.New(fmt.Sprintf("Vehicle of type %d not exist\n", v)) + } +} diff --git a/creational/abstract_factory/cruise_motorbike.go b/creational/abstract_factory/cruise_motorbike.go new file mode 100644 index 0000000..5a13c52 --- /dev/null +++ b/creational/abstract_factory/cruise_motorbike.go @@ -0,0 +1,15 @@ +package main + +type CruiseMotorbike struct{} + +func (s *CruiseMotorbike) NumWheels() int { + return 2 +} + +func (s *CruiseMotorbike) NumSeats() int { + return 2 +} + +func (s *CruiseMotorbike) GetMotorbikeType() int { + return CruiseMotorbikeType +} diff --git a/creational/abstract_factory/family_car.go b/creational/abstract_factory/family_car.go new file mode 100644 index 0000000..3ea2bc2 --- /dev/null +++ b/creational/abstract_factory/family_car.go @@ -0,0 +1,15 @@ +package main + +type FamilyCar struct{} + +func (*FamilyCar) NumDoors() int { + return 5 +} + +func (*FamilyCar) NumWheels() int { + return 4 +} + +func (*FamilyCar) NumSeats() int { + return 5 +} diff --git a/creational/abstract_factory/luxury_car.go b/creational/abstract_factory/luxury_car.go new file mode 100644 index 0000000..a0e26eb --- /dev/null +++ b/creational/abstract_factory/luxury_car.go @@ -0,0 +1,15 @@ +package main + +type LuxuryCar struct{} + +func (*LuxuryCar) NumDoors() int { + return 4 +} + +func (*LuxuryCar) NumWheels() int { + return 4 +} + +func (*LuxuryCar) NumSeats() int { + return 5 +} diff --git a/creational/abstract_factory/main.go b/creational/abstract_factory/main.go index dbe8f2d..cbaa25f 100644 --- a/creational/abstract_factory/main.go +++ b/creational/abstract_factory/main.go @@ -9,3 +9,5 @@ package main // Objectives: Grouping related families of objects is very convenient when your object number is growing so much that // creating a unique point to get them all seems the only way to gain the flexible of runtime object creation. + + diff --git a/creational/abstract_factory/motorbike.go b/creational/abstract_factory/motorbike.go new file mode 100644 index 0000000..049dacc --- /dev/null +++ b/creational/abstract_factory/motorbike.go @@ -0,0 +1,5 @@ +package main + +type Motorbike interface { + GetMotorbikeType() int +} diff --git a/creational/abstract_factory/motorbike_factory.go b/creational/abstract_factory/motorbike_factory.go new file mode 100644 index 0000000..4fccb2f --- /dev/null +++ b/creational/abstract_factory/motorbike_factory.go @@ -0,0 +1,24 @@ +package main + +import ( + "errors" + "fmt" +) + +const ( + SportMotorbikeType = 1 + CruiseMotorbikeType = 2 +) + +type MotorbikeFactory struct{} + +func (m *MotorbikeFactory) Build(v int) (Vehicle, error) { + switch v { + case SportMotorbikeType: + return new(SportMotorbike), nil + case CruiseMotorbikeType: + return new(CruiseMotorbike), nil + default: + return nil, errors.New(fmt.Sprintf("Motor bike of type %d not exist\n", v)) + } +} diff --git a/creational/abstract_factory/sport_motorbike.go b/creational/abstract_factory/sport_motorbike.go new file mode 100644 index 0000000..98a7fd1 --- /dev/null +++ b/creational/abstract_factory/sport_motorbike.go @@ -0,0 +1,15 @@ +package main + +type SportMotorbike struct{} + +func (s *SportMotorbike) NumWheels() int { + return 2 +} + +func (s *SportMotorbike) NumSeats() int { + return 1 +} + +func (s *SportMotorbike) GetMotorbikeType() int { + return SportMotorbikeType +} diff --git a/creational/abstract_factory/vehicle.go b/creational/abstract_factory/vehicle.go new file mode 100644 index 0000000..fa17c60 --- /dev/null +++ b/creational/abstract_factory/vehicle.go @@ -0,0 +1,6 @@ +package main + +type Vehicle interface { + NumWheels() int + NumSeats() int +} diff --git a/creational/abstract_factory/vehicle_factory.go b/creational/abstract_factory/vehicle_factory.go new file mode 100644 index 0000000..6b4efce --- /dev/null +++ b/creational/abstract_factory/vehicle_factory.go @@ -0,0 +1,5 @@ +package main + +type VehicleFactory interface { + NewVehicle(v int) (Vehicle, error) +}