tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
252
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    DarthShader DarthShader ist offline Mitglied Platin
    Registriert seit
    May 2004
    Beiträge
    684
    Hallo,

    ich habe hier 2 Enums, wobei das erste die Kategorien für die Werte des zweiten stehen:
    • Automarke
    • Modell

    Es gibt z.b. "Automarke.AUDI", wobei es dann "Modell.A6" gibt. Nun wird ja ganz deutlich, dass das zweite Enum quasi fachlich abhängig vom ersten ist, da das erste ja die Kategorie darstellt.

    Gibt es vielleicht eine geschicktere Möglichkeit, z.b. mit einer EnumMap o.Ä., diese beiden Enums tatsächlich zu verschachteln, sodass man quasi Automarke.AUDI.A8 oder so schreiben kann? Wie würdet Ihr diese beiden Enums aufbauen - so wie ich lose gekoppelt nebeneinander her existieren lassen oder vielleicht doch was mit einer EnumMap?


    Über Eure Ideen würde ich mich sehr freuen


    Vielen Dank!
     

  2. #2
    RoCMe RoCMe ist offline Mitglied Gold
    Registriert seit
    Dec 2007
    Beiträge
    193
    Hallo!

    Warum brauchst du denn unbedingt Enums? Enums sind toll, aber hier meiner Meinung nach nicht das Mittel der Wahl...


    Bau dir zwei "echte" Klassen "Automarke" und "Modell", wobei jede Automarke eine Liste von Modellen beinhaltet. Das ist meiner Meinung nach sauber und spätestens, wenn du irgendwann dein Datenmodell erweiten musst, wirst du dich über diese saubere Implementierung freuen.

    Gruß,

    RoCMe
     

  3. #3
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo,

    schau mal hier:
    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    
    package de.tutorials.training;
     
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
     
    import de.tutorials.training.Brands.Audi.AudiType;
    import de.tutorials.training.Brands.Mercedes.MercedesType;
     
     
    public interface Brands {
     
      Mercedes MERCEDES = new Mercedes();
      
      public static class Mercedes extends AbstractBrand<MercedesType>{
        public static class MercedesType extends BaseType{
          public MercedesType(String name) {
            super(name);
          }
        }
        
        public static final MercedesType A = new MercedesType("A");
        public static final MercedesType B = new MercedesType("B");
        public static final MercedesType C = new MercedesType("C");
        public static final MercedesType D = new MercedesType("D");
        public static final MercedesType E = new MercedesType("E");
        public static final MercedesType S = new MercedesType("S");
        public static final MercedesType M = new MercedesType("M");
        
        {
          add(A,B,C,D,E,S,M);
        }
      }
     
      
      Audi AUDI = new Audi();
      
      public static class Audi extends AbstractBrand<AudiType>{
        public static class AudiType extends BaseType{
          public AudiType(String name) {
            super(name);
          }
        }
        
        public static final AudiType A3 = new AudiType("A3");
        public static final AudiType A4 = new AudiType("A4");
        public static final AudiType A5 = new AudiType("A5");
        public static final AudiType A6 = new AudiType("A6");
        public static final AudiType A7 = new AudiType("A7");
        public static final  AudiType A8 = new AudiType("A8");
        
        {
          add(A3,A4,A5,A6,A7,A8);
        }
      }
      
      public static abstract class AbstractBrand<T extends Type> implements Brand<T>{
        protected List<T> types = new ArrayList<T>();
        private final String name;
        
        public AbstractBrand() {
          this.name = getClass().getSimpleName();
        }
     
        protected void add(T ... types){
          for(T type : types){
            this.types.add(type);
          }
        }
        
        public Iterator<T> iterator() {
          return types.iterator();
        }
        
        public String name() {
          return name;
        }
        
        @Override
        public String toString() {
          return this.name;
        }
      }
      
      public static class BaseType implements Type{
        private final String name;
     
        public BaseType(String name) {
          this.name = name;
        }
        
        public String name() {
         return this.name();
        }
        
        @Override
        public String toString() {
          return name;
        }
      }
      
      public static interface Brand<TType extends Type> extends Iterable<TType>{}
      public static interface Type {}
    }

    Code java:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    
    package de.tutorials.training;
     
    import static de.tutorials.training.Brands.AUDI;
    import static de.tutorials.training.Brands.MERCEDES;
    import static java.util.Arrays.asList;
    import de.tutorials.training.Brands.Audi.AudiType;
    import de.tutorials.training.Brands.Brand;
    import de.tutorials.training.Brands.Mercedes.MercedesType;
    import de.tutorials.training.Brands.Type;
     
     
     
    public class HandMadeEnumExample {
      public static void main(String[] args) {
        System.out.println(Brands.Mercedes.A);
        
        
       
        for(MercedesType type : MERCEDES){
          System.out.println(type);
        }
        
        /* Compile Fehler... :)
        for(MercedesType type : Brands.Audi){
          System.out.println(type);
        }
        */
        
        for(AudiType type : AUDI){
          System.out.println(type);
        }
        
       
        System.out.println("#######");
        
        for(Brand<? extends Type> brand : asList(MERCEDES, AUDI)){
          System.out.println("Brand: " + brand);
          for(Type type : brand){
            System.out.println(type);
          }
        }
        
      }
    }

    Gruß Tom
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

Ähnliche Themen

  1. Datensatz geschickt kopieren.
    Von topf im Forum Relationale Datenbanksysteme
    Antworten: 3
    Letzter Beitrag: 12.12.07, 15:57
  2. Geschickt IE- Größen-Bug umgehen ?
    Von cosmanova im Forum CSS
    Antworten: 8
    Letzter Beitrag: 21.09.07, 14:35
  3. Geschickt freistellen.
    Von Vale-Feil im Forum Photoshop
    Antworten: 10
    Letzter Beitrag: 11.07.05, 21:21
  4. Antworten: 2
    Letzter Beitrag: 20.04.05, 21:12
  5. Antworten: 6
    Letzter Beitrag: 21.05.04, 18:55