-1

I am developing a marketplace for real estate ads and I need some advice on which design pattern to use for creating different types of ads. Each ad can be either a Property or a Residence, and each Property has subclasses such as House, Flat, Garage, etc. Each subclass has its own fields and methods. Each Property also has a type of transaction, such as Buy, BuyNew, Rent, VacationRent, Colocation, etc. Each transaction type also has its own fields and methods. For example, Rent has duration and deposit fields, while VacationRent has rent fields plus vacation rent fields.

I want to know how to create an ad object that can handle different types of properties and transactions without using too many if-else statements or switch cases. I have read about some design patterns such as Strategy, State, Factory Method, etc., but I am not sure which one is the best for my problem. Can anyone suggest a good design pattern for this scenario. I am using PHP as my programming language.

I have searched online for similar problems but I could not find any clear solutions or examples.

Adel taf
  • 33
  • 6
  • 2
    First, create a database design for your application. Create an entity for each subclass and transaction. After you've created all the different entities, then and only then do you start looking for common elements. It may be that your application requires several objects for the flexibility you're seeking. – Gilbert Le Blanc Jun 16 '23 at 10:56
  • I've already done it. i have created tables for :Ad, Property, Residence, Flat, House, Garage, Buy, BuyNew, Rent, VacationRent, ColocationRent – Adel taf Jun 16 '23 at 11:09
  • 1
    @Adeltaf could you show your tables with code? And show, please, some code where you struggle to create objects – StepUp Jun 16 '23 at 11:10

1 Answers1

0

Usually design patterns are started to mention when existing code is necessary to be optimized or when you think that your code have unsatisfied behavioral, structural or creational conditions. So this is a reason why it is hard to say what pattern should be used for your case because there is no code in your question.

I want to know how to create an ad object that can handle different types of properties and transactions without using too many if-else statements or switch cases

I believe you can use creational patterns such as:

StepUp
  • 36,391
  • 15
  • 88
  • 148