design patterns – Should validation logic be inside a factory method or inside the object’s constructor?

Say I have a hypothetical factory method whose single responsibility is to create MyObjects. However, MyObject should only ever be constructed with an ordered list. Further, MyObjects without an ordered list should never exist my world.

Is it the responsibility of the factory to make sure the list is ordered before creating a new MyObject?

public class Factory {
    public MyObject make(List<Integer> myList) {
        // Should I check if the myList is ordered here?
        if (myList is not ordered) {
            throw new IllegalArgumentException("List not ordered");
        }
        return new MyObject(myList);
    }
}

Or is it the responsibility of the Object itself to make sure the list is ordered in the constructor as shown below?

public class MyObject {
    public MyObject(List<Integer> myList) {
        // Or should I check if myList is ordered here?
        if (myList is not ordered) {
            throw new IllegalArgumentException("List is not ordered");
        }
    }
}

My own arguments in favour of option 1:

  • Since the factory’s sole job is to create MyObjects, it feels somewhat reasonable that this validation logic should be the responsibility of the thing that is making the object.

My own arguments in favour of option 2:

  • By keeping validation logic in MyObject‘s constructor, we increase the Cohesion of our code (from GRASP), as this validation logic is directly relevant to MyObject itself.
  • We also improve modularity of our MyObject class since it can be easily re-used in other areas of our code. For example, if we went with option 1, we would need to duplicate the validation logic before constructing a MyObject directly (if we decided for whatever reason we didn’t want to use our Factory from option 1).

My own opinion is that Option 2 is probably the more preferred approach in this specific instance. What are people’s thoughts?

bootloader – How factory reset process looks in details?

So I tried to find answer for this question, but it is very hard to find something other than simple instruction of doing factory reset and backuping data.

I’m interested of detailed process of the factory reset.
When I force system to do this, what is going under the shell.
It is some image on hidden partition from bootloader boots “clear” system? Or maybe it’s just deleting user configuration files, and every corrupted system file states same as before reset. Or maybe there is the way of checking hash of critical files and repair them from some kind of recovery image?

macbook pro – Cannot see drive when trying to factory reset

I’m currently trying to factory reset my 2015 macbook pro, but when I start it up, click cmd+r and try to erase it from disk utility, it doesn’t show up. I also tried doing a diskutil list command, and my main hard drive wasn’t listed on there. When I ran the same command on a normal boot, it was listed.

One important thing to note is that I installed an aftermarket SSD since my old Apple one was too small. Could that have something to do with it?

Overall, I’m not too sure why it’s not showing up in the boot screen. Anyone have any idea why? Thanks in advance.

haskell – Build a fixed-length vector from a recursive factory function

I need a function of type forall (n :: Nat). RandomGen q => q -> Vec n q. Obviously this is possible to do (correctly, don’t just repeat) using split.

(Documentation links: Nat, RandomGen, Vec, repeat, split, induction1.)

I generalized this to not specifically be about RNGs by taking the splitting/unfolding function as an argument. I’m not wedded to this decision; I don’t think it makes much difference.

import Data.Type.Nat (SNatI, induction1)
import Data.Vec.Lazy (Vec(VNil, (:::)))
import qualified Data.Vec.Lazy as Vec

unfold :: SNatI n =>
          (a -> (a, a)) ->
          a ->
          Vec n a
unfold uf value = induction1 VNil (vs -> let v ::: vs' = vs `Vec.snoc` value
                                              (v', v'') = uf v
                                          in Vec.init $ v' ::: v'' ::: vs')

It works, but it’s pretty clunky looking. Also, consider an example like

> import Data.Nat (Nat(Z, S))
> unfold (n -> (n * 2, n*2)) 1 :: Vec (S(S(S(S(S Z))))) Int
↪ 32 ::: 32 ::: 16 ::: 8 ::: 4 ::: VNil

That’s not wrong, but I think it would be just as correct (and less weird looking) if it came out to
32 ::: 16 ::: 8 ::: 4 ::: 2 ::: VNil.

Anyone know what options I’m overlooking?

linux – How should i factory reset SD card / USB drive st MacOS can automatically mount it?

After taking SD card from my IP camera (some version of linux), the card could not be mounted in MacOS anymore. In Mac DiskUtils, the card was said to have 0 free space and 100% used even after formatting to FAT32 using Mac DiskUtils.

On the other hand, the card could be mounted in linux. I wiped the filesystem, dd zeros to the disk, created a new partition (W95 FAT32), and mkfs.vfat. It worked just fine in linux. But when put it back in Mac, it still could not be mounted automatically. It can only be mounted using command line in Mac Terminal.

Is there a way to “factory reset” a USB drive or SD card such that it’s like new to Mac or Windows?

dependency injection – Return a Closure from a Factory

I have services that depend on request information. I’m trying to create a Factory Factory which will have the Dependency Injection container returns a factory (an anonymous function) that will create the services I want. So basically somthing like this:

class FooFactoryFactory {
  public static function create(ContainerInterface $container): callable {
     return static function (Request $request) use ($container) {
       $raz = request->get('param_raz');
       return new Bar(
           $container->get(LoggerInterface::class),
           $raz,
       );
     };
  }

}

The issue I have is that the Closure is properly created, but de Drupal’s Dependency Injection Container class tries to set a proprty to the Closure. This cannot work and ends in a fatal error:

Closure object cannot have properties

Here is the place where it occurs:

// DrupalComponentDependencyInjection/Container::createService() - Line 283-293
if (isset($definition('properties'))) {
  if ($definition('properties') instanceof stdClass) {
    $definition('properties') = $this->resolveServicesAndParameters($definition('properties'));
  }
  foreach ($definition('properties') as $key => $value) {
    if(!$service instanceof Closure){
      $service->{$key} = $value;  // <-- Will not work for Closure
    }
  }
}
...

So the questions I have:

  • Is this by design? Meaning is it something thta is not wanted in term of coding practice?
  • The error can be avoided if I manage to make sure, that the service I want to create does not have properties it it’s definition. I just don’t know how to achieve this.

Thanks for the help on this matter.

Confusion regarding Factory Reset via TWRP

Background The OS is Android 10 that uses FBE as encryption type.

Problem I do not remember my PIN, that would decrypt the files in /data partition, so I cannot unlock my screen.

Question I can boot into recovery (TWRP). And, naturally, TWRP asks for PIN to decrypt the files.

But if I choose not to enter the PIN, and decrypt the files, and go straight to do a Factory Reset via TWRP, will I be able to access my files (images, media) in internal storage? (/data/media/0/)