[Java] Object Oriented Programming

William Parker·2022년 11월 22일
post-thumbnail

Object oriented programming

Object-Oriented Programming (OOP) is one of the paradigms of computer programming. Object-oriented programming seeks to view a computer program as a collection of independent units, or “objects,” rather than as a list of instructions. Each object can send and receive messages and process data.

Object-oriented programming is often used in large-scale software development because it makes programs flexible and easy to change. It also has the advantage of making programming easier to learn, easier software development and maintenance, and enabling more intuitive code analysis. However, the excessive objectification of programs is criticized for failing to reflect the real world as it is. (by wiki)

Polymorphism

Internal functionality can be changed without changing the client (requester).

// change the status function.
// Return status as "off".
class Plug {
    public void on() {}
    public void off() {}
}

interface InternetOfThings {
    String status();
}

class SmartPlug extends Plug implements InternetOfThings {
     public String status() {
         ...
         ...
         return "ok";
     }
}

class OffPlug extends Plug implements InternetOfThings {
		public String status() {
        return "off";
    }
}


SmartPlug sp = new SmartPlug();
sp.on();
sp.off();
OffPlug op = new OffPlug();

Plug p = sp;
p.on();
p.stop();

InternetOfThings iot = sp; -> InternetOfThings iot = op

iot.status(); // sp -> ok, op -> off

SOLID

  • SRP (Single Responsibility Principle) - Single Responsibility Principle
    • A class should have only one responsibility.
    • A single responsibility can be large or small.
    • One criterion of responsibility is change.
      • When changes occur, the single responsibility principle is well followed if the number of parts to be changed is small.
      • There should be only one reason to change a class.
    • Example) The location of the payment button has been changed, but there is no effect on the payment function.
  • OCP (Open/Closed Principle) - Open/closed principle
    • Open for extension, but closed for modification.
    • Reduce the cost of change and maximize the cost of expansion.
    • A very useful principle that maximizes the advantages of object orientation (feat, polymorphism).
    • Look for the template method pattern → A good example of the OCP principle.
  • LSP (Liskov Substitution Principle) - Liskov Substitution Principle
    • A subtype (child) must always be interchangeable with its supertype (parent).
    • Do not break accuracy.
    • Why is it important?
    • If the principle is not followed, the method may behave strangely when an instance of a child class is passed as a parameter.
      • Among the speaker functions, volume up is a function to raise the sound. Volume up on Harman Kardon speakers should not reduce the sound. Even if the sound is small, you have to raise it. → Only then can you trust and write.
    • Unit tests written for the parent class will not work for the child class.
    • If you are using inheritance well, you are already doing LSP
  • ISP (Interface segregation principle) - interface segregation principle
    • Clients should not depend on methods they do not use.
    • For a specific client, it is better to provide multiple interfaces rather than one generic interface.
  • DIP (Dependency Inversion Principle) - Dependency Inversion Principle
    • A dependency relationship is formed in the process of cooperation between objects.
    • The dependency inversion principle is a guideline on how to respond to changes easily when establishing such a dependency relationship.
    • Distinguish between changeable and difficult.
      • changeable
        • specific action
          • Make a call on your smartphone.
          • Make a call from a public phone.
          • send an e-mail
          • Deliver KakaoTalk messages.
      • hard to change
        • abstract things such as flows or concepts
          • make a phone call
          • convey the message
profile
Developer who does not give up and keeps on going.

0개의 댓글