jeudi 28 mai 2020

Identify an Object by property or type

I have a question regarding the design pattern in java.

I have a class named Bottle. Now, I have two kinds of Bottle: glass bottle, and metal bottle.

Which of the following design patterns should I use: 1. Set type of bottle as its property

    class Bottle {
        public enum BottleType { GLASS, METAL }

        BottleType type;
    }
  1. Use inheritance
    abstract class Bottle {
    }

    class GlassBottle extends Bottle {
    }

    class MetalBotle extends Bottle {
    }

Update my question Bottle will be processed by a processor named BottleProcessor

    class BottleProcessor {
        public static breakBottle(Bottle _bottle) {
            //TODO: break bottle
            if _bottle is glass bottle 
                breakGlassBottle()
            else if _bottle is metal bottle 
                breakMetalBottle()
        }
    }

Aucun commentaire:

Enregistrer un commentaire