Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this bad oop design?

Tags:

I have class called Chicken and in Chicken I have some methods, so in another class where I instantiate and call methods on Chicken, I might do something like this:

Chicken chicken = new Chicken("Name","Description")   public void UpdateChicken(Chicken chicken) {     chicken.Update(chicken); } 

Is the above fine or does it present problems, if so, is it better to have another class, such as ChickenCalculations and do something like:

public void UpdateChick(Chicken chicken) {     ChickenCalculations.Update(chicken); } 

Here is an implementation:

Chicken chicken = new Chicken("Bob","Coolest Chicken", 4, 123, 5, 388, true, false, true);  Chicken anotherChicken = new Chicken() anotherChicken.Update(chicken); chicken.Update(chicken) 

Here is a more practical example instead of using a Chicken:

public class AirlineBooking {     int BookingId {get;set;}     string Name {get;set;}     string Description {get;set;}     decimal Price {get;set;}     decimal Tax {get;set;}     string seat {get;set;}     bool IsActive {get;set;}     bool IsCanceld {get;set;}       public AirlineBooking(string name, string description, decimal price,                            decimal tax, string seat, bool isActive, bool isCanceled)     {         Name = name;         Description = description;         Price = price;         Tax = tax;         Seat = seat;         IsActive = isActive;         IsCanceled = isCanceled;     }      public Update(AirlineBooking airlineBooking, int id)     {           //Call stored proc here to update booking by id     }      public class BookingSystem     {        //Create new booking        AirlineBooking booking = new AirlineBooking("ticket-1",                                                    "desc",150.2,22.0,                                                    "22A",true, false);         //Change properties and update.        booking.Name ="ticket-2";        booking.Description = "desc2";        booking.Price = 200.52;        booking.Tax = 38.50;         public void UpdateBooking(AirlineBooking booking, int id)        {             /* This is the meat of the question, should the passed in booking to                update itself or should I have a Service Class , such as                AirlineBookingOperations with an update method. */             booking.Update(booking,id);        }     } } 
like image 913
Xaisoft Avatar asked Apr 08 '11 14:04

Xaisoft


2 Answers

Why isn't the UpdateChicken function a member of the Chicken class?

That way, you wouldn't have to pass in an instance of a Chicken object, but rather just call the Update method on an existing instance:

Chicken chicken = new Chicken("Name", "Description"); chicken.Update(); 

It's generally best to encapsulate all the methods that operate on a particular class inside of that class, rather than splitting them up into a separate "helper" class. Let them chickens manage themselves!

like image 106
Cody Gray Avatar answered Oct 07 '22 19:10

Cody Gray


The whole idea of Object Oriented Programing is to think of objects as able to act upon themselves.

So you should just use chicken.Update() to update a chicken.

like image 31
Yochai Timmer Avatar answered Oct 07 '22 19:10

Yochai Timmer