-2
Class Book
{
  private int year;
  private String session;
  private int volume;
  private int number;
  private String khand;
  private Date proceeding_date;
  private int pageNo;
  Book(year,session,volume)
  {
    $this.year=year;
    $this.session=session;
    $this.volume=volume;
  }
 public getBookName()
 {
   return (year+session+volume)  //concatenation of three
 }   
}

Book b1=new Book(1952,abc,123);

Suppose my requirements change and now the creation of book also include the proceeding_date in the constructor parameters, after code is compiled I am allowed to make changes in the class or there is a design flaw?

The class should be designed so that it will accept any changes in future, how I fit the OOAD principle in my design, did my class follow SRP, open/closed principle.

Suppose there is requirement to add more private variable in class will it break the open/closed principle? Is added new members [variables/methods] in a class breaks the OCP?

2 Answers2

2

suppose my requirement change and now the creation of book also include the proceeding_date in the constructor paramters, after code is compiled di i m allowed to make changes in the class or there is a design flaw, the class should be designed so that it will accept any changes in future,

You always have to change your code to implement new requirements.

The open/closed principle aims to minimize the impact of this changes. But a single (DTO) class is the wrong place to talk about that.

However: One of the more important programming principles (not only in OOP) is You ain't gonna need it!. This means you should not introduce complexity or indirection just because you think it could be useful in future without having an actual need justified by you current requirements.

So back to your example this means you should not add "generic" properties to the class just because you might need some more in future. Add new properties as needed when requirements change.

0

Depends on the language. If it is loosely typed and supports passing JSON-y stuctures (e.g. JavaScript) or key-value args (e.g. Python) they are a good way to pass additional or "optional" arguments.

In any language, especially a strongly typed language like Java, you can consider using a Fluent Builder Pattern. Your code would look something like

Book b1=new Book(1952,abc,123).proceedingDate(1957)

Note: that's a mishmash of a traditional constructor and a fluent builder, normally it would be all one or the other.

Book b1 = new BookBuilder()
  .date(1952)
  .session("abc")
  .volume(123)
  .proceedsDate(1951)
  .build();
user949300
  • 9,009