M
M
m0Hze2014-05-20 15:53:23
Design patterns
m0Hze, 2014-05-20 15:53:23

How to solve this nicely with OOP?

Rebzi, tell me how to solve this beautifully, from the point of view of OOP.
The source class is given, in which there are 2 methods: save, delete.
There are 2-N "types" of materials that this class can work with. With each material, you can carry out all these actions, but with each you need to do it differently. If you approach the task from the point of view: this is how it will work, then you can do it like this:

public function save($one,$type){
if($type == 'type_1'){
//some actions
}elseif($type == 'type_2'){
//else actions
}
}
..

It is quite logical that this is a simple and effective way, and for many years it served faithfully, but (!).
Now the task has expanded a bit, and there can be more than 2 "types", and therefore, finishing the code in this way is not a breeze.
Question: How to solve this problem using modern OOP techniques (interfaces, inheritance, etc). So that for each type it would be possible to simply and easily expand the function, say, by adding a new class.
If it's not difficult for you, then you can with code examples.
// Forgot to clarify. Operations with methods occur according to the same algorithm, some data needed for writing / reading, working over the network (different parameters of the post' request) change.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Solovyov, 2014-05-20
@m0Hze

probably in your case you need a design pattern Factory method.

class MaterialFactory 
{
    public static function build($type)
    {
        // тут генерим путь до класса на основе его типа
        return new $className();
    }
}

class Material
{
    public function save()
    {
        // тут свои действия
    }
}

class SuperMaterial
{
    public function save()
    {
        // тут другие действия
    }
}

then in your method
public function save($one,$type){
     $obj = MaterialFactory::build($type);
     $obj->save();
}

but the scheme can be further improved, it all depends on your current code

A
andfadeev, 2014-05-20
@andfadeev

The code is in Java, but the essence of OOP is the same. The material is the interface, the concrete material is its implementation. We feed the interface into the methods of the first class, then polymorphism works.

class A {
  void save(Material m) {
    m.save();
  }
  void delete(Material m) {
    m.delete();
  }
}


interface Material {
  void save();
  void delete();
}

class MaterialNumberOne  implements Material  {
  void save() {
    //делаем что нужно для первого материала
  }
  void delete() {
    //делаем что нужно для первого материала
  }
}

class MaterialNumberTwo  implements Material  {
  void save() {
    //делаем что нужно для второго материала
  }
  void delete() {
    //делаем что нужно для второго материала
  }
}


// юзаем так
public static void main(String[] args) {
  Material m1 = new MaterialNumberOne();
  Material m2 = new MaterialNumberTwo();

  A a = new A();
  a.save(m1);
  a.delete(m1);
  a.save(m2);
  a.delete(m2);
}

A
Alexander Kubintsev, 2014-05-20
@akubintsev

interface StorableMaterial
{
    public function save();
}

class Material implements StorableMaterial
{
    public function save()
    {
        // ...
    }
}

class SuperMaterial implements StorableMaterial
{
    public function save()
    {
        // ...
    }
}

And then you need to look at the class where the save() method is pulled from and work there with an abstraction in the form of the SavableMaterial interface, and not with a specific class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question