Difference between Inheritance and Composition

前端 未结 17 2293
忘了有多久
忘了有多久 2020-11-22 02:35

Are Composition and Inheritance the same? If I want to implement the composition pattern, how can I do that in Java?

17条回答
  •  时光取名叫无心
    2020-11-22 03:26

    Are Composition and Inheritance the same?

    They are not same.

    Composition : It enables a group of objects have to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies

    Inheritance: A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits.

    If I want to implement the composition pattern, how can I do that in Java?

    Wikipedia article is good enough to implement composite pattern in java.

    Key Participants:

    Component:

    1. Is the abstraction for all components, including composite ones
    2. Declares the interface for objects in the composition

    Leaf:

    1. Represents leaf objects in the composition
    2. Implements all Component methods

    Composite:

    1. Represents a composite Component (component having children)
    2. Implements methods to manipulate children
    3. Implements all Component methods, generally by delegating them to its children

    Code example to understand Composite pattern:

    import java.util.List;
    import java.util.ArrayList;
    
    interface Part{
        public double getPrice();
        public String getName();
    }
    class Engine implements Part{
        String name;
        double price;
        public Engine(String name,double price){
            this.name = name;
            this.price = price;
        }
        public double getPrice(){
            return price;
        }
        public String getName(){
            return name;
        }
    }
    class Trunk implements Part{
        String name;
        double price;
        public Trunk(String name,double price){
            this.name = name;
            this.price = price;
        }
        public double getPrice(){
            return price;
        }
        public String getName(){
            return name;
        }
    }
    class Body implements Part{
        String name;
        double price;
        public Body(String name,double price){
            this.name = name;
            this.price = price;
        }
        public double getPrice(){
            return price;
        }
        public String getName(){
            return name;
        }
    }
    class Car implements Part{
        List parts;
        String name;
    
        public Car(String name){
            this.name = name;
            parts = new ArrayList();
        }
        public void addPart(Part part){
            parts.add(part);
        }
        public String getName(){
            return name;
        }
        public String getPartNames(){
            StringBuilder sb = new StringBuilder();
            for ( Part part: parts){
                sb.append(part.getName()).append(" ");
            }
            return sb.toString();
        }
        public double getPrice(){
            double price = 0;
            for ( Part part: parts){
                price += part.getPrice();
            }
            return price;
        }   
    }
    
    public class CompositeDemo{
        public static void main(String args[]){
            Part engine = new Engine("DiselEngine",15000);
            Part trunk = new Trunk("Trunk",10000);
            Part body = new Body("Body",12000);
    
            Car car = new Car("Innova");
            car.addPart(engine);
            car.addPart(trunk);
            car.addPart(body);
    
            double price = car.getPrice();
    
            System.out.println("Car name:"+car.getName());
            System.out.println("Car parts:"+car.getPartNames());
            System.out.println("Car price:"+car.getPrice());
        }
    
    }
    

    output:

    Car name:Innova
    Car parts:DiselEngine Trunk Body
    Car price:37000.0
    

    Explanation:

    1. Part is a leaf
    2. Car contains many Parts
    3. Different Parts of the car have been added to Car
    4. The price of Car = sum of ( Price of each Part )

    Refer to below question for Pros and Cons of Composition and Inheritance.

    Prefer composition over inheritance?

提交回复
热议问题